ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SEList.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*----------------------------- File SEList.h ------------------------------*/
3/*--------------------------------------------------------------------------*/
69/*--------------------------------------------------------------------------*/
70/*----------------------------- DEFINITIONS --------------------------------*/
71/*--------------------------------------------------------------------------*/
72
73#ifndef __ADS_SEList
74 #define __ADS_SEList
75/* self-identification: #endif constant */
76
77/*--------------------------------------------------------------------------*/
78/*------------------------------ INCLUDES ----------------------------------*/
79/*--------------------------------------------------------------------------*/
80
81#include "ADS/Contract.h"
82
83#include <cstddef>
84#include <initializer_list>
85#include <iterator>
86#include <new>
87#include <type_traits>
88#include <utility>
89
90/*--------------------------------------------------------------------------*/
91/*--------------------------- NAMESPACE ------------------------------------*/
92/*--------------------------------------------------------------------------*/
93
94namespace ADS
95{
96
97/*--------------------------------------------------------------------------*/
98/*------------------------------ CLASS SEList ------------------------------*/
99/*--------------------------------------------------------------------------*/
100
103template< class T >
105{
106
107 struct Node;
108
109/*--------------------------------------------------------------------------*/
110 public:
111 using value_type = T;
112 using size_type = std::size_t;
113 using difference_type = std::ptrdiff_t;
114 using reference = T &;
115 using const_reference = const T &;
116
119 explicit SEList( std::size_t b = 63 );
120
122 SEList( std::initializer_list< T > l );
123
125 SEList( const SEList & o );
126
128 SEList( SEList && o ) noexcept;
129
131 SEList & operator=( SEList o ) noexcept;
132
135
137 void swap( SEList & o ) noexcept;
138
140 [[nodiscard]] bool empty( void ) const { return( f_n == 0 ); }
141
143 [[nodiscard]] std::size_t size( void ) const { return( f_n ); }
144
146 [[nodiscard]] std::size_t block( void ) const { return( f_b ); }
147
149 [[nodiscard]] std::size_t blocks( void ) const { return( f_blocks ); }
150
153 [[nodiscard]] std::size_t capacity( void ) const {
154 return( f_blocks * f_cap );
155 }
156
159 [[nodiscard]] T & operator[]( std::size_t i );
160
163 [[nodiscard]] const T & operator[]( std::size_t i ) const;
164
167 [[nodiscard]] const T & front( void ) const;
168
171 [[nodiscard]] const T & back( void ) const;
172
198 void insert( std::size_t i , const T & x );
199
227 void erase( std::size_t i );
228
230 void push_front( const T & x );
231
233 void push_back( const T & x );
234
237 void pop_front( void );
238
241 void pop_back( void );
242
244 void clear( void );
245
247 [[nodiscard]] bool operator==( const SEList & o ) const;
248
250 template< bool Const >
252 {
253 using Owner = std::conditional_t< Const , const SEList , SEList >;
254
255 public:
257 using iterator_category = std::bidirectional_iterator_tag;
258 using value_type = T;
259 using difference_type = std::ptrdiff_t;
261 using reference = std::conditional_t< Const , const T & , T & >;
263 using pointer = std::conditional_t< Const , const T * , T * >;
264
266 Iterator( void ) = default;
267
269 Iterator( Owner * l , Node * u , std::size_t j )
270 : f_l( l ) , f_u( u ) , f_j( j ) { }
271
273 template< bool K >
274 requires( Const && ! K )
276 : f_l( o.f_l ) , f_u( o.f_u ) , f_j( o.f_j ) { }
277
279 reference operator*( void ) const { return( f_l->at( f_u , f_j ) ); }
281 pointer operator->( void ) const { return( &**this ); }
284 if( ++f_j == f_u->n ) {
285 f_u = f_u->next;
286 f_j = 0;
287 }
288 return( *this );
289 }
290
292 if( f_j == 0 ) {
293 f_u = f_u->prev;
294 f_j = f_u->n;
295 }
296 --f_j;
297 return( *this );
298 }
299
301 auto t = *this;
302 ++*this;
303 return( t );
304 }
305
307 auto t = *this;
308 --*this;
309 return( t );
310 }
311
312 bool operator==( const Iterator & o ) const {
313 return( ( f_u == o.f_u ) && ( f_j == o.f_j ) );
314 }
315
316 private:
317 template< bool >
318 friend class Iterator;
319 Owner * f_l = nullptr;
320 Node * f_u = nullptr;
321 std::size_t f_j = 0;
322 }; // end( class Iterator )
323
326
328 [[nodiscard]] iterator begin( void ) {
329 return( iterator( this , f_dummy.next , 0 ) );
330 }
331
332 [[nodiscard]] iterator end( void ) {
333 return( iterator( this , &f_dummy , 0 ) );
334 }
335
336 [[nodiscard]] const_iterator begin( void ) const {
337 return( const_iterator( this , f_dummy.next , 0 ) );
338 }
339
340 [[nodiscard]] const_iterator end( void ) const {
341 return( const_iterator( this , &f_dummy , 0 ) );
342 }
343
344/*--------------------------------------------------------------------------*/
345 private:
347 struct Node
348 {
349 Node * prev;
350 Node * next;
351 std::size_t head;
352 std::size_t n;
353 };
354
356 static constexpr std::size_t OFF =
357 ( sizeof( Node ) + alignof( T ) - 1 ) / alignof( T ) * alignof( T );
359 static constexpr std::size_t ALIGN = alignof( T ) > alignof( Node )
360 ? alignof( T )
361 : alignof( Node );
362
364 T * places( Node * u ) const {
365 return( reinterpret_cast< T * >( reinterpret_cast< unsigned char * >( u ) +
366 OFF ) );
367 }
368
371 std::size_t wrap( std::size_t i ) const {
372 return( i >= f_cap ? i - f_cap : i );
373 }
374
376 T & at( Node * u , std::size_t j ) const {
377 return( places( u )[ wrap( u->head + j ) ] );
378 }
379
382 Node * locate( std::size_t i , std::size_t & j ) const;
383
385 bool middle( const Node * u ) const {
386 return( ( u->prev != &f_dummy ) && ( u->next != &f_dummy ) );
387 }
388
390 Node * newBlock( Node * p );
391
393 void dropBlock( Node * u );
394
396 void pushFront( Node * u , T && x );
397
399 void pushBack( Node * u , T && x );
400
402 T popFront( Node * u );
403
405 T popBack( Node * u );
406
409 void insert( Node * u , std::size_t j , T && x );
410
413 void erase( Node * u , std::size_t j );
414
430 void spread( Node * u );
431
448 void gather( Node * u );
449
452 [[nodiscard]] bool bounded( void ) const;
453
454 mutable Node f_dummy;
455 Node * f_spare = nullptr;
456 std::size_t f_n = 0;
457 std::size_t f_blocks = 0;
458 std::size_t f_b;
459 std::size_t f_cap;
460
461 }; // end( class SEList )
462
463/*--------------------------------------------------------------------------*/
464
465} // end( namespace ADS )
466
467/*--------------------------------------------------------------------------*/
468/*-------------------------- IMPLEMENTATION --------------------------------*/
469/*--------------------------------------------------------------------------*/
470
471#include "ADS/sequence/impl/SEList.tpp"
472
473/*--------------------------------------------------------------------------*/
474
475#endif /* SEList.h included */
476
477/*--------------------------------------------------------------------------*/
478/*--------------------------- End File SEList.h ----------------------------*/
479/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
Iterator(const Iterator< K > &o)
the constant iterator from the mutable one
Definition SEList.h:275
std::conditional_t< Const, const T &, T & > reference
a reference to an item, constant if Const
Definition SEList.h:261
Iterator operator++(int)
to the next item, returning the iterator before the step
Definition SEList.h:300
reference operator*(void) const
the item
Definition SEList.h:279
std::conditional_t< Const, const T *, T * > pointer
a pointer to an item, constant if Const
Definition SEList.h:263
T value_type
the items
Definition SEList.h:258
Iterator(Owner *l, Node *u, std::size_t j)
on the item of the block of l
Definition SEList.h:269
Iterator & operator--(void)
to the previous item
Definition SEList.h:291
pointer operator->(void) const
the address of the item
Definition SEList.h:281
Iterator & operator++(void)
to the next item, the next block after the last one of a block
Definition SEList.h:283
bool operator==(const Iterator &o) const
equal on the same item
Definition SEList.h:312
std::bidirectional_iterator_tag iterator_category
bidirectional
Definition SEList.h:257
Iterator operator--(int)
to the previous item, returning the iterator before the step
Definition SEList.h:306
std::ptrdiff_t difference_type
the distances
Definition SEList.h:259
friend class Iterator
Definition SEList.h:318
Iterator(void)=default
an iterator on no list
SEList(std::initializer_list< T > l)
the items of l, in order, with blocks of items at most
void insert(std::size_t i, const T &x)
puts at the rank , in amortized time
SEList(const SEList &o)
a copy of o, with its
void erase(std::size_t i)
removes the item of rank , in amortized time
void clear(void)
removes all the items and frees the blocks
void swap(SEList &o) noexcept
exchanges the blocks with those of o
T & reference
a reference to an item
Definition SEList.h:114
SEList(SEList &&o) noexcept
the blocks of o, which is left empty
const T & back(void) const
the last item
bool empty(void) const
true if there is no item
Definition SEList.h:140
Iterator< true > const_iterator
on the constant items
Definition SEList.h:325
SEList & operator=(SEList o) noexcept
the items of o, by copy and swap
const T & const_reference
a constant reference
Definition SEList.h:115
const_iterator begin(void) const
the constant iterator on the first item
Definition SEList.h:336
iterator begin(void)
the iterator on the first item
Definition SEList.h:328
iterator end(void)
the iterator past the last item
Definition SEList.h:332
std::size_t block(void) const
the parameter
Definition SEList.h:146
void pop_back(void)
removes the last item,
std::ptrdiff_t difference_type
the type of the distances
Definition SEList.h:113
const T & front(void) const
the first item
bool operator==(const SEList &o) const
the same items in the same order
std::size_t blocks(void) const
the number of blocks
Definition SEList.h:149
std::size_t size_type
the type of the sizes
Definition SEList.h:112
T value_type
the type of the items
Definition SEList.h:111
void pop_front(void)
removes the first item,
void push_front(const T &x)
at the front,
const_iterator end(void) const
the constant iterator past the last item
Definition SEList.h:340
~SEList()
destroys the items and frees the blocks
std::size_t capacity(void) const
the places of the blocks, each, at most
Definition SEList.h:153
SEList(std::size_t b=63)
the empty list with blocks of items at most
void push_back(const T &x)
at the back,
Iterator< false > iterator
the iterator on the items
Definition SEList.h:324
std::size_t size(void) const
the number of items
Definition SEList.h:143
const T & operator[](std::size_t i) const
the item of rank , constant
T & operator[](std::size_t i)
the item of rank ,
Definition ADT.h:96