ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
CursorList.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File CursorList.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
43/*--------------------------------------------------------------------------*/
44/*----------------------------- DEFINITIONS --------------------------------*/
45/*--------------------------------------------------------------------------*/
46
47#ifndef __ADS_CursorList
48 #define __ADS_CursorList
49/* self-identification: #endif constant */
50
51/*--------------------------------------------------------------------------*/
52/*------------------------------ INCLUDES ----------------------------------*/
53/*--------------------------------------------------------------------------*/
54
55#include "ADS/Contract.h"
56
57#include <cstddef>
58#include <initializer_list>
59#include <iterator>
60#include <memory>
61#include <type_traits>
62#include <utility>
63#include <vector>
64
65/*--------------------------------------------------------------------------*/
66/*--------------------------- NAMESPACE ------------------------------------*/
67/*--------------------------------------------------------------------------*/
68
69namespace ADS
70{
71
72/*--------------------------------------------------------------------------*/
73/*-------------------------- CLASS CursorSpace -----------------------------*/
74/*--------------------------------------------------------------------------*/
75
77template< class T >
79{
80
81/*--------------------------------------------------------------------------*/
82 public:
84 struct Cell
85 {
87 long next;
88 };
89
93 explicit CursorSpace( long dim = 8 );
94
97 [[nodiscard]] Cell & operator[]( long c );
98
101 [[nodiscard]] const Cell & operator[]( long c ) const;
102
123 long take( void );
124
136 void give( long c );
137
139 [[nodiscard]] long capacity( void ) const;
140
142 [[nodiscard]] long used( void ) const;
143
144/*--------------------------------------------------------------------------*/
145 private:
148 [[nodiscard]] bool partition( void ) const;
149
150 std::vector< Cell > f_A;
151 long f_free;
152 long f_used = 0;
153
154 }; // end( class CursorSpace )
155
156/*--------------------------------------------------------------------------*/
157/*-------------------------- CLASS CursorList ------------------------------*/
158/*--------------------------------------------------------------------------*/
159
161template< class T >
163{
164
165/*--------------------------------------------------------------------------*/
166 public:
167 using Item = T;
168 using Pos = long;
169
171 CursorList( void );
172
174 explicit CursorList( std::shared_ptr< CursorSpace< T > > S );
175
177 CursorList( std::initializer_list< T > items );
178
180 CursorList( const CursorList & other );
181
183 CursorList( CursorList && other ) noexcept;
184
186 CursorList & operator=( CursorList other ) noexcept;
187
190
192 void swap( CursorList & other ) noexcept;
193
194/*--------------------------------------------------------------------------*/
195/*-------------------------- THE SEQUENCE ----------------------------------*/
196/*--------------------------------------------------------------------------*/
197
199 [[nodiscard]] bool empty( void ) const;
200
202 [[nodiscard]] bool finished( Pos p ) const;
203
205 [[nodiscard]] Pos head( void ) const;
206
209 [[nodiscard]] Pos tail( void ) const;
210
213 [[nodiscard]] Pos next( Pos p ) const;
214
217 [[nodiscard]] Pos prev( Pos p ) const;
218
236 Pos insert( Pos p , const T & v );
237
253
256 [[nodiscard]] const T & read( Pos p ) const;
257
261 void write( Pos p , const T & v );
262
263/*--------------------------------------------------------------------------*/
264/*---------------------------- BEYOND THE BOOK -----------------------------*/
265/*--------------------------------------------------------------------------*/
266
268 [[nodiscard]] bool operator==( const CursorList & o ) const;
269
271 [[nodiscard]] const CursorSpace< T > & space( void ) const;
272
274 [[nodiscard]] long first( void ) const;
275
276/*------------------------- THE STANDARD INTERFACE -------------------------*/
277
278 // a forward iterator holds the space and the index of the cell of its
279 // item, never a reference to the cell, since take() in any list of the
280 // space may move the cells; the end is the index 0
281
283 template< bool Const >
285 {
286 using Space =
287 std::conditional_t< Const , const CursorSpace< T > , CursorSpace< T > >;
288
289 public:
291 using iterator_category = std::forward_iterator_tag;
292 using value_type = T;
293 using difference_type = std::ptrdiff_t;
295 using reference = std::conditional_t< Const , const T & , T & >;
297 using pointer = std::conditional_t< Const , const T * , T * >;
298
300 Iterator( void ) = default;
301
304 Iterator( Space * S , long c ) : f_S( S ) , f_c( c ) { }
305
307 template< bool C >
308 requires( Const && ! C )
309 Iterator( const Iterator< C > & o ) : f_S( o.f_S ) , f_c( o.f_c ) { }
310
312 reference operator*( void ) const { return( ( *f_S )[ f_c ].value ); }
314 pointer operator->( void ) const { return( &**this ); }
317 f_c = ( *f_S )[ f_c ].next;
318 return( *this );
319 }
320
322 auto t = *this;
323 ++*this;
324 return( t );
325 }
326
327 bool operator==( const Iterator & o ) const = default;
328
329 private:
330 Space * f_S = nullptr;
331 long f_c = 0;
332
333 template< bool >
334 friend class Iterator;
335 };
336
338 using value_type = T;
340 using size_type = std::size_t;
342 using difference_type = std::ptrdiff_t;
344 using reference = T &;
346 using const_reference = const T &;
351
353 [[nodiscard]] iterator begin( void ) {
354 return( iterator( f_S.get() , f_first ) );
355 }
356
357 [[nodiscard]] iterator end( void ) { return( iterator( f_S.get() , 0 ) ); }
359 [[nodiscard]] const_iterator begin( void ) const {
360 return( const_iterator( f_S.get() , f_first ) );
361 }
362
363 [[nodiscard]] const_iterator end( void ) const {
364 return( const_iterator( f_S.get() , 0 ) );
365 }
366
367 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
369 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
370
372 template< std::input_iterator I , std::sentinel_for< I > S >
373 CursorList( I first , S last );
374
376 void push_front( const T & v );
377
378/*--------------------------------------------------------------------------*/
379 private:
381 [[nodiscard]] long & link( Pos p );
382
384 [[nodiscard]] long cell( Pos p ) const;
385
386 std::shared_ptr< CursorSpace< T > > f_S;
387 long f_first = 0;
388
389 }; // end( class CursorList )
390
391/*--------------------------------------------------------------------------*/
392
393} // end( namespace ADS )
394
395/*--------------------------------------------------------------------------*/
396/*-------------------------- IMPLEMENTATION --------------------------------*/
397/*--------------------------------------------------------------------------*/
398
399#include "ADS/sequence/impl/CursorList.tpp"
400
401/*--------------------------------------------------------------------------*/
402
403#endif /* CursorList.h included */
404
405/*--------------------------------------------------------------------------*/
406/*------------------------- End File CursorList.h --------------------------*/
407/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
Iterator(const Iterator< C > &o)
the const iterator from the other one
Definition CursorList.h:309
pointer operator->(void) const
the address of the item
Definition CursorList.h:314
bool operator==(const Iterator &o) const =default
equal on the same cell of the same space
std::ptrdiff_t difference_type
the type of the distances
Definition CursorList.h:293
std::conditional_t< Const, const T *, T * > pointer
a pointer to an item, constant if Const
Definition CursorList.h:297
Iterator(Space *S, long c)
on the item of the cell of the space , the end if
Definition CursorList.h:304
Iterator(void)=default
an iterator on no space
std::forward_iterator_tag iterator_category
the category of the iterator, forward
Definition CursorList.h:291
reference operator*(void) const
the item
Definition CursorList.h:312
T value_type
the type of the items
Definition CursorList.h:292
std::conditional_t< Const, const T &, T & > reference
a reference to an item, constant if Const
Definition CursorList.h:295
friend class Iterator
Definition CursorList.h:334
Iterator & operator++(void)
to the next cell
Definition CursorList.h:316
Iterator operator++(int)
to the next cell, returning the iterator before the step
Definition CursorList.h:321
long Pos
the cursor of the cell before the item
Definition CursorList.h:168
CursorList(std::initializer_list< T > items)
the list of the given items, in a space of its own
CursorList(CursorList &&other) noexcept
the cells of other, which is left empty in the same space
Pos next(Pos p) const
the position that follows
std::ptrdiff_t difference_type
the type of the distances
Definition CursorList.h:342
std::size_t size_type
the type of the sizes
Definition CursorList.h:340
Pos insert(Pos p, const T &v)
puts in the position , the new item taking the position
T & reference
a reference to an item
Definition CursorList.h:344
long first(void) const
the cursor of the first cell, 0 if the list is empty
iterator begin(void)
the iterator on the first item
Definition CursorList.h:353
iterator end(void)
the iterator past the last item
Definition CursorList.h:357
T value_type
the type of the items
Definition CursorList.h:338
bool empty(void) const
true if there is no item
const_iterator cbegin(void) const
the constant iterator on the first item
Definition CursorList.h:367
CursorList(I first, S last)
the list of the items of [ first , last ), in their order
Iterator< false > iterator
the iterator on the items
Definition CursorList.h:348
Pos prev(Pos p) const
the position that precedes , , by a scan as in SinglyList::prev()
const CursorSpace< T > & space(void) const
the space of the list
Pos tail(void) const
the position of the last item, , by a scan as in SinglyList::tail()
const T & const_reference
a constant reference to an item
Definition CursorList.h:346
void swap(CursorList &other) noexcept
exchanges the cells, and the spaces, with other
void push_front(const T &v)
at the front, for std::front_inserter
Pos remove(Pos p)
removes the item in the position , whose successor takes the position
CursorList(void)
the empty list, in a space of its own
const T & read(Pos p) const
the item in the position
bool operator==(const CursorList &o) const
the same items in the same order
CursorList & operator=(CursorList other) noexcept
the items of other, by copy and swap
CursorList(const CursorList &other)
a copy of the items of other, in the same space
CursorList(std::shared_ptr< CursorSpace< T > > S)
the empty list in the space , shared with other lists
const_iterator begin(void) const
the constant iterator on the first item
Definition CursorList.h:359
const_iterator cend(void) const
the constant iterator past the last item
Definition CursorList.h:369
T Item
the type of the items
Definition CursorList.h:167
~CursorList()
gives the cells back to the free list of the space
bool finished(Pos p) const
true on and on
const_iterator end(void) const
the constant iterator past the last item
Definition CursorList.h:363
Iterator< true > const_iterator
the iterator on the constant items
Definition CursorList.h:350
Pos head(void) const
, i.e., 0
void write(Pos p, const T &v)
writes in the position
the vector of the cells of some CursorList, with its free list
Definition CursorList.h:79
CursorSpace(long dim=8)
a space of free cells, the places
void give(long c)
gives the cell back to the free list
const Cell & operator[](long c) const
the cell at the cursor , constant
long capacity(void) const
the number of places, 0 excluded
Cell & operator[](long c)
the cell at the cursor
long used(void) const
the number of cells in use
long take(void)
takes the first free cell, doubling the vector if there is none
Definition ADT.h:96
a cell: the item and the cursor of the next cell, 0 for nil
Definition CursorList.h:85
T value
the item
Definition CursorList.h:86
long next
the cursor of the next cell, 0 for nil
Definition CursorList.h:87