ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
RootishArrayStack.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------ File RootishArrayStack.h ------------------------*/
3/*--------------------------------------------------------------------------*/
64/*--------------------------------------------------------------------------*/
65/*----------------------------- DEFINITIONS --------------------------------*/
66/*--------------------------------------------------------------------------*/
67
68#ifndef __ADS_RootishArrayStack
69 #define __ADS_RootishArrayStack
70/* self-identification: #endif constant */
71
72/*--------------------------------------------------------------------------*/
73/*------------------------------ INCLUDES ----------------------------------*/
74/*--------------------------------------------------------------------------*/
75
76#include "ADS/Contract.h"
78#include "ADS/Iterators.h"
79
80#include <cmath>
81#include <cstddef>
82#include <initializer_list>
83#include <iterator>
84#include <memory>
85#include <new>
86#include <utility>
87#include <vector>
88
89/*--------------------------------------------------------------------------*/
90/*--------------------------- NAMESPACE ------------------------------------*/
91/*--------------------------------------------------------------------------*/
92
93namespace ADS
94{
95
96/*--------------------------------------------------------------------------*/
97/*------------------------ CLASS RootishArrayStack -------------------------*/
98/*--------------------------------------------------------------------------*/
99
102template< class T , class A = std::allocator< T > >
104{
105
106/*--------------------------------------------------------------------------*/
107 public:
108 using value_type = T;
109 using size_type = std::size_t;
110 using difference_type = std::ptrdiff_t;
111 using reference = T &;
112 using const_reference = const T &;
118 using allocator_type = A;
120 [[nodiscard]] allocator_type get_allocator( void ) const {
121 return( allocator_type( f_blocks.get_allocator() ) );
122 }
123
125 RootishArrayStack( void ) = default;
126
128 explicit RootishArrayStack( const A & alloc ) : f_blocks( alloc ) { }
129
131 RootishArrayStack( std::initializer_list< T > l );
132
136
138 RootishArrayStack( const RootishArrayStack & o , const A & alloc );
139
142
145 RootishArrayStack( RootishArrayStack && o , const A & alloc );
146
149 requires detail::alwaysEqualAlloc< A >;
150
153 requires( ! detail::alwaysEqualAlloc< A > )
154 {
155 return( detail::copyAssign( *this , o ) );
156 }
157
161 requires( ! detail::alwaysEqualAlloc< A > )
162 {
163 return( detail::moveAssign( *this , std::move( o ) ) );
164 }
165
168
172 void swap( RootishArrayStack & o ) noexcept( detail::swapNoexcept< A > );
173
175 [[nodiscard]] bool empty( void ) const { return( f_n == 0 ); }
176
178 [[nodiscard]] std::size_t size( void ) const { return( f_n ); }
179
181 [[nodiscard]] std::size_t capacity( void ) const {
182 return( f_blocks.size() * ( f_blocks.size() + 1 ) / 2 );
183 }
184
186 [[nodiscard]] std::size_t blocks( void ) const { return( f_blocks.size() ); }
187
203 [[nodiscard]] static std::size_t block( std::size_t i );
204
207 [[nodiscard]] T & operator[]( std::size_t i );
208
211 [[nodiscard]] const T & operator[]( std::size_t i ) const;
212
215 [[nodiscard]] const T & front( void ) const;
216
219 [[nodiscard]] const T & back( void ) const;
220
238 void insert( std::size_t i , const T & x );
239
256 void erase( std::size_t i );
257
263 iterator insert( const_iterator p , const T & x ) {
264 const std::size_t i = rankOf( p );
265 insert( i , x );
266 return( begin() + std::ptrdiff_t( i ) );
267 }
268
272 template< std::input_iterator I , std::sentinel_for< I > E >
273 iterator insert( const_iterator p , I first , E last ) {
274 const std::size_t i0 = rankOf( p );
275 for( std::size_t i = i0 ; first != last ; ++first )
276 insert( i++ , T( *first ) );
277 return( begin() + std::ptrdiff_t( i0 ) );
278 }
279
281 template< class... Args >
282 iterator emplace( const_iterator p , Args &&... a ) {
283 return( insert( p , T( std::forward< Args >( a )... ) ) );
284 }
285
290 const std::size_t i = rankOf( p );
291 erase( i );
292 return( begin() + std::ptrdiff_t( i ) );
293 }
294
298 const std::size_t i = rankOf( first );
299 for( std::size_t k = rankOf( last ) - i ; k > 0 ; --k )
300 erase( i );
301 return( begin() + std::ptrdiff_t( i ) );
302 }
303
305 void push_back( const T & x ) { emplace_back( x ); }
306
308 void push_back( T && x ) { emplace_back( std::move( x ) ); }
309
311 template< class... Args >
312 T & emplace_back( Args &&... args );
313
316 void pop_back( void ) { erase( f_n - 1 ); }
317
319 void clear( void );
320
322 [[nodiscard]] bool operator==( const RootishArrayStack & o ) const;
323
325 [[nodiscard]] iterator begin( void ) { return( iterator( this , 0 ) ); }
327 [[nodiscard]] iterator end( void ) { return( iterator( this , f_n ) ); }
329 [[nodiscard]] const_iterator begin( void ) const {
330 return( const_iterator( this , 0 ) );
331 }
332
333 [[nodiscard]] const_iterator end( void ) const {
334 return( const_iterator( this , f_n ) );
335 }
336
337 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
339 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
340
341/*--------------------------------------------------------------------------*/
342 private:
344 std::size_t rankOf( const_iterator p ) const {
345 return( std::size_t( p - begin() ) );
346 }
347
349 [[nodiscard]] T * place( std::size_t i ) const;
350
352 void grow( void );
353
355 void shrink( void );
356
358 [[nodiscard]] bool fits( void ) const;
359
361 using Alloc = detail::RebindAlloc< A , T >;
362 using Traits = std::allocator_traits< Alloc >;
363
365 detail::AllocVector< A , T * > f_blocks;
366 std::size_t f_n = 0;
367
368 }; // end( class RootishArrayStack )
369
370/*--------------------------------------------------------------------------*/
371
372} // end( namespace ADS )
373
374/*--------------------------------------------------------------------------*/
375/*-------------------------- IMPLEMENTATION --------------------------------*/
376/*--------------------------------------------------------------------------*/
377
378#include "ADS/sequence/impl/RootishArrayStack.tpp"
379
380/*--------------------------------------------------------------------------*/
381
382#endif /* RootishArrayStack.h included */
383
384/*--------------------------------------------------------------------------*/
385/*---------------------- End File RootishArrayStack.h ----------------------*/
386/*--------------------------------------------------------------------------*/
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,...
The iterators that the containers of the library have in common, beyond the book: they give a contain...
the random-access iterator on the items of a container C read by rank, c[ i ] the item ,...
Definition Iterators.h:392
const_iterator end(void) const
the constant iterator past the last item
Definition RootishArrayStack.h:333
void pop_back(void)
removes the last item
Definition RootishArrayStack.h:316
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 RootishArrayStack.h:263
void clear(void)
removes all the items and frees the blocks
RootishArrayStack(void)=default
the empty list, with no block
T & emplace_back(Args &&... args)
the item built from args at the end, amortized
iterator end(void)
the iterator past the last item
Definition RootishArrayStack.h:327
~RootishArrayStack()
destroys the items and frees the blocks
bool operator==(const RootishArrayStack &o) const
the same items in the same order
RootishArrayStack(RootishArrayStack &&o, const A &alloc)
the items of o, which is left empty, on alloc: its blocks if the allocators are equal,...
RootishArrayStack(const A &alloc)
the empty sequence, its blocks from alloc
Definition RootishArrayStack.h:128
bool empty(void) const
true if there is no item
Definition RootishArrayStack.h:175
const_iterator cbegin(void) const
the constant iterator on the first item
Definition RootishArrayStack.h:337
std::size_t size(void) const
the number of items
Definition RootishArrayStack.h:178
const_iterator cend(void) const
the constant iterator past the last item
Definition RootishArrayStack.h:339
A allocator_type
the type of the allocator
Definition RootishArrayStack.h:118
RootishArrayStack(const RootishArrayStack &o, const A &alloc)
a copy of o, on alloc
RootishArrayStack(std::initializer_list< T > l)
the items of l, in order
iterator emplace(const_iterator p, Args &&... a)
an item built from before the item of
Definition RootishArrayStack.h:282
RankIterator< RootishArrayStack, T, true > const_iterator
the iterator on the constant items
Definition RootishArrayStack.h:116
iterator begin(void)
the iterator on the first item
Definition RootishArrayStack.h:325
const T & back(void) const
the last item
void push_back(const T &x)
at the end, insert( size() , x )
Definition RootishArrayStack.h:305
const_iterator begin(void) const
the constant iterator on the first item
Definition RootishArrayStack.h:329
const T & operator[](std::size_t i) const
the item of rank , constant
iterator erase(const_iterator p)
the item of removed, i.e., erase( i ) with its rank; an iterator on the item after it
Definition RootishArrayStack.h:289
std::size_t size_type
the type of the sizes
Definition RootishArrayStack.h:109
RootishArrayStack & operator=(RootishArrayStack o) noexcept
the items of o, by copy and swap, when the allocators are all equal
void swap(RootishArrayStack &o) noexcept(detail::swapNoexcept< A >)
exchanges the blocks with those of o, and the allocators if they propagate on a swap
static std::size_t block(std::size_t i)
the block of the rank , (1)
void insert(std::size_t i, const T &x)
puts at the rank , the items from one rank on, in amortized time
RootishArrayStack & operator=(const RootishArrayStack &o)
the copy assignment, with the allocator of o if it propagates
Definition RootishArrayStack.h:152
T value_type
the type of the items
Definition RootishArrayStack.h:108
const T & const_reference
a constant reference
Definition RootishArrayStack.h:112
T & operator[](std::size_t i)
the item of rank , in the block of (1)
iterator erase(const_iterator first, const_iterator last)
the items of removed, one erase( i ) each; an iterator on the item after them
Definition RootishArrayStack.h:297
RootishArrayStack & operator=(RootishArrayStack &&o)
the move assignment: the memory of o if its allocator propagates or is equal, else its items moved on...
Definition RootishArrayStack.h:160
void push_back(T &&x)
at the end, moved
Definition RootishArrayStack.h:308
RootishArrayStack(const RootishArrayStack &o)
a copy of o, on the allocator that select_on_container_copy_construction() gives
std::ptrdiff_t difference_type
the type of the distances
Definition RootishArrayStack.h:110
allocator_type get_allocator(void) const
the allocator of the blocks
Definition RootishArrayStack.h:120
void erase(std::size_t i)
removes the item of rank , those after it one rank back, in amortized time
std::size_t blocks(void) const
the number of blocks
Definition RootishArrayStack.h:186
T & reference
a reference to an item
Definition RootishArrayStack.h:111
std::size_t capacity(void) const
the places of the blocks,
Definition RootishArrayStack.h:181
const T & front(void) const
the first item
RankIterator< RootishArrayStack, T, false > iterator
the iterator on the items
Definition RootishArrayStack.h:114
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 RootishArrayStack.h:273
RootishArrayStack(RootishArrayStack &&o) noexcept
the blocks of o, which is left empty
Definition ADT.h:96