ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SinglyList.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File SinglyList.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
63/*--------------------------------------------------------------------------*/
64/*----------------------------- DEFINITIONS --------------------------------*/
65/*--------------------------------------------------------------------------*/
66
67#ifndef __ADS_SinglyList
68 #define __ADS_SinglyList
69/* self-identification: #endif constant */
70
71/*--------------------------------------------------------------------------*/
72/*------------------------------ INCLUDES ----------------------------------*/
73/*--------------------------------------------------------------------------*/
74
75#include "ADS/Contract.h"
77
78#include <cstddef>
79#include <initializer_list>
80#include <iterator>
81#include <stdexcept>
82#include <string>
83#include <type_traits>
84#include <utility>
85
86/*--------------------------------------------------------------------------*/
87/*--------------------------- NAMESPACE ------------------------------------*/
88/*--------------------------------------------------------------------------*/
89
90namespace ADS
91{
92
93/*--------------------------------------------------------------------------*/
94/*-------------------------- CLASS SinglyList ------------------------------*/
95/*--------------------------------------------------------------------------*/
96
98template< class T >
100{
101
102/*--------------------------------------------------------------------------*/
103 public:
105 struct Cell
106 {
109 };
110
111 using Item = T;
112 using Pos = Cell *;
113
115 SinglyList( void ) = default;
116
118 SinglyList( std::initializer_list< T > items );
119
121 SinglyList( const SinglyList & other );
122
124 SinglyList( SinglyList && other ) noexcept;
125
127 SinglyList & operator=( SinglyList other ) noexcept;
128
131
133 void swap( SinglyList & other ) noexcept;
134
135/*--------------------------------------------------------------------------*/
136/*-------------------------- THE SEQUENCE ----------------------------------*/
137/*--------------------------------------------------------------------------*/
138
140 [[nodiscard]] bool empty( void ) const;
141
144 [[nodiscard]] bool finished( Pos p ) const;
145
147 [[nodiscard]] Pos head( void ) const;
148
166 [[nodiscard]] Pos tail( void ) const;
167
170 [[nodiscard]] Pos next( Pos p ) const;
171
187 [[nodiscard]] Pos prev( Pos p ) const;
188
207 Pos insert( Pos p , const T & v );
208
226
229 [[nodiscard]] const T & read( Pos p ) const;
230
234 void write( Pos p , const T & v );
235
236/*--------------------------------------------------------------------------*/
237/*---------------------------- BEYOND THE BOOK -----------------------------*/
238/*--------------------------------------------------------------------------*/
239
241 [[nodiscard]] bool operator==( const SinglyList & o ) const;
242
243/*------------------------- THE STANDARD INTERFACE -------------------------*/
244
245 // a forward iterator holds the cell of its item, not the cell before it
246 // as a position does, so that the end is nil; it stays valid until its
247 // cell is removed, and a change made through it bypasses the audits of
248 // write()
249
251 template< bool Const >
253 {
254 public:
256 using iterator_category = std::forward_iterator_tag;
257 using value_type = T;
258 using difference_type = std::ptrdiff_t;
260 using reference = std::conditional_t< Const , const T & , T & >;
262 using pointer = std::conditional_t< Const , const T * , T * >;
263
265 Iterator( void ) = default;
266
268 explicit Iterator( Cell * c ) : f_c( c ) { }
269
271 template< bool C >
272 requires( Const && ! C )
273 Iterator( const Iterator< C > & o ) : f_c( o.f_c ) { }
274
276 reference operator*( void ) const { return( f_c->value ); }
278 pointer operator->( void ) const { return( &**this ); }
281 f_c = f_c->next;
282 return( *this );
283 }
284
286 auto t = *this;
287 ++*this;
288 return( t );
289 }
290
291 bool operator==( const Iterator & o ) const = default;
292
293 private:
294 Cell * f_c = nullptr;
295
296 template< bool >
297 friend class Iterator;
298 };
299
301 using value_type = T;
303 using size_type = std::size_t;
305 using difference_type = std::ptrdiff_t;
307 using reference = T &;
309 using const_reference = const T &;
314
316 [[nodiscard]] iterator begin( void ) { return( iterator( f_first ) ); }
318 [[nodiscard]] iterator end( void ) { return( iterator() ); }
320 [[nodiscard]] const_iterator begin( void ) const {
321 return( const_iterator( f_first ) );
322 }
323
324 [[nodiscard]] const_iterator end( void ) const {
325 return( const_iterator() );
326 }
327
328 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
330 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
331
333 template< std::input_iterator I , std::sentinel_for< I > S >
334 SinglyList( I first , S last );
335
338 void push_front( const T & v );
339
340/*------------------------- THE SLList OF MORIN ----------------------------*/
341
343 [[nodiscard]] std::size_t size( void ) const { return( f_size ); }
344
347 void push_back( const T & v );
348
352 T pop_front( void );
353
356 [[nodiscard]] const T & front( void ) const;
357
360 [[nodiscard]] const T & back( void ) const;
361
365 [[nodiscard]] const T & get( std::size_t i ) const;
366
370 T set( std::size_t i , const T & x );
371
390 void add( std::size_t i , const T & x );
391
396 T erase( std::size_t i );
397
412 [[nodiscard]] const T & secondLast( void ) const;
413
431 void reverse( void );
432
435 void checkSize( void ) const;
436
437/*--------------------------------------------------------------------------*/
438 private:
440 [[nodiscard]] Cell * cell( Pos p ) const;
441
443 [[nodiscard]] Cell * walk( std::size_t i ) const;
444
445 Cell * f_first = nullptr;
446 Cell * f_last = nullptr;
447 std::size_t f_size = 0;
448 ObjectPool< Cell > f_pool;
450 mutable Cell f_before{ T() , nullptr };
451
452 }; // end( class SinglyList )
453
454/*--------------------------------------------------------------------------*/
455
456} // end( namespace ADS )
457
458/*--------------------------------------------------------------------------*/
459/*-------------------------- IMPLEMENTATION --------------------------------*/
460/*--------------------------------------------------------------------------*/
461
462#include "ADS/sequence/impl/SinglyList.tpp"
463
464/*--------------------------------------------------------------------------*/
465
466#endif /* SinglyList.h included */
467
468/*--------------------------------------------------------------------------*/
469/*------------------------- End File SinglyList.h --------------------------*/
470/*--------------------------------------------------------------------------*/
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::forward_iterator_tag iterator_category
the category of the iterator, forward
Definition SinglyList.h:256
pointer operator->(void) const
the address of the item
Definition SinglyList.h:278
Iterator operator++(int)
to the next cell, returning the iterator before the step
Definition SinglyList.h:285
std::conditional_t< Const, const T &, T & > reference
a reference to an item, constant if Const
Definition SinglyList.h:260
std::ptrdiff_t difference_type
the type of the distances
Definition SinglyList.h:258
Iterator(const Iterator< C > &o)
the const iterator from the other one
Definition SinglyList.h:273
Iterator(Cell *c)
on the item of the cell , the end if is nil
Definition SinglyList.h:268
reference operator*(void) const
the item
Definition SinglyList.h:276
Iterator(void)=default
the end
T value_type
the type of the items
Definition SinglyList.h:257
Iterator & operator++(void)
to the next cell
Definition SinglyList.h:280
std::conditional_t< Const, const T *, T * > pointer
a pointer to an item, constant if Const
Definition SinglyList.h:262
friend class Iterator
Definition SinglyList.h:297
bool operator==(const Iterator &o) const =default
equal on the same cell
Iterator< true > const_iterator
the iterator on the constant items
Definition SinglyList.h:313
const_iterator begin(void) const
the constant iterator on the first item
Definition SinglyList.h:320
bool empty(void) const
true if there is no item
SinglyList(void)=default
the empty list
void push_front(const T &v)
at the front, for std::front_inserter (Morin's push( x )),
const T & read(Pos p) const
the item in the position
bool finished(Pos p) const
true on and on , the address of the last cell (nil if the list is empty)
T value_type
the type of the items
Definition SinglyList.h:301
iterator end(void)
the iterator past the last item
Definition SinglyList.h:318
void write(Pos p, const T &v)
writes in the position
T set(std::size_t i, const T &x)
written at the rank , ; the old item returned (exercise 3.3)
std::ptrdiff_t difference_type
the type of the distances
Definition SinglyList.h:305
const_iterator cend(void) const
the constant iterator past the last item
Definition SinglyList.h:330
std::size_t size(void) const
the number of items, kept
Definition SinglyList.h:343
SinglyList(I first, S last)
the list of the items of [ first , last ), in their order
void reverse(void)
the order of the items reversed by relinking the cells, in time and space, with no cell made (exerc...
T erase(std::size_t i)
removes the item of rank and returns it, (exercise 3.3)
void push_back(const T &v)
at the back, after the last cell (Morin's add( x )),
void swap(SinglyList &other) noexcept
exchanges the cells with other
const T & get(std::size_t i) const
the item of rank , by a walk of cells (exercise 3.3 of Morin's book)
const_iterator cbegin(void) const
the constant iterator on the first item
Definition SinglyList.h:328
Pos remove(Pos p)
removes the item in the position , whose successor takes the position
std::size_t size_type
the type of the sizes
Definition SinglyList.h:303
SinglyList(std::initializer_list< T > items)
the list of the given items, in their order
Pos next(Pos p) const
the position that follows
Pos tail(void) const
the position of the last item,
~SinglyList()
frees the cells
Pos prev(Pos p) const
the position that precedes ,
const T & secondLast(void) const
the item before the last one, by a walk that reads neither the size nor the last cell (exercise 3....
Pos insert(Pos p, const T &v)
puts in the position , i.e., before the item in ; the new item takes the position
SinglyList(SinglyList &&other) noexcept
the cells of other, which is left empty
Cell * Pos
the cell before the item, nil for the first one
Definition SinglyList.h:112
SinglyList & operator=(SinglyList other) noexcept
the items of other, by copy and swap
void add(std::size_t i, const T &x)
put at the rank , after the cell of rank reached by a walk, (exercise 3.3)
const T & back(void) const
the last item, from the last cell,
T pop_front(void)
removes the first item and returns it (Morin's pop() and remove()),
iterator begin(void)
the iterator on the first item
Definition SinglyList.h:316
bool operator==(const SinglyList &o) const
the same items in the same order
SinglyList(const SinglyList &other)
a copy of other, cell by cell
Iterator< false > iterator
the iterator on the items
Definition SinglyList.h:311
void checkSize(void) const
counts the cells and throws std::logic_error if they are not the size kept (exercise 3....
T & reference
a reference to an item
Definition SinglyList.h:307
const_iterator end(void) const
the constant iterator past the last item
Definition SinglyList.h:324
Pos head(void) const
, i.e., nil
T Item
the type of the items
Definition SinglyList.h:111
const T & const_reference
a constant reference to an item
Definition SinglyList.h:309
const T & front(void) const
the first item
Definition ADT.h:96
a cell of the list
Definition SinglyList.h:106
T value
the item
Definition SinglyList.h:107
Cell * next
the next cell, nil after the last one
Definition SinglyList.h:108