ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
DualArrayDeque.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------- File DualArrayDeque.h --------------------------*/
3/*--------------------------------------------------------------------------*/
57/*--------------------------------------------------------------------------*/
58/*----------------------------- DEFINITIONS --------------------------------*/
59/*--------------------------------------------------------------------------*/
60
61#ifndef __ADS_DualArrayDeque
62 #define __ADS_DualArrayDeque
63/* self-identification: #endif constant */
64
65/*--------------------------------------------------------------------------*/
66/*------------------------------ INCLUDES ----------------------------------*/
67/*--------------------------------------------------------------------------*/
68
69#include "ADS/Contract.h"
70#include "ADS/Iterators.h"
71
72#include <cstddef>
73#include <initializer_list>
74#include <iterator>
75#include <utility>
76#include <vector>
77
78/*--------------------------------------------------------------------------*/
79/*--------------------------- NAMESPACE ------------------------------------*/
80/*--------------------------------------------------------------------------*/
81
82namespace ADS
83{
84
85/*--------------------------------------------------------------------------*/
86/*-------------------------- CLASS DualArrayDeque --------------------------*/
87/*--------------------------------------------------------------------------*/
88
90template< class T , class S = std::vector< T > >
92{
93
94/*--------------------------------------------------------------------------*/
95 public:
96 using value_type = T;
97 using size_type = std::size_t;
98 using difference_type = std::ptrdiff_t;
99 using reference = T &;
100 using const_reference = const T &;
105
107 DualArrayDeque( void ) = default;
108
110 DualArrayDeque( std::initializer_list< T > l );
111
113 [[nodiscard]] bool empty( void ) const {
114 return( ( f_F.size() == 0 ) && ( f_B.size() == 0 ) );
115 }
116
118 [[nodiscard]] std::size_t size( void ) const {
119 return( f_F.size() + f_B.size() );
120 }
121
124 [[nodiscard]] T & operator[]( std::size_t i );
125
128 [[nodiscard]] const T & operator[]( std::size_t i ) const;
129
132 [[nodiscard]] const T & front( void ) const { return( ( *this )[ 0 ] ); }
133
136 [[nodiscard]] const T & back( void ) const {
137 return( ( *this )[ size() - 1 ] );
138 }
139
155 void insert( std::size_t i , const T & x );
156
172 void erase( std::size_t i );
173
179 iterator insert( const_iterator p , const T & x ) {
180 const std::size_t i = rankOf( p );
181 insert( i , x );
182 return( begin() + std::ptrdiff_t( i ) );
183 }
184
188 template< std::input_iterator I , std::sentinel_for< I > E >
189 iterator insert( const_iterator p , I first , E last ) {
190 const std::size_t i0 = rankOf( p );
191 for( std::size_t i = i0 ; first != last ; ++first )
192 insert( i++ , T( *first ) );
193 return( begin() + std::ptrdiff_t( i0 ) );
194 }
195
197 template< class... A >
198 iterator emplace( const_iterator p , A &&... a ) {
199 return( insert( p , T( std::forward< A >( a )... ) ) );
200 }
201
206 const std::size_t i = rankOf( p );
207 erase( i );
208 return( begin() + std::ptrdiff_t( i ) );
209 }
210
214 const std::size_t i = rankOf( first );
215 for( std::size_t k = rankOf( last ) - i ; k > 0 ; --k )
216 erase( i );
217 return( begin() + std::ptrdiff_t( i ) );
218 }
219
221 void push_front( const T & x ) { insert( 0 , x ); }
222
224 void push_back( const T & x ) { insert( size() , x ); }
225
228 void pop_front( void ) { erase( 0 ); }
229
232 void pop_back( void ) { erase( size() - 1 ); }
233
250 void rotate( std::size_t r );
251
253 void clear( void );
254
256 [[nodiscard]] std::size_t frontSize( void ) const { return( f_F.size() ); }
257
259 [[nodiscard]] bool operator==( const DualArrayDeque & o ) const;
260
262 [[nodiscard]] iterator begin( void ) { return( iterator( this , 0 ) ); }
264 [[nodiscard]] iterator end( void ) { return( iterator( this , size() ) ); }
266 [[nodiscard]] const_iterator begin( void ) const {
267 return( const_iterator( this , 0 ) );
268 }
269
270 [[nodiscard]] const_iterator end( void ) const {
271 return( const_iterator( this , size() ) );
272 }
273
274 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
276 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
277
278/*--------------------------------------------------------------------------*/
279 private:
281 std::size_t rankOf( const_iterator p ) const {
282 return( std::size_t( p - begin() ) );
283 }
284
286 static void put( S & s , std::size_t j , T && x );
287
289 static void take( S & s , std::size_t j );
290
316 void balance( void );
317
322 static void deal( S & big , S & small , std::size_t t );
323
325 [[nodiscard]] bool balanced( void ) const;
326
327 S f_F;
328 S f_B;
329
330 }; // end( class DualArrayDeque )
331
332/*--------------------------------------------------------------------------*/
333
334} // end( namespace ADS )
335
336/*--------------------------------------------------------------------------*/
337/*-------------------------- IMPLEMENTATION --------------------------------*/
338/*--------------------------------------------------------------------------*/
339
340#include "ADS/sequence/impl/DualArrayDeque.tpp"
341
342/*--------------------------------------------------------------------------*/
343
344#endif /* DualArrayDeque.h included */
345
346/*--------------------------------------------------------------------------*/
347/*----------------------- End File DualArrayDeque.h ------------------------*/
348/*--------------------------------------------------------------------------*/
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...
std::size_t frontSize(void) const
the number of items of the front stack
Definition DualArrayDeque.h:256
iterator erase(const_iterator first, const_iterator last)
the items of removed, one erase( i ) each; an iterator on the item after them
Definition DualArrayDeque.h:213
const_iterator cbegin(void) const
the constant iterator on the first item
Definition DualArrayDeque.h:274
void insert(std::size_t i, const T &x)
puts at the rank , in amortized time
std::size_t size(void) const
the number of items
Definition DualArrayDeque.h:118
RankIterator< DualArrayDeque, T, false > iterator
the iterator on the items
Definition DualArrayDeque.h:102
T & operator[](std::size_t i)
the item of rank
const T & front(void) const
the first item
Definition DualArrayDeque.h:132
bool empty(void) const
true if there is no item
Definition DualArrayDeque.h:113
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 DualArrayDeque.h:179
void clear(void)
removes all the items
const_iterator begin(void) const
the constant iterator on the first item
Definition DualArrayDeque.h:266
const T & back(void) const
the last item
Definition DualArrayDeque.h:136
bool operator==(const DualArrayDeque &o) const
the same items in the same order
const_iterator cend(void) const
the constant iterator past the last item
Definition DualArrayDeque.h:276
const T & const_reference
a constant reference
Definition DualArrayDeque.h:100
DualArrayDeque(void)=default
the empty deque
iterator erase(const_iterator p)
the item of removed, i.e., erase( i ) with its rank; an iterator on the item after it
Definition DualArrayDeque.h:205
iterator end(void)
the iterator past the last item
Definition DualArrayDeque.h:264
void rotate(std::size_t r)
the item of rank moved to the rank , in amortized time (exercise 2.5 of Morin's book)
RankIterator< DualArrayDeque, T, true > const_iterator
the iterator on the constant items
Definition DualArrayDeque.h:104
T value_type
the type of the items
Definition DualArrayDeque.h:96
void push_front(const T &x)
at the front, insert( 0 , x )
Definition DualArrayDeque.h:221
const_iterator end(void) const
the constant iterator past the last item
Definition DualArrayDeque.h:270
iterator begin(void)
the iterator on the first item
Definition DualArrayDeque.h:262
std::ptrdiff_t difference_type
the type of the distances
Definition DualArrayDeque.h:98
iterator emplace(const_iterator p, A &&... a)
an item built from before the item of
Definition DualArrayDeque.h:198
std::size_t size_type
the type of the sizes
Definition DualArrayDeque.h:97
void pop_back(void)
removes the last item
Definition DualArrayDeque.h:232
void erase(std::size_t i)
removes the item of rank , in amortized time
const T & operator[](std::size_t i) const
the item of rank , constant
DualArrayDeque(std::initializer_list< T > l)
the items of l, in order
void pop_front(void)
removes the first item
Definition DualArrayDeque.h:228
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 DualArrayDeque.h:189
T & reference
a reference to an item
Definition DualArrayDeque.h:99
void push_back(const T &x)
at the back, insert( size() , x )
Definition DualArrayDeque.h:224
the random-access iterator on the items of a container C read by rank, c[ i ] the item ,...
Definition Iterators.h:392
@ big
the most significant byte first
Definition Persistence.h:121
Definition ADT.h:96