ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
XorList.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*----------------------------- File XorList.h -----------------------------*/
3/*--------------------------------------------------------------------------*/
47/*--------------------------------------------------------------------------*/
48/*----------------------------- DEFINITIONS --------------------------------*/
49/*--------------------------------------------------------------------------*/
50
51#ifndef __ADS_XorList
52 #define __ADS_XorList
53/* self-identification: #endif constant */
54
55/*--------------------------------------------------------------------------*/
56/*------------------------------ INCLUDES ----------------------------------*/
57/*--------------------------------------------------------------------------*/
58
59#include "ADS/Contract.h"
61
62#include <concepts>
63#include <cstddef>
64#include <cstdint>
65#include <initializer_list>
66#include <iterator>
67#include <type_traits>
68#include <utility>
69
70/*--------------------------------------------------------------------------*/
71/*--------------------------- NAMESPACE ------------------------------------*/
72/*--------------------------------------------------------------------------*/
73
74namespace ADS
75{
76
77/*--------------------------------------------------------------------------*/
78/*----------------------------- CLASS XorList ------------------------------*/
79/*--------------------------------------------------------------------------*/
80
83template< class T >
85{
86
87 struct Cell;
88
89/*--------------------------------------------------------------------------*/
90 public:
91 using value_type = T;
92 using size_type = std::size_t;
93 using difference_type = std::ptrdiff_t;
94 using reference = T &;
95 using const_reference = const T &;
96
98 XorList( void ) = default;
99
101 XorList( std::initializer_list< T > l );
102
104 XorList( const XorList & o );
105
107 XorList( XorList && o ) noexcept;
108
110 XorList & operator=( XorList o ) noexcept;
111
114
116 void swap( XorList & o ) noexcept;
117
119 [[nodiscard]] bool empty( void ) const { return( f_n == 0 ); }
120
122 [[nodiscard]] std::size_t size( void ) const { return( f_n ); }
123
126 [[nodiscard]] const T & front( void ) const;
127
130 [[nodiscard]] const T & back( void ) const;
131
146 void push_front( const T & x );
147
150 void push_back( const T & x );
151
168 void pop_front( void );
169
173 void pop_back( void );
174
177 void reverse( void ) { std::swap( f_head , f_tail ); }
178
180 void clear( void );
181
183 [[nodiscard]] bool operator==( const XorList & o ) const;
184
187 template< bool Const >
189 {
190 public:
192 using iterator_category = std::bidirectional_iterator_tag;
193 using value_type = T;
194 using difference_type = std::ptrdiff_t;
196 using reference = std::conditional_t< Const , const T & , T & >;
198 using pointer = std::conditional_t< Const , const T * , T * >;
199
201 Iterator( void ) = default;
202
204 Iterator( Cell * p , Cell * c ) : f_p( p ) , f_c( c ) { }
205
207 template< bool K >
208 requires( Const && ! K )
209 Iterator( const Iterator< K > & o ) : f_p( o.f_p ) , f_c( o.f_c ) { }
210
212 reference operator*( void ) const { return( f_c->value ); }
214 pointer operator->( void ) const { return( &f_c->value ); }
217 Cell * n = other( f_c , f_p );
218 f_p = f_c;
219 f_c = n;
220 return( *this );
221 }
222
224 Cell * p = other( f_p , f_c );
225 f_c = f_p;
226 f_p = p;
227 return( *this );
228 }
229
231 auto t = *this;
232 ++*this;
233 return( t );
234 }
235
237 auto t = *this;
238 --*this;
239 return( t );
240 }
241
242 bool operator==( const Iterator & o ) const {
243 return( ( f_c == o.f_c ) && ( f_p == o.f_p ) );
244 }
245
246 private:
247 template< bool >
248 friend class Iterator;
249 Cell * f_p = nullptr;
250 Cell * f_c = nullptr;
251 }; // end( class Iterator )
252
255
257 [[nodiscard]] iterator begin( void ) {
258 return( iterator( nullptr , f_head ) );
259 }
260
261 [[nodiscard]] iterator end( void ) { return( iterator( f_tail , nullptr ) ); }
263 [[nodiscard]] const_iterator begin( void ) const {
264 return( const_iterator( nullptr , f_head ) );
265 }
266
267 [[nodiscard]] const_iterator end( void ) const {
268 return( const_iterator( f_tail , nullptr ) );
269 }
270
271/*--------------------------------------------------------------------------*/
272 private:
275 struct Cell
276 {
277 std::uintptr_t link;
278 T value;
279 };
280
282 static std::uintptr_t word( const Cell * a ) {
283 return( reinterpret_cast< std::uintptr_t >( a ) );
284 }
285
287 static Cell * other( const Cell * c , const Cell * p ) {
288 return( reinterpret_cast< Cell * >( c->link ^ word( p ) ) );
289 }
290
292 [[nodiscard]] bool walks( void ) const;
293
294 Cell * f_head = nullptr;
295 Cell * f_tail = nullptr;
296 std::size_t f_n = 0;
297 ObjectPool< Cell > f_pool;
298
299 }; // end( class XorList )
300
301/*--------------------------------------------------------------------------*/
302/*------------------------------- FUNCTIONS --------------------------------*/
303/*--------------------------------------------------------------------------*/
304
316template< std::unsigned_integral U >
317constexpr void xorSwap( U & a , U & b );
318
319/*--------------------------------------------------------------------------*/
320
321} // end( namespace ADS )
322
323/*--------------------------------------------------------------------------*/
324/*-------------------------- IMPLEMENTATION --------------------------------*/
325/*--------------------------------------------------------------------------*/
326
327#include "ADS/sequence/impl/XorList.tpp"
328
329/*--------------------------------------------------------------------------*/
330
331#endif /* XorList.h included */
332
333/*--------------------------------------------------------------------------*/
334/*--------------------------- End File XorList.h ---------------------------*/
335/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
A pool of objects of one type, beyond the book: make() builds an object in a slot of the pool and fre...
std::bidirectional_iterator_tag iterator_category
bidirectional
Definition XorList.h:192
std::ptrdiff_t difference_type
the distances
Definition XorList.h:194
Iterator operator++(int)
to the next item, returning the iterator before the step
Definition XorList.h:230
bool operator==(const Iterator &o) const
equal on the same cell
Definition XorList.h:242
Iterator operator--(int)
to the previous item, returning the iterator before the step
Definition XorList.h:236
Iterator & operator--(void)
to the previous cell
Definition XorList.h:223
reference operator*(void) const
the item
Definition XorList.h:212
pointer operator->(void) const
the address of the item
Definition XorList.h:214
std::conditional_t< Const, const T *, T * > pointer
a pointer to an item, constant if Const
Definition XorList.h:198
Iterator(Cell *p, Cell *c)
on the cell , coming from
Definition XorList.h:204
Iterator(const Iterator< K > &o)
the constant iterator from the mutable one
Definition XorList.h:209
Iterator & operator++(void)
to the next cell,
Definition XorList.h:216
Iterator(void)=default
an iterator on no list
T value_type
the items
Definition XorList.h:193
friend class Iterator
Definition XorList.h:248
std::conditional_t< Const, const T &, T & > reference
a reference to an item, constant if Const
Definition XorList.h:196
const T & back(void) const
the last item
Iterator< true > const_iterator
on the constant items
Definition XorList.h:254
void swap(XorList &o) noexcept
exchanges the cells with those of o
std::ptrdiff_t difference_type
the type of the distances
Definition XorList.h:93
iterator begin(void)
the iterator on the first item
Definition XorList.h:257
std::size_t size_type
the type of the sizes
Definition XorList.h:92
~XorList()
destroys the items
XorList(const XorList &o)
a copy of o, cell by cell
const_iterator end(void) const
the constant iterator past the last item
Definition XorList.h:267
const T & const_reference
a constant reference
Definition XorList.h:95
Iterator< false > iterator
the iterator on the items
Definition XorList.h:253
XorList & operator=(XorList o) noexcept
the items of o, by copy and swap
const_iterator begin(void) const
the constant iterator on the first item
Definition XorList.h:263
iterator end(void)
the iterator past the last item
Definition XorList.h:261
void push_front(const T &x)
at the front,
void clear(void)
removes all the items
std::size_t size(void) const
the number of items
Definition XorList.h:122
void reverse(void)
the order of the items reversed in , by exchanging the first and the last cell
Definition XorList.h:177
XorList(XorList &&o) noexcept
the cells of o, which is left empty
bool empty(void) const
true if there is no item
Definition XorList.h:119
void pop_back(void)
removes the last item, , the mirror of pop_front()
void push_back(const T &x)
at the back, , the mirror of push_front()
T value_type
the type of the items
Definition XorList.h:91
void pop_front(void)
removes the first item,
T & reference
a reference to an item
Definition XorList.h:94
const T & front(void) const
the first item
bool operator==(const XorList &o) const
the same items in the same order
XorList(std::initializer_list< T > l)
the items of l, in order
XorList(void)=default
the empty list
constexpr void xorSwap(U &a, U &b)
and swapped by three exclusive ors and no third variable (exercise 3.19 of Morin's book)
Definition ADT.h:96