ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
TieredVector.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File TieredVector.h --------------------------*/
3/*--------------------------------------------------------------------------*/
70/*--------------------------------------------------------------------------*/
71/*----------------------------- DEFINITIONS --------------------------------*/
72/*--------------------------------------------------------------------------*/
73
74#ifndef __ADS_TieredVector
75 #define __ADS_TieredVector
76/* self-identification: #endif constant */
77
78/*--------------------------------------------------------------------------*/
79/*------------------------------ INCLUDES ----------------------------------*/
80/*--------------------------------------------------------------------------*/
81
82#include "ADS/Contract.h"
84
85#include <bit>
86#include <cstddef>
87#include <iterator>
88#include <memory>
89#include <utility>
90#include <vector>
91
92/*--------------------------------------------------------------------------*/
93/*--------------------------- NAMESPACE ------------------------------------*/
94/*--------------------------------------------------------------------------*/
95
96namespace ADS
97{
98
99/*--------------------------------------------------------------------------*/
100/*-------------------------- CLASS TieredVector ----------------------------*/
101/*--------------------------------------------------------------------------*/
102
105template< class T , class A = std::allocator< T > >
107{
108
109/*--------------------------------------------------------------------------*/
110 public:
112 using value_type = T;
114 using size_type = std::size_t;
116 using difference_type = std::ptrdiff_t;
118 using reference = T &;
120 using const_reference = const T &;
121
123 TieredVector( void ) = default;
124
126 explicit TieredVector( const A & alloc ) : f_blocks( alloc ) { }
127
129 using allocator_type = A;
131 [[nodiscard]] allocator_type get_allocator( void ) const {
132 return( allocator_type( f_blocks.get_allocator() ) );
133 }
134
136 [[nodiscard]] bool empty( void ) const { return( f_n == 0 ); }
137
139 [[nodiscard]] std::size_t size( void ) const { return( f_n ); }
140
142 [[nodiscard]] std::size_t block( void ) const { return( f_b ); }
143
145 [[nodiscard]] std::size_t capacity( void ) const {
146 return( f_blocks.size() * f_b );
147 }
148
154 [[nodiscard]] T & operator[]( std::size_t i ) {
155 ADS_PRE( i < f_n );
156 const std::size_t j = f_g + i;
157 Block & k = f_blocks[ j >> f_lg ];
158 return( k.a[ ( k.off + j ) & ( f_b - 1 ) ] );
159 }
160
162 [[nodiscard]] const T & operator[]( std::size_t i ) const {
163 ADS_PRE( i < f_n );
164 const std::size_t j = f_g + i;
165 const Block & k = f_blocks[ j >> f_lg ];
166 return( k.a[ ( k.off + j ) & ( f_b - 1 ) ] );
167 }
168
171 [[nodiscard]] const T & front( void ) const { return( ( *this )[ 0 ] ); }
172
175 [[nodiscard]] const T & back( void ) const {
176 return( ( *this )[ f_n - 1 ] );
177 }
178
210 void insert( std::size_t i , const T & x );
211
238 void erase( std::size_t i );
239
241 void push_back( const T & x ) { insert( f_n , x ); }
242
245 void pop_back( void ) { erase( f_n - 1 ); }
246
248 void push_front( const T & x ) { insert( 0 , x ); }
249
252 void pop_front( void ) { erase( 0 ); }
253
255 void clear( void );
256
257/*------------------------- THE STANDARD INTERFACE -------------------------*/
258
260 template< bool Const >
262 {
263 using Owner = std::conditional_t< Const , const TieredVector ,
264 TieredVector >;
265
266 public:
268 using iterator_category = std::random_access_iterator_tag;
270 using value_type = T;
272 using difference_type = std::ptrdiff_t;
274 using reference = std::conditional_t< Const , const T & , T & >;
276 using pointer = std::conditional_t< Const , const T * , T * >;
277
279 Iterator( void ) = default;
280
282 Iterator( Owner * v , std::size_t i ) : f_v( v ) , f_i( i ) { }
283
285 template< bool K >
286 requires( Const && ! K )
287 Iterator( const Iterator< K > & o ) : f_v( o.f_v ) , f_i( o.f_i ) { }
288
290 reference operator*( void ) const { return( ( *f_v )[ f_i ] ); }
292 pointer operator->( void ) const { return( &**this ); }
295 return( ( *f_v )[ std::size_t( difference_type( f_i ) + k ) ] );
296 }
297
299 ++f_i;
300 return( *this );
301 }
302
304 --f_i;
305 return( *this );
306 }
307
309 auto t = *this;
310 ++f_i;
311 return( t );
312 }
313
315 auto t = *this;
316 --f_i;
317 return( t );
318 }
319
321 f_i = std::size_t( difference_type( f_i ) + k );
322 return( *this );
323 }
324
325 Iterator & operator-=( difference_type k ) { return( *this += -k ); }
327 return( a += k );
328 }
330 return( a += k );
331 }
333 return( a -= k );
334 }
335 friend difference_type operator-( const Iterator & a , const Iterator & b ) {
336 return( difference_type( a.f_i ) - difference_type( b.f_i ) );
337 }
338
339 bool operator==( const Iterator & o ) const { return( f_i == o.f_i ); }
341 auto operator<=>( const Iterator & o ) const { return( f_i <=> o.f_i ); }
342
343 private:
344 Owner * f_v = nullptr;
345 std::size_t f_i = 0;
346
347 template< bool >
348 friend class Iterator;
349 };
350
355
357 [[nodiscard]] iterator begin( void ) { return( iterator( this , 0 ) ); }
359 [[nodiscard]] iterator end( void ) { return( iterator( this , f_n ) ); }
361 [[nodiscard]] const_iterator begin( void ) const {
362 return( const_iterator( this , 0 ) );
363 }
364
365 [[nodiscard]] const_iterator end( void ) const {
366 return( const_iterator( this , f_n ) );
367 }
368
369 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
371 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
372
378 iterator insert( const_iterator p , const T & x ) {
379 const std::size_t i = rankOf( p );
380 insert( i , x );
381 return( begin() + std::ptrdiff_t( i ) );
382 }
383
387 template< std::input_iterator I , std::sentinel_for< I > E >
388 iterator insert( const_iterator p , I first , E last ) {
389 const std::size_t i0 = rankOf( p );
390 for( std::size_t i = i0 ; first != last ; ++first )
391 insert( i++ , T( *first ) );
392 return( begin() + std::ptrdiff_t( i0 ) );
393 }
394
396 template< class... Args >
397 iterator emplace( const_iterator p , Args &&... a ) {
398 return( insert( p , T( std::forward< Args >( a )... ) ) );
399 }
400
405 const std::size_t i = rankOf( p );
406 erase( i );
407 return( begin() + std::ptrdiff_t( i ) );
408 }
409
413 const std::size_t i = rankOf( first );
414 for( std::size_t k = rankOf( last ) - i ; k > 0 ; --k )
415 erase( i );
416 return( begin() + std::ptrdiff_t( i ) );
417 }
418
419/*--------------------------------------------------------------------------*/
420 private:
422 std::size_t rankOf( const_iterator p ) const {
423 return( std::size_t( p - begin() ) );
424 }
425
429 static constexpr bool STD = detail::isStdAllocator< A >;
430
432 using Places = std::conditional_t< STD , std::unique_ptr< T[] > ,
433 detail::AllocArray< A , T > >;
434
435 struct Block
436 {
437 Places a;
438 std::size_t off = 0;
439 };
440
442 using Blocks = detail::AllocVector< A , Block >;
443
446 [[nodiscard]] Blocks blocksOf( std::size_t k , std::size_t b ) const {
447 Blocks B( f_blocks.get_allocator() );
448 B.reserve( k );
449 for( std::size_t i = 0 ; i < k ; ++i )
450 B.push_back( Block{ places( b ) } );
451 return( B );
452 }
453
455 [[nodiscard]] Places places( std::size_t b ) const {
456 if constexpr( STD )
457 return( std::make_unique< T[] >( b ) );
458 else
459 return( Places( f_blocks.get_allocator() , b ) );
460 }
461
465 void rebuild( std::size_t n );
466
491 void insertFront( std::size_t i , T && x );
492
495 void eraseFront( std::size_t i );
496
499 void eraseBack( std::size_t i );
500
503 [[nodiscard]] bool full( void ) const;
504
505 Blocks f_blocks;
506 std::size_t f_n = 0;
507 std::size_t f_g = 0;
508 std::size_t f_b = 64;
509 unsigned f_lg = 6;
510
511 }; // end( class TieredVector )
512
513/*--------------------------------------------------------------------------*/
514
515} // end( namespace ADS )
516
517/*--------------------------------------------------------------------------*/
518/*-------------------------- IMPLEMENTATION --------------------------------*/
519/*--------------------------------------------------------------------------*/
520
521#include "ADS/sequence/impl/TieredVector.tpp"
522
523/*--------------------------------------------------------------------------*/
524
525#endif /* TieredVector.h included */
526
527/*--------------------------------------------------------------------------*/
528/*------------------------ End File TieredVector.h -------------------------*/
529/*--------------------------------------------------------------------------*/
The rules of the allocator-aware containers of the standard ([container.reqmts], [allocator....
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
friend Iterator operator+(Iterator a, difference_type k)
Definition TieredVector.h:326
Iterator & operator+=(difference_type k)
items on
Definition TieredVector.h:320
friend Iterator operator+(difference_type k, Iterator a)
Definition TieredVector.h:329
T value_type
the type of the items
Definition TieredVector.h:270
std::conditional_t< Const, const T &, T & > reference
a reference to an item, constant if Const
Definition TieredVector.h:274
Iterator operator--(int)
to the previous item, returning the iterator before the step
Definition TieredVector.h:314
std::random_access_iterator_tag iterator_category
the category of the iterator, random access
Definition TieredVector.h:268
reference operator[](difference_type k) const
the item places on
Definition TieredVector.h:294
Iterator(void)=default
an iterator on no vector
std::conditional_t< Const, const T *, T * > pointer
a pointer to an item, constant if Const
Definition TieredVector.h:276
Iterator operator++(int)
to the next item, returning the iterator before the step
Definition TieredVector.h:308
pointer operator->(void) const
the address of the item
Definition TieredVector.h:292
std::ptrdiff_t difference_type
the type of the distances
Definition TieredVector.h:272
auto operator<=>(const Iterator &o) const
the order of the indices
Definition TieredVector.h:341
reference operator*(void) const
the item
Definition TieredVector.h:290
Iterator(Owner *v, std::size_t i)
on the item of index of the vector
Definition TieredVector.h:282
friend difference_type operator-(const Iterator &a, const Iterator &b)
Definition TieredVector.h:335
Iterator & operator--(void)
to the previous item
Definition TieredVector.h:303
bool operator==(const Iterator &o) const
equal on the same index
Definition TieredVector.h:339
friend class Iterator
Definition TieredVector.h:348
Iterator(const Iterator< K > &o)
the const iterator from the other one
Definition TieredVector.h:287
Iterator & operator-=(difference_type k)
items back
Definition TieredVector.h:325
friend Iterator operator-(Iterator a, difference_type k)
Definition TieredVector.h:332
Iterator & operator++(void)
to the next item
Definition TieredVector.h:298
T value_type
the type of the items
Definition TieredVector.h:112
Iterator< true > const_iterator
the iterator on the constant items
Definition TieredVector.h:354
const_iterator end(void) const
the constant iterator past the last item
Definition TieredVector.h:365
TieredVector(const A &alloc)
the empty sequence, its blocks from alloc
Definition TieredVector.h:126
T & reference
a reference to an item
Definition TieredVector.h:118
std::size_t capacity(void) const
the places of the blocks,
Definition TieredVector.h:145
iterator begin(void)
the iterator on the first item
Definition TieredVector.h:357
iterator erase(const_iterator p)
the item of removed, i.e., erase( i ) with its rank; an iterator on the item after it
Definition TieredVector.h:404
iterator insert(const_iterator p, const T &x)
before the item of , i.e., insert( i , x ) with the rank of : the members by iterator of a SequenceC...
Definition TieredVector.h:378
T & operator[](std::size_t i)
the item at the index , at the place of the block , the offset of the block
Definition TieredVector.h:154
void insert(std::size_t i, const T &x)
at the index , the items from one place on, or those before one place back if , in amortized time
std::ptrdiff_t difference_type
the type of the distances
Definition TieredVector.h:116
void pop_back(void)
removes the last item, erase( n - 1 )
Definition TieredVector.h:245
bool empty(void) const
true if there is no item
Definition TieredVector.h:136
void erase(std::size_t i)
removes the item at the index , the items after it one place back, or those before it one place on if...
const_iterator begin(void) const
the constant iterator on the first item
Definition TieredVector.h:361
void push_back(const T &x)
at the end, insert( n , x )
Definition TieredVector.h:241
iterator erase(const_iterator first, const_iterator last)
the items of removed, one erase( i ) each; an iterator on the item after them
Definition TieredVector.h:412
allocator_type get_allocator(void) const
the allocator of the blocks
Definition TieredVector.h:131
TieredVector(void)=default
the empty vector
iterator insert(const_iterator p, I first, E last)
the items of before the item of , in their order, one insert( i , x ) each; an iterator on the first...
Definition TieredVector.h:388
std::size_t size_type
the type of the sizes
Definition TieredVector.h:114
const T & operator[](std::size_t i) const
the item at the index , constant
Definition TieredVector.h:162
iterator end(void)
the iterator past the last item
Definition TieredVector.h:359
const T & front(void) const
the first item
Definition TieredVector.h:171
std::size_t size(void) const
the number of items,
Definition TieredVector.h:139
const_iterator cbegin(void) const
the constant iterator on the first item
Definition TieredVector.h:369
void pop_front(void)
removes the first item, erase( 0 ), amortized
Definition TieredVector.h:252
std::size_t block(void) const
the places of a block
Definition TieredVector.h:142
void push_front(const T &x)
at the front, insert( 0 , x ), amortized
Definition TieredVector.h:248
A allocator_type
the type of the allocator
Definition TieredVector.h:129
const T & const_reference
a constant reference to an item
Definition TieredVector.h:120
Iterator< false > iterator
the iterator on the items
Definition TieredVector.h:352
const T & back(void) const
the last item
Definition TieredVector.h:175
const_iterator cend(void) const
the constant iterator past the last item
Definition TieredVector.h:371
iterator emplace(const_iterator p, Args &&... a)
an item built from before the item of
Definition TieredVector.h:397
void clear(void)
removes the items
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
Definition ADT.h:96