ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
CubishArrayStack.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------ File CubishArrayStack.h -------------------------*/
3/*--------------------------------------------------------------------------*/
58/*--------------------------------------------------------------------------*/
59/*----------------------------- DEFINITIONS --------------------------------*/
60/*--------------------------------------------------------------------------*/
61
62#ifndef __ADS_CubishArrayStack
63 #define __ADS_CubishArrayStack
64/* self-identification: #endif constant */
65
66/*--------------------------------------------------------------------------*/
67/*------------------------------ INCLUDES ----------------------------------*/
68/*--------------------------------------------------------------------------*/
69
70#include "ADS/Contract.h"
71#include "ADS/Iterators.h"
72
73#include <bit>
74#include <cstddef>
75#include <initializer_list>
76#include <iterator>
77#include <memory>
78#include <utility>
79#include <vector>
80
81/*--------------------------------------------------------------------------*/
82/*--------------------------- NAMESPACE ------------------------------------*/
83/*--------------------------------------------------------------------------*/
84
85namespace ADS
86{
87
88/*--------------------------------------------------------------------------*/
89/*------------------------- CLASS CubishArrayStack -------------------------*/
90/*--------------------------------------------------------------------------*/
91
94template< class T >
96{
97
98/*--------------------------------------------------------------------------*/
99 public:
100 using value_type = T;
101 using size_type = std::size_t;
102 using difference_type = std::ptrdiff_t;
103 using reference = T &;
104 using const_reference = const T &;
109
111 CubishArrayStack( void ) = default;
112
114 CubishArrayStack( std::initializer_list< T > l );
115
118
121
124
126 void swap( CubishArrayStack & o ) noexcept;
127
129 [[nodiscard]] bool empty( void ) const { return( f_n == 0 ); }
130
132 [[nodiscard]] std::size_t size( void ) const { return( f_n ); }
133
135 [[nodiscard]] std::size_t block( void ) const { return( f_b ); }
136
138 [[nodiscard]] std::size_t capacity( void ) const;
139
142 [[nodiscard]] T & operator[]( std::size_t i ) {
143 ADS_PRE( i < f_n );
144 return( at( f_mids[ i >> ( 2 * f_lg ) ] , i & ( f_b * f_b - 1 ) ) );
145 }
146
149 [[nodiscard]] const T & operator[]( std::size_t i ) const {
150 ADS_PRE( i < f_n );
151 return( at( f_mids[ i >> ( 2 * f_lg ) ] , i & ( f_b * f_b - 1 ) ) );
152 }
153
176 void insert( std::size_t i , const T & x );
177
197 void erase( std::size_t i );
198
204 iterator insert( const_iterator p , const T & x ) {
205 const std::size_t i = rankOf( p );
206 insert( i , x );
207 return( begin() + std::ptrdiff_t( i ) );
208 }
209
213 template< std::input_iterator I , std::sentinel_for< I > E >
214 iterator insert( const_iterator p , I first , E last ) {
215 const std::size_t i0 = rankOf( p );
216 for( std::size_t i = i0 ; first != last ; ++first )
217 insert( i++ , T( *first ) );
218 return( begin() + std::ptrdiff_t( i0 ) );
219 }
220
222 template< class... A >
223 iterator emplace( const_iterator p , A &&... a ) {
224 return( insert( p , T( std::forward< A >( a )... ) ) );
225 }
226
231 const std::size_t i = rankOf( p );
232 erase( i );
233 return( begin() + std::ptrdiff_t( i ) );
234 }
235
239 const std::size_t i = rankOf( first );
240 for( std::size_t k = rankOf( last ) - i ; k > 0 ; --k )
241 erase( i );
242 return( begin() + std::ptrdiff_t( i ) );
243 }
244
246 void push_back( const T & x ) { insert( f_n , x ); }
247
250 void pop_back( void ) { erase( f_n - 1 ); }
251
253 void clear( void );
254
256 [[nodiscard]] bool operator==( const CubishArrayStack & o ) const;
257
259 [[nodiscard]] iterator begin( void ) { return( iterator( this , 0 ) ); }
261 [[nodiscard]] iterator end( void ) { return( iterator( this , f_n ) ); }
263 [[nodiscard]] const_iterator begin( void ) const {
264 return( const_iterator( this , 0 ) );
265 }
266
267 [[nodiscard]] const_iterator end( void ) const {
268 return( const_iterator( this , f_n ) );
269 }
270
271 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
273 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
274
275/*--------------------------------------------------------------------------*/
276 private:
278 std::size_t rankOf( const_iterator p ) const {
279 return( std::size_t( p - begin() ) );
280 }
281
283 struct Leaf
284 {
285 std::unique_ptr< T[] > a;
286 std::size_t off = 0;
287 };
288
290 struct Mid
291 {
292 std::unique_ptr< Leaf[] > L;
293 std::size_t head = 0;
294 std::size_t nl = 0;
295 std::size_t g = 0;
296 std::size_t n = 0;
297 };
298
300 Leaf & leaf( const Mid & M , std::size_t j ) const {
301 return( M.L[ ( M.head + j ) & ( 2 * f_b - 1 ) ] );
302 }
303
305 T & at( const Mid & M , std::size_t r ) const {
306 const std::size_t v = M.g + r;
307 const Leaf & f = leaf( M , v >> f_lg );
308 return( f.a[ ( f.off + v ) & ( f_b - 1 ) ] );
309 }
310
312 [[nodiscard]] Mid newMid( void ) const;
313
315 std::unique_ptr< T[] > newLeaf( void );
316
318 void freeLeaf( std::unique_ptr< T[] > a );
319
321 void reset( Mid & M );
322
324 void pushFront( Mid & M , T && x );
325
327 void pushBack( Mid & M , T && x );
328
330 T popFront( Mid & M );
331
333 T popBack( Mid & M );
334
338 void insert( Mid & M , std::size_t r , T && x );
339
341 void erase( Mid & M , std::size_t r );
342
345 void rebuild( std::size_t n );
346
348 [[nodiscard]] bool full( void ) const;
349
350 std::vector< Mid > f_mids;
351 std::vector< std::unique_ptr< T[] > > f_spare;
352 std::size_t f_n = 0;
353 std::size_t f_b = 16;
354 unsigned f_lg = 4;
355
356 }; // end( class CubishArrayStack )
357
358/*--------------------------------------------------------------------------*/
359
360} // end( namespace ADS )
361
362/*--------------------------------------------------------------------------*/
363/*-------------------------- IMPLEMENTATION --------------------------------*/
364/*--------------------------------------------------------------------------*/
365
366#include "ADS/sequence/impl/CubishArrayStack.tpp"
367
368/*--------------------------------------------------------------------------*/
369
370#endif /* CubishArrayStack.h included */
371
372/*--------------------------------------------------------------------------*/
373/*---------------------- End File CubishArrayStack.h -----------------------*/
374/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The iterators that the containers of the library have in common, beyond the book: they give a contain...
CubishArrayStack & operator=(CubishArrayStack o) noexcept
the items of o, by copy and swap
T value_type
the type of the items
Definition CubishArrayStack.h:100
iterator emplace(const_iterator p, A &&... a)
an item built from before the item of
Definition CubishArrayStack.h:223
std::size_t size_type
the type of the sizes
Definition CubishArrayStack.h:101
void pop_back(void)
removes the last item
Definition CubishArrayStack.h:250
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 CubishArrayStack.h:214
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 CubishArrayStack.h:204
bool operator==(const CubishArrayStack &o) const
the same items in the same order
iterator end(void)
the iterator past the last item
Definition CubishArrayStack.h:261
const_iterator cend(void) const
the constant iterator past the last item
Definition CubishArrayStack.h:273
const_iterator end(void) const
the constant iterator past the last item
Definition CubishArrayStack.h:267
void erase(std::size_t i)
removes the item of rank , in amortized time
iterator begin(void)
the iterator on the first item
Definition CubishArrayStack.h:259
const_iterator begin(void) const
the constant iterator on the first item
Definition CubishArrayStack.h:263
std::size_t size(void) const
the number of items
Definition CubishArrayStack.h:132
void swap(CubishArrayStack &o) noexcept
exchanges the blocks with those of o
const T & operator[](std::size_t i) const
the item of rank , constant
Definition CubishArrayStack.h:149
T & operator[](std::size_t i)
the item of rank
Definition CubishArrayStack.h:142
CubishArrayStack(std::initializer_list< T > l)
the items of l, in order
RankIterator< CubishArrayStack, T, true > const_iterator
the iterator on the constant items
Definition CubishArrayStack.h:108
iterator erase(const_iterator p)
the item of removed, i.e., erase( i ) with its rank; an iterator on the item after it
Definition CubishArrayStack.h:230
CubishArrayStack(void)=default
the empty list
const_iterator cbegin(void) const
the constant iterator on the first item
Definition CubishArrayStack.h:271
CubishArrayStack(CubishArrayStack &&o) noexcept
the blocks of o, which is left empty
void clear(void)
removes all the items and frees the leaves
bool empty(void) const
true if there is no item
Definition CubishArrayStack.h:129
std::ptrdiff_t difference_type
the type of the distances
Definition CubishArrayStack.h:102
const T & const_reference
a constant reference
Definition CubishArrayStack.h:104
CubishArrayStack(const CubishArrayStack &o)
a copy of o
void push_back(const T &x)
at the end, amortized
Definition CubishArrayStack.h:246
std::size_t block(void) const
the places of a leaf
Definition CubishArrayStack.h:135
std::size_t capacity(void) const
the places of the leaves, those kept for later included
iterator erase(const_iterator first, const_iterator last)
the items of removed, one erase( i ) each; an iterator on the item after them
Definition CubishArrayStack.h:238
void insert(std::size_t i, const T &x)
puts at the rank , in amortized time
T & reference
a reference to an item
Definition CubishArrayStack.h:103
RankIterator< CubishArrayStack, T, false > iterator
the iterator on the items
Definition CubishArrayStack.h:106
the random-access iterator on the items of a container C read by rank, c[ i ] the item ,...
Definition Iterators.h:392
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
Definition ADT.h:96