ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ArraySequence.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File ArraySequence.h --------------------------*/
3/*--------------------------------------------------------------------------*/
75/*--------------------------------------------------------------------------*/
76/*----------------------------- DEFINITIONS --------------------------------*/
77/*--------------------------------------------------------------------------*/
78
79#ifndef __ADS_ArraySequence
80 #define __ADS_ArraySequence
81/* self-identification: #endif constant */
82
83/*--------------------------------------------------------------------------*/
84/*------------------------------ INCLUDES ----------------------------------*/
85/*--------------------------------------------------------------------------*/
86
87#include "ADS/Contract.h"
88
89#include <algorithm>
90#include <cstddef>
91#include <initializer_list>
92#include <iterator>
93#include <memory>
94#include <utility>
95
96/*--------------------------------------------------------------------------*/
97/*--------------------------- NAMESPACE ------------------------------------*/
98/*--------------------------------------------------------------------------*/
99
100namespace ADS
101{
102
103/*--------------------------------------------------------------------------*/
104/*------------------------- CLASS ArraySequence ----------------------------*/
105/*--------------------------------------------------------------------------*/
106
108template< class T >
110{
111
112/*--------------------------------------------------------------------------*/
113 public:
114 using Item = T;
115 using Pos = long;
116
119
121 ArraySequence( std::initializer_list< T > items );
122
124 template< std::input_iterator I , std::sentinel_for< I > S >
125 ArraySequence( I first , S last );
126
128 ArraySequence( const ArraySequence & other );
129
131 ArraySequence( ArraySequence && other ) noexcept;
132
135
136/*--------------------------------------------------------------------------*/
137/*-------------------------- THE SEQUENCE ----------------------------------*/
138/*--------------------------------------------------------------------------*/
139
141 [[nodiscard]] bool empty( void ) const;
142
144 [[nodiscard]] bool finished( Pos p ) const;
145
147 [[nodiscard]] Pos head( void ) const;
148
150 [[nodiscard]] Pos tail( void ) const;
151
154 [[nodiscard]] Pos next( Pos p ) const;
155
158 [[nodiscard]] Pos prev( Pos p ) const;
159
180 Pos insert( Pos p , const T & v );
181
203 template< std::input_iterator I , std::sentinel_for< I > S >
204 Pos insert( Pos p , I first , S last );
205
226
229 [[nodiscard]] const T & read( Pos p ) const;
230
234 void write( Pos p , const T & v );
235
236/*--------------------------------------------------------------------------*/
237/*---------------------------- BEYOND THE BOOK -----------------------------*/
238/*--------------------------------------------------------------------------*/
239
241 [[nodiscard]] Pos size( void ) const;
242
245 [[nodiscard]] Pos capacity( void ) const;
246
248 [[nodiscard]] bool operator==( const ArraySequence & o ) const;
249
250/*------------------------- THE STANDARD INTERFACE -------------------------*/
251
252 // the items are contiguous, so that the iterators are pointers and every
253 // algorithm of the standard library and of Boost applies; a change made
254 // through them bypasses the audits of write()
255
257 using value_type = T;
259 using size_type = std::size_t;
261 using difference_type = std::ptrdiff_t;
263 using reference = T &;
265 using const_reference = const T &;
267 using pointer = T *;
269 using const_pointer = const T *;
271 using iterator = T *;
273 using const_iterator = const T *;
275 using reverse_iterator = std::reverse_iterator< iterator >;
277 using const_reverse_iterator = std::reverse_iterator< const_iterator >;
278
280 [[nodiscard]] iterator begin( void ) { return( f_A.get() ); }
282 [[nodiscard]] iterator end( void ) { return( f_A.get() + f_size ); }
284 [[nodiscard]] const_iterator begin( void ) const { return( f_A.get() ); }
286 [[nodiscard]] const_iterator end( void ) const {
287 return( f_A.get() + f_size );
288 }
289
290 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
292 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
294 [[nodiscard]] reverse_iterator rbegin( void ) {
295 return( reverse_iterator( end() ) );
296 }
297
298 [[nodiscard]] reverse_iterator rend( void ) {
299 return( reverse_iterator( begin() ) );
300 }
301
302 [[nodiscard]] const_reverse_iterator rbegin( void ) const {
303 return( const_reverse_iterator( end() ) );
304 }
305
306 [[nodiscard]] const_reverse_iterator rend( void ) const {
307 return( const_reverse_iterator( begin() ) );
308 }
309
311 [[nodiscard]] T * data( void ) { return( f_A.get() ); }
313 [[nodiscard]] const T * data( void ) const { return( f_A.get() ); }
314
317 [[nodiscard]] T & operator[]( Pos i );
318
321 [[nodiscard]] const T & operator[]( Pos i ) const;
322
324 void push_back( const T & v );
325
326/*--------------------------------------------------------------------------*/
327 private:
329 [[nodiscard]] bool fits( void ) const;
330
333 [[nodiscard]] bool same( const ArraySequence & o , Pos i , Pos j ,
334 Pos k ) const;
335
337 void resize( Pos c );
338
339 std::unique_ptr< T[] > f_A;
340 Pos f_size;
341 Pos f_capacity;
342
343 }; // end( class ArraySequence )
344
345/*--------------------------------------------------------------------------*/
346
347} // end( namespace ADS )
348
349/*--------------------------------------------------------------------------*/
350/*-------------------------- IMPLEMENTATION --------------------------------*/
351/*--------------------------------------------------------------------------*/
352
353#include "ADS/sequence/impl/ArraySequence.tpp"
354
355/*--------------------------------------------------------------------------*/
356
357#endif /* ArraySequence.h included */
358
359/*--------------------------------------------------------------------------*/
360/*------------------------ End File ArraySequence.h ------------------------*/
361/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
const_iterator end(void) const
the constant iterator past the last item
Definition ArraySequence.h:286
T * pointer
a pointer to an item
Definition ArraySequence.h:267
Pos capacity(void) const
the size of the vector, which the analysis of the doubling is about
Pos tail(void) const
the position of the last item,
void write(Pos p, const T &v)
writes at the index
const T & read(Pos p) const
the item at the index
void push_back(const T &v)
at the end, for std::back_inserter
ArraySequence(void)
the empty sequence, with no vector
T & operator[](Pos i)
the item at the index
std::reverse_iterator< const_iterator > const_reverse_iterator
the iterator on the constant items, backwards
Definition ArraySequence.h:277
reverse_iterator rend(void)
the iterator before the first item, backwards
Definition ArraySequence.h:298
const_reverse_iterator rbegin(void) const
the constant iterator on the last item, backwards
Definition ArraySequence.h:302
T * data(void)
the vector of the items
Definition ArraySequence.h:311
bool operator==(const ArraySequence &o) const
the same items in the same order
ArraySequence(ArraySequence &&other) noexcept
the items of other, which is left empty
iterator end(void)
the iterator past the last item
Definition ArraySequence.h:282
reverse_iterator rbegin(void)
the iterator on the last item, backwards
Definition ArraySequence.h:294
ArraySequence(std::initializer_list< T > items)
the sequence of the given items, in their order
const T & operator[](Pos i) const
the item at the index , constant
Pos insert(Pos p, I first, S last)
puts the items of [ first , last ) at the indices , in their order, the items from the index moving...
const_iterator cend(void) const
the constant iterator past the last item
Definition ArraySequence.h:292
long Pos
a position is an index, from -1 to
Definition ArraySequence.h:115
T Item
the type of the items
Definition ArraySequence.h:114
ArraySequence & operator=(ArraySequence other) noexcept
the items of other, by copy and swap
ArraySequence(I first, S last)
the sequence of the items of [ first , last ), in their order
bool finished(Pos p) const
true if is -1 or
T * iterator
the iterator on the items
Definition ArraySequence.h:271
iterator begin(void)
the iterator on the first item
Definition ArraySequence.h:280
Pos next(Pos p) const
the position
const_iterator cbegin(void) const
the constant iterator on the first item
Definition ArraySequence.h:290
Pos remove(Pos p)
removes the item at the index , the following ones moving one place back; a vector left a quarter ful...
const T * const_pointer
a constant pointer to an item
Definition ArraySequence.h:269
const T & const_reference
a constant reference to an item
Definition ArraySequence.h:265
Pos prev(Pos p) const
the position
ArraySequence(const ArraySequence &other)
a copy of other
const_iterator begin(void) const
the constant iterator on the first item
Definition ArraySequence.h:284
bool empty(void) const
true if there is no item, i.e.,
Pos size(void) const
the number of items,
Pos head(void) const
the position of the first item, 0
Pos insert(Pos p, const T &v)
puts at the index , the items from the index moving one place on, which takes time,...
const_reverse_iterator rend(void) const
the constant iterator before the first item, backwards
Definition ArraySequence.h:306
T & reference
a reference to an item
Definition ArraySequence.h:263
std::size_t size_type
the type of the sizes
Definition ArraySequence.h:259
std::ptrdiff_t difference_type
the type of the distances
Definition ArraySequence.h:261
T value_type
the type of the items
Definition ArraySequence.h:257
const T * data(void) const
the vector of the items, constant
Definition ArraySequence.h:313
const T * const_iterator
the iterator on the constant items
Definition ArraySequence.h:273
std::reverse_iterator< iterator > reverse_iterator
the iterator on the items, backwards
Definition ArraySequence.h:275
Definition ADT.h:96