ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ContiguousDeque.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------- File ContiguousDeque.h -------------------------*/
3/*--------------------------------------------------------------------------*/
46/*--------------------------------------------------------------------------*/
47/*----------------------------- DEFINITIONS --------------------------------*/
48/*--------------------------------------------------------------------------*/
49
50#ifndef __ADS_ContiguousDeque
51 #define __ADS_ContiguousDeque
52/* self-identification: #endif constant */
53
54/*--------------------------------------------------------------------------*/
55/*------------------------------ INCLUDES ----------------------------------*/
56/*--------------------------------------------------------------------------*/
57
58#include "ADS/Contract.h"
59
60#include <algorithm>
61#include <cstddef>
62#include <cstring>
63#include <initializer_list>
64#include <iterator>
65#include <memory>
66#include <new>
67#include <type_traits>
68#include <utility>
69
70/*--------------------------------------------------------------------------*/
71/*--------------------------- NAMESPACE ------------------------------------*/
72/*--------------------------------------------------------------------------*/
73
74namespace ADS
75{
76
77/*--------------------------------------------------------------------------*/
78/*------------------------- CLASS ContiguousDeque --------------------------*/
79/*--------------------------------------------------------------------------*/
80
83template< class T >
85{
86
87/*--------------------------------------------------------------------------*/
88 public:
89 using value_type = T;
90 using size_type = std::size_t;
91 using difference_type = std::ptrdiff_t;
92 using reference = T &;
93 using const_reference = const T &;
94 using iterator = T *;
95 using const_iterator = const T *;
97 using reverse_iterator = std::reverse_iterator< iterator >;
99 using const_reverse_iterator = std::reverse_iterator< const_iterator >;
100
102 ContiguousDeque( void ) = default;
103
105 ContiguousDeque( std::initializer_list< T > l );
106
109
112
115
118
120 void swap( ContiguousDeque & o ) noexcept;
121
123 [[nodiscard]] bool empty( void ) const { return( f_n == 0 ); }
124
126 [[nodiscard]] std::size_t size( void ) const { return( f_n ); }
127
129 [[nodiscard]] std::size_t capacity( void ) const { return( f_c ); }
130
133 [[nodiscard]] T & operator[]( std::size_t i ) {
134 ADS_PRE( i < f_n );
135 return( f_A[ f_h + i ] );
136 }
137
140 [[nodiscard]] const T & operator[]( std::size_t i ) const {
141 ADS_PRE( i < f_n );
142 return( f_A[ f_h + i ] );
143 }
144
147 [[nodiscard]] T & front( void ) {
148 ADS_PRE( f_n > 0 );
149 return( f_A[ f_h ] );
150 }
151
154 [[nodiscard]] T & back( void ) {
155 ADS_PRE( f_n > 0 );
156 return( f_A[ f_h + f_n - 1 ] );
157 }
158
160 [[nodiscard]] T * data( void ) { return( f_A + f_h ); }
161
163 [[nodiscard]] const T * data( void ) const { return( f_A + f_h ); }
164
185 void insert( std::size_t i , const T & x );
186
205 void erase( std::size_t i );
206
208 void push_front( const T & x );
209
211 void push_back( const T & x );
212
215 void pop_front( void ) { erase( 0 ); }
216
219 void pop_back( void ) { erase( f_n - 1 ); }
220
222 void clear( void );
223
225 [[nodiscard]] bool operator==( const ContiguousDeque & o ) const {
226 return( std::equal( begin() , end() , o.begin() , o.end() ) );
227 }
228
230 [[nodiscard]] iterator begin( void ) { return( f_A + f_h ); }
232 [[nodiscard]] iterator end( void ) { return( f_A + f_h + f_n ); }
234 [[nodiscard]] const_iterator begin( void ) const { return( f_A + f_h ); }
236 [[nodiscard]] const_iterator end( void ) const { return( f_A + f_h + f_n ); }
238 [[nodiscard]] reverse_iterator rbegin( void ) {
239 return( reverse_iterator( end() ) );
240 }
241
242 [[nodiscard]] reverse_iterator rend( void ) {
243 return( reverse_iterator( begin() ) );
244 }
245
246/*--------------------------------------------------------------------------*/
247 private:
260 void rebuild( void );
261
263 [[nodiscard]] bool fits( void ) const { return( f_h + f_n <= f_c ); }
264
265 T * f_A = nullptr;
266 std::size_t f_c = 0;
267 std::size_t f_h = 0;
268 std::size_t f_n = 0;
269
270 }; // end( class ContiguousDeque )
271
272/*--------------------------------------------------------------------------*/
273
274} // end( namespace ADS )
275
276/*--------------------------------------------------------------------------*/
277/*-------------------------- IMPLEMENTATION --------------------------------*/
278/*--------------------------------------------------------------------------*/
279
280#include "ADS/sequence/impl/ContiguousDeque.tpp"
281
282/*--------------------------------------------------------------------------*/
283
284#endif /* ContiguousDeque.h included */
285
286/*--------------------------------------------------------------------------*/
287/*----------------------- End File ContiguousDeque.h -----------------------*/
288/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
ContiguousDeque(std::initializer_list< T > l)
the items of l, in order
void push_front(const T &x)
at the front, amortized
void clear(void)
removes all the items, the places kept
const T & const_reference
a constant reference
Definition ContiguousDeque.h:93
void pop_front(void)
removes the first item
Definition ContiguousDeque.h:215
bool operator==(const ContiguousDeque &o) const
the same items in the same order
Definition ContiguousDeque.h:225
const T * data(void) const
the items, contiguous and constant
Definition ContiguousDeque.h:163
reverse_iterator rend(void)
the iterator before the first item, backwards
Definition ContiguousDeque.h:242
std::reverse_iterator< iterator > reverse_iterator
the iterator on the items, backwards
Definition ContiguousDeque.h:97
T & back(void)
the last item
Definition ContiguousDeque.h:154
ContiguousDeque & operator=(ContiguousDeque o) noexcept
the items of o, by copy and swap
void swap(ContiguousDeque &o) noexcept
exchanges the vector with that of o
void erase(std::size_t i)
removes the item of rank , moving the items of the shorter side by one place, in amortized time
iterator begin(void)
the iterator on the first item
Definition ContiguousDeque.h:230
ContiguousDeque(ContiguousDeque &&o) noexcept
the vector of o, which is left empty with no places
~ContiguousDeque()
destroys the items and frees the places
reverse_iterator rbegin(void)
the iterator on the last item, backwards
Definition ContiguousDeque.h:238
std::size_t capacity(void) const
the number of places
Definition ContiguousDeque.h:129
T * iterator
the iterator on the items
Definition ContiguousDeque.h:94
std::size_t size_type
the type of the sizes
Definition ContiguousDeque.h:90
ContiguousDeque(void)=default
the empty deque, with no places
T & reference
a reference to an item
Definition ContiguousDeque.h:92
const_iterator begin(void) const
the constant iterator on the first item
Definition ContiguousDeque.h:234
void push_back(const T &x)
at the back, amortized
void pop_back(void)
removes the last item
Definition ContiguousDeque.h:219
const_iterator end(void) const
the constant iterator past the last item
Definition ContiguousDeque.h:236
std::ptrdiff_t difference_type
the type of the distances
Definition ContiguousDeque.h:91
const T * const_iterator
on the constant items
Definition ContiguousDeque.h:95
T * data(void)
the items, contiguous
Definition ContiguousDeque.h:160
std::size_t size(void) const
the number of items
Definition ContiguousDeque.h:126
ContiguousDeque(const ContiguousDeque &o)
a copy of o
T & front(void)
the first item
Definition ContiguousDeque.h:147
T & operator[](std::size_t i)
the item of rank ,
Definition ContiguousDeque.h:133
bool empty(void) const
true if there is no item
Definition ContiguousDeque.h:123
const T & operator[](std::size_t i) const
the item of rank , constant
Definition ContiguousDeque.h:140
iterator end(void)
the iterator past the last item
Definition ContiguousDeque.h:232
std::reverse_iterator< const_iterator > const_reverse_iterator
the iterator on the constant items, backwards
Definition ContiguousDeque.h:99
T value_type
the type of the items
Definition ContiguousDeque.h:89
void insert(std::size_t i, const T &x)
puts at the rank , moving the items of the shorter side by one place, in amortized time
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
Definition ADT.h:96