ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Treque.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*----------------------------- File Treque.h ------------------------------*/
3/*--------------------------------------------------------------------------*/
34/*--------------------------------------------------------------------------*/
35/*----------------------------- DEFINITIONS --------------------------------*/
36/*--------------------------------------------------------------------------*/
37
38#ifndef __ADS_Treque
39 #define __ADS_Treque
40/* self-identification: #endif constant */
41
42/*--------------------------------------------------------------------------*/
43/*------------------------------ INCLUDES ----------------------------------*/
44/*--------------------------------------------------------------------------*/
45
46#include "ADS/Contract.h"
47#include "ADS/Iterators.h"
48#include "ADS/queue/RingDeque.h"
49
50#include <cstddef>
51#include <initializer_list>
52#include <iterator>
53#include <utility>
54
55/*--------------------------------------------------------------------------*/
56/*--------------------------- NAMESPACE ------------------------------------*/
57/*--------------------------------------------------------------------------*/
58
59namespace ADS
60{
61
62/*--------------------------------------------------------------------------*/
63/*------------------------------ CLASS Treque ------------------------------*/
64/*--------------------------------------------------------------------------*/
65
68template< class T >
69class Treque
70{
71
72/*--------------------------------------------------------------------------*/
73 public:
74 using value_type = T;
75 using size_type = std::size_t;
76 using difference_type = std::ptrdiff_t;
77 using reference = T &;
78 using const_reference = const T &;
83
85 Treque( void ) = default;
86
88 Treque( std::initializer_list< T > l );
89
91 [[nodiscard]] bool empty( void ) const { return( size() == 0 ); }
92
94 [[nodiscard]] std::size_t size( void ) const {
95 return( f_L.size() + f_R.size() );
96 }
97
100 [[nodiscard]] T & operator[]( std::size_t i ) {
101 ADS_PRE( i < size() );
102 return( i < f_L.size() ? f_L[ i ] : f_R[ i - f_L.size() ] );
103 }
104
107 [[nodiscard]] const T & operator[]( std::size_t i ) const {
108 ADS_PRE( i < size() );
109 return( i < f_L.size() ? f_L[ i ] : f_R[ i - f_L.size() ] );
110 }
111
114 [[nodiscard]] const T & front( void ) const { return( ( *this )[ 0 ] ); }
115
118 [[nodiscard]] const T & back( void ) const {
119 return( ( *this )[ size() - 1 ] );
120 }
121
137 void insert( std::size_t i , const T & x );
138
153 void erase( std::size_t i );
154
160 iterator insert( const_iterator p , const T & x ) {
161 const std::size_t i = rankOf( p );
162 insert( i , x );
163 return( begin() + std::ptrdiff_t( i ) );
164 }
165
169 template< std::input_iterator I , std::sentinel_for< I > E >
170 iterator insert( const_iterator p , I first , E last ) {
171 const std::size_t i0 = rankOf( p );
172 for( std::size_t i = i0 ; first != last ; ++first )
173 insert( i++ , T( *first ) );
174 return( begin() + std::ptrdiff_t( i0 ) );
175 }
176
178 template< class... A >
179 iterator emplace( const_iterator p , A &&... a ) {
180 return( insert( p , T( std::forward< A >( a )... ) ) );
181 }
182
187 const std::size_t i = rankOf( p );
188 erase( i );
189 return( begin() + std::ptrdiff_t( i ) );
190 }
191
195 const std::size_t i = rankOf( first );
196 for( std::size_t k = rankOf( last ) - i ; k > 0 ; --k )
197 erase( i );
198 return( begin() + std::ptrdiff_t( i ) );
199 }
200
202 void push_front( const T & x ) { insert( 0 , x ); }
203
205 void push_back( const T & x ) { insert( size() , x ); }
206
209 void pop_front( void ) { erase( 0 ); }
210
213 void pop_back( void ) { erase( size() - 1 ); }
214
216 void clear( void ) {
217 f_L.clear();
218 f_R.clear();
219 }
220
222 [[nodiscard]] bool operator==( const Treque & o ) const;
223
225 [[nodiscard]] iterator begin( void ) { return( iterator( this , 0 ) ); }
227 [[nodiscard]] iterator end( void ) { return( iterator( this , size() ) ); }
229 [[nodiscard]] const_iterator begin( void ) const {
230 return( const_iterator( this , 0 ) );
231 }
232
233 [[nodiscard]] const_iterator end( void ) const {
234 return( const_iterator( this , size() ) );
235 }
236
237 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
239 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
240
241/*--------------------------------------------------------------------------*/
242 private:
244 std::size_t rankOf( const_iterator p ) const {
245 return( std::size_t( p - begin() ) );
246 }
247
261 void balance( void );
262
264 [[nodiscard]] bool balanced( void ) const;
265
266 RingDeque< T > f_L;
267 RingDeque< T > f_R;
268
269 }; // end( class Treque )
270
271/*--------------------------------------------------------------------------*/
272
273} // end( namespace ADS )
274
275/*--------------------------------------------------------------------------*/
276/*-------------------------- IMPLEMENTATION --------------------------------*/
277/*--------------------------------------------------------------------------*/
278
279#include "ADS/sequence/impl/Treque.tpp"
280
281/*--------------------------------------------------------------------------*/
282
283#endif /* Treque.h included */
284
285/*--------------------------------------------------------------------------*/
286/*--------------------------- End File Treque.h ----------------------------*/
287/*--------------------------------------------------------------------------*/
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...
A double ended queue on a circular vector that grows, beyond the book: the circular vector of ArrayQu...
the random-access iterator on the items of a container C read by rank, c[ i ] the item ,...
Definition Iterators.h:392
void clear(void)
removes all the items
Definition Treque.h:216
void erase(std::size_t i)
removes the item of rank , in time
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 Treque.h:170
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 Treque.h:160
iterator end(void)
the iterator past the last item
Definition Treque.h:227
void pop_front(void)
removes the first item
Definition Treque.h:209
iterator erase(const_iterator p)
the item of removed, i.e., erase( i ) with its rank; an iterator on the item after it
Definition Treque.h:186
void push_front(const T &x)
at the front
Definition Treque.h:202
T & operator[](std::size_t i)
the item of rank , in or in
Definition Treque.h:100
const T & back(void) const
the last item
Definition Treque.h:118
std::size_t size_type
the type of the sizes
Definition Treque.h:75
const T & const_reference
a constant reference
Definition Treque.h:78
Treque(std::initializer_list< T > l)
the items of l, in order
const_iterator cend(void) const
the constant iterator past the last item
Definition Treque.h:239
RankIterator< Treque, T, true > const_iterator
the iterator on the constant items
Definition Treque.h:82
iterator emplace(const_iterator p, A &&... a)
an item built from before the item of
Definition Treque.h:179
bool operator==(const Treque &o) const
the same items in the same order
std::size_t size(void) const
the number of items
Definition Treque.h:94
const_iterator cbegin(void) const
the constant iterator on the first item
Definition Treque.h:237
void push_back(const T &x)
at the back
Definition Treque.h:205
RankIterator< Treque, T, false > iterator
the iterator on the items
Definition Treque.h:80
const T & front(void) const
the first item
Definition Treque.h:114
iterator erase(const_iterator first, const_iterator last)
the items of removed, one erase( i ) each; an iterator on the item after them
Definition Treque.h:194
Treque(void)=default
the empty treque
const_iterator end(void) const
the constant iterator past the last item
Definition Treque.h:233
bool empty(void) const
true if there is no item
Definition Treque.h:91
const T & operator[](std::size_t i) const
the item of rank , constant
Definition Treque.h:107
T & reference
a reference to an item
Definition Treque.h:77
T value_type
the type of the items
Definition Treque.h:74
const_iterator begin(void) const
the constant iterator on the first item
Definition Treque.h:229
void insert(std::size_t i, const T &x)
puts at the rank , in amortized time
std::ptrdiff_t difference_type
the type of the distances
Definition Treque.h:76
void pop_back(void)
removes the last item
Definition Treque.h:213
iterator begin(void)
the iterator on the first item
Definition Treque.h:225
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
Definition ADT.h:96