ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
RingDeque.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File RingDeque.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
57/*--------------------------------------------------------------------------*/
58/*----------------------------- DEFINITIONS --------------------------------*/
59/*--------------------------------------------------------------------------*/
60
61#ifndef __ADS_RingDeque
62 #define __ADS_RingDeque
63/* self-identification: #endif constant */
64
65/*--------------------------------------------------------------------------*/
66/*------------------------------ INCLUDES ----------------------------------*/
67/*--------------------------------------------------------------------------*/
68
69#include "ADS/Contract.h"
70#include "ADS/Portable.h"
71
72#include <algorithm>
73#include <bit>
74#include <compare>
75#include <cstddef>
76#include <cstring>
77#include <initializer_list>
78#include <iterator>
79#include <memory>
80#include <new>
81#include <type_traits>
82#include <utility>
83
84/*--------------------------------------------------------------------------*/
85/*--------------------------- NAMESPACE ------------------------------------*/
86/*--------------------------------------------------------------------------*/
87
89#define ADS_RING_INLINE ADS_ALWAYS_INLINE
91#define ADS_RING_NOINLINE ADS_NOINLINE
92
93namespace ADS
94{
95
96namespace detail
97{
98
101template< class T , bool Const >
102class MaskIterator
103{
104 using Item = std::conditional_t< Const , const T , T >;
105
106 public:
108 using iterator_category = std::random_access_iterator_tag;
110 using value_type = T;
112 using difference_type = std::ptrdiff_t;
114 using reference = Item &;
116 using pointer = Item *;
117
119 MaskIterator( void ) = default;
120 MaskIterator( Item * A , std::size_t head , std::size_t mask , std::size_t i )
121 : f_A( A ) , f_head( head ) , f_mask( mask ) , f_i( i ) { }
122
124 template< bool K >
125 requires( Const && ! K )
126 MaskIterator( const MaskIterator< T , K > & o )
127 : f_A( o.f_A ) , f_head( o.f_head ) , f_mask( o.f_mask ) ,
128 f_i( o.f_i ) { }
129
131 reference operator*( void ) const {
132 return( f_A[ ( f_head + f_i ) & f_mask ] );
133 }
135 pointer operator->( void ) const { return( &**this ); }
137 reference operator[]( difference_type k ) const {
138 return( f_A[ ( f_head + f_i + std::size_t( k ) ) & f_mask ] );
139 }
140
142 MaskIterator & operator++( void ) {
143 ++f_i;
144 return( *this );
145 }
147 MaskIterator & operator--( void ) {
148 --f_i;
149 return( *this );
150 }
152 MaskIterator operator++( int ) {
153 auto t = *this;
154 ++f_i;
155 return( t );
156 }
158 MaskIterator operator--( int ) {
159 auto t = *this;
160 --f_i;
161 return( t );
162 }
164 MaskIterator & operator+=( difference_type k ) {
165 f_i += std::size_t( k );
166 return( *this );
167 }
169 MaskIterator & operator-=( difference_type k ) {
170 f_i -= std::size_t( k );
171 return( *this );
172 }
173 friend MaskIterator operator+( MaskIterator a , difference_type k ) {
174 return( a += k );
175 }
176 friend MaskIterator operator+( difference_type k , MaskIterator a ) {
177 return( a += k );
178 }
179 friend MaskIterator operator-( MaskIterator a , difference_type k ) {
180 return( a -= k );
181 }
182 friend difference_type operator-( const MaskIterator & a ,
183 const MaskIterator & b ) {
184 return( difference_type( a.f_i - b.f_i ) );
185 }
186 bool operator==( const MaskIterator & o ) const { return( f_i == o.f_i ); }
187 std::strong_ordering operator<=>( const MaskIterator & o ) const {
188 return( f_i <=> o.f_i );
189 }
190
191 private:
192 template< class , bool >
193 friend class MaskIterator;
194
195 Item * f_A = nullptr;
196 std::size_t f_head = 0 , f_mask = 0;
197 std::size_t f_i = 0;
198 }; // end( class MaskIterator )
199
200} // end( namespace detail )
201
202/*--------------------------------------------------------------------------*/
203/*---------------------------- CLASS RingDeque -----------------------------*/
204/*--------------------------------------------------------------------------*/
205
208template< class T >
210{
211/*--------------------------------------------------------------------------*/
212 public:
214 using value_type = T;
216 using size_type = std::size_t;
218 using difference_type = std::ptrdiff_t;
220 using reference = T &;
222 using const_reference = const T &;
224 using iterator = detail::MaskIterator< T , false >;
226 using const_iterator = detail::MaskIterator< T , true >;
228 using reverse_iterator = std::reverse_iterator< iterator >;
230 using const_reverse_iterator = std::reverse_iterator< const_iterator >;
231
233 RingDeque( void ) = default;
234
236 RingDeque( std::initializer_list< T > l );
237
239 RingDeque( const RingDeque & o );
240
242 RingDeque( RingDeque && o ) noexcept;
243
246
249
251 void swap( RingDeque & o ) noexcept;
252
254 [[nodiscard]] bool empty( void ) const { return( f_head == f_tail ); }
255
257 [[nodiscard]] std::size_t size( void ) const { return( f_tail - f_head ); }
258
260 [[nodiscard]] std::size_t capacity( void ) const { return( f_mask + 1 ); }
261
263 void reserve( std::size_t n );
264
267 [[nodiscard]] T & front( void );
268
271 [[nodiscard]] const T & front( void ) const;
272
275 [[nodiscard]] T & back( void );
276
279 [[nodiscard]] const T & back( void ) const;
280
284 [[nodiscard]] T & operator[]( std::size_t i );
285
288 [[nodiscard]] const T & operator[]( std::size_t i ) const;
289
291 ADS_RING_INLINE void push_back( const T & v ) { emplace_back( v ); }
292
294 ADS_RING_INLINE void push_back( T && v ) { emplace_back( std::move( v ) ); }
295
297 ADS_RING_INLINE void push_front( const T & v ) { emplace_front( v ); }
298
300 ADS_RING_INLINE void push_front( T && v ) { emplace_front( std::move( v ) ); }
301
323 template< class... Args >
324 T & emplace_back( Args &&... args );
325
341 template< class... Args >
342 T & emplace_front( Args &&... args );
343
347 void pop_front( void );
348
352 void pop_back( void );
353
355 void clear( void );
356
379 void insert( std::size_t i , const T & v );
380
401 void erase( std::size_t i );
402
422 void rotate( std::size_t r );
423
429 void shrink_to_fit( void );
430
432 [[nodiscard]] bool operator==( const RingDeque & o ) const;
433
435 [[nodiscard]] iterator begin( void ) {
436 return( iterator( f_A , f_head , f_mask , 0 ) );
437 }
438
439 [[nodiscard]] iterator end( void ) {
440 return( iterator( f_A , f_head , f_mask , size() ) );
441 }
442
443 [[nodiscard]] const_iterator begin( void ) const {
444 return( const_iterator( f_A , f_head , f_mask , 0 ) );
445 }
446
447 [[nodiscard]] const_iterator end( void ) const {
448 return( const_iterator( f_A , f_head , f_mask , size() ) );
449 }
450
451 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
453 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
455 [[nodiscard]] reverse_iterator rbegin( void ) {
456 return( reverse_iterator( end() ) );
457 }
458
459 [[nodiscard]] reverse_iterator rend( void ) {
460 return( reverse_iterator( begin() ) );
461 }
462
463 [[nodiscard]] const_reverse_iterator rbegin( void ) const {
464 return( const_reverse_iterator( end() ) );
465 }
466
467 [[nodiscard]] const_reverse_iterator rend( void ) const {
468 return( const_reverse_iterator( begin() ) );
469 }
470
471/*--------------------------------------------------------------------------*/
472 private:
478 ADS_RING_NOINLINE static T * relocate( T * A , std::size_t m , std::size_t h ,
479 std::size_t n );
480
482 void grow( void );
483
487 T & growBack( T && v );
488 T & growFront( T && v );
489
510 void shiftDown( std::size_t lo , std::size_t hi );
511
514 void shiftUp( std::size_t lo , std::size_t hi );
515
517 [[nodiscard]] bool fits( void ) const;
518
519 // the front and the back are counters modulo 2^64, apart by the size,
520 // so that the insertions at the back and the removals from the front
521 // change one each, two chains that do not wait for each other
522 using Count = std::size_t;
523
524 T * f_A = nullptr;
525 Count f_mask = Count( -1 );
526 Count f_head = 0;
527 Count f_tail = 0;
528 }; // end( class RingDeque )
529
530/*--------------------------------------------------------------------------*/
531
532} // end( namespace ADS )
533
534/*--------------------------------------------------------------------------*/
535/*-------------------------- IMPLEMENTATION --------------------------------*/
536/*--------------------------------------------------------------------------*/
537
538#include "ADS/queue/impl/RingDeque.tpp"
539
540/*--------------------------------------------------------------------------*/
541
542#endif /* RingDeque.h included */
543
544/*--------------------------------------------------------------------------*/
545/*-------------------------- End File RingDeque.h --------------------------*/
546/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
const_reverse_iterator rend(void) const
the constant iterator before the first item, backwards
Definition RingDeque.h:467
const_iterator cend(void) const
the constant iterator past the last item
Definition RingDeque.h:453
T & reference
a reference to an item
Definition RingDeque.h:220
std::ptrdiff_t difference_type
the type of the distances
Definition RingDeque.h:218
T & front(void)
the item at the front
const_iterator begin(void) const
the constant iterator on the first item
Definition RingDeque.h:443
const_reverse_iterator rbegin(void) const
the constant iterator on the last item, backwards
Definition RingDeque.h:463
void push_front(const T &v)
at the front, emplace_front( v )
Definition RingDeque.h:297
detail::MaskIterator< T, false > iterator
the iterator on the items
Definition RingDeque.h:224
T & emplace_back(Args &&... args)
the item built from args at the back, amortized
void pop_front(void)
removes the item at the front, advancing
const_iterator end(void) const
the constant iterator past the last item
Definition RingDeque.h:447
bool operator==(const RingDeque &o) const
the same items in the same order
void push_back(const T &v)
at the back, emplace_back( v )
Definition RingDeque.h:291
std::size_t size(void) const
the number of items, the difference of the two counters
Definition RingDeque.h:257
reverse_iterator rbegin(void)
the iterator on the last item, backwards
Definition RingDeque.h:455
const_iterator cbegin(void) const
the constant iterator on the first item
Definition RingDeque.h:451
void push_front(T &&v)
at the front, moved
Definition RingDeque.h:300
RingDeque(RingDeque &&o) noexcept
the vector of o, which is left empty with no capacity
reverse_iterator rend(void)
the iterator before the first item, backwards
Definition RingDeque.h:459
void reserve(std::size_t n)
at least places
void swap(RingDeque &o) noexcept
exchanges the vector with that of o
RingDeque(void)=default
the empty deque, with no places
iterator begin(void)
the iterator on the first item
Definition RingDeque.h:435
RingDeque & operator=(RingDeque o) noexcept
the items of o, by copy and swap
const T & front(void) const
the item at the front, constant
void insert(std::size_t i, const T &v)
puts at the rank , moving by one place the items on the shorter side of , in amortized time (the ad...
iterator end(void)
the iterator past the last item
Definition RingDeque.h:439
const T & const_reference
a constant reference to an item
Definition RingDeque.h:222
std::reverse_iterator< iterator > reverse_iterator
the iterator on the items, backwards
Definition RingDeque.h:228
T & back(void)
the item at the back
std::size_t capacity(void) const
the places, 0 or a power of 2
Definition RingDeque.h:260
T value_type
the type of the items
Definition RingDeque.h:214
const T & back(void) const
the item at the back, constant
T & emplace_front(Args &&... args)
the item built from args at the front, amortized
RingDeque(const RingDeque &o)
a copy of o
T & operator[](std::size_t i)
the item of rank from the front, at the place
std::reverse_iterator< const_iterator > const_reverse_iterator
the iterator on the constant items, backwards
Definition RingDeque.h:230
std::size_t size_type
the type of the sizes
Definition RingDeque.h:216
void clear(void)
removes all the items, the places kept
detail::MaskIterator< T, true > const_iterator
the iterator on the constant items
Definition RingDeque.h:226
const T & operator[](std::size_t i) const
the item of rank from the front, constant
void shrink_to_fit(void)
the least number of places, a power of 2 and 8 at least, that holds the items, the items moved there...
~RingDeque()
destroys the items and frees the places
void rotate(std::size_t r)
the item of rank moved to the rank , for every : time when the vector is full, the two counters alo...
void erase(std::size_t i)
removes the item of rank , moving by one place the items on the shorter side, in time (the remove( i...
void push_back(T &&v)
at the back, moved
Definition RingDeque.h:294
bool empty(void) const
true if there is no item, i.e., the two counters are equal
Definition RingDeque.h:254
RingDeque(std::initializer_list< T > l)
the items of l, in order
void pop_back(void)
removes the item at the back, moving back
#define ADS_RING_NOINLINE
the growth, never inlined
Definition RingDeque.h:91
#define ADS_RING_INLINE
the operations at the ends, inlined even where the compiler would not
Definition RingDeque.h:89
Definition ADT.h:96