ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
List.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------------ File List.h -------------------------------*/
3/*--------------------------------------------------------------------------*/
78/*--------------------------------------------------------------------------*/
79/*----------------------------- DEFINITIONS --------------------------------*/
80/*--------------------------------------------------------------------------*/
81
82#ifndef __ADS_List
83 #define __ADS_List
84/* self-identification: #endif constant */
85
86/*--------------------------------------------------------------------------*/
87/*------------------------------ INCLUDES ----------------------------------*/
88/*--------------------------------------------------------------------------*/
89
90#include "ADS/Contract.h"
91#include "ADS/Portable.h"
93
94#include <cstddef>
95#include <functional>
96#include <initializer_list>
97#include <iterator>
98#include <stdexcept>
99#include <string>
100#include <type_traits>
101#include <utility>
102
103/*--------------------------------------------------------------------------*/
104/*--------------------------- NAMESPACE ------------------------------------*/
105/*--------------------------------------------------------------------------*/
106
107namespace ADS
108{
109
110/*--------------------------------------------------------------------------*/
111/*---------------------------- CLASS List ----------------------------------*/
112/*--------------------------------------------------------------------------*/
113
116template< class T , bool Pooled = true >
117class List
118{
119
120/*--------------------------------------------------------------------------*/
121 public:
122 using Item = T;
123
125 struct Cell
126 {
130 };
131
132 using Pos = Cell *;
133
134/*--------------------------------------------------------------------------*/
135
137 List( void );
138
140 List( std::initializer_list< T > items );
141
143 template< std::input_iterator I , std::sentinel_for< I > S >
144 List( I first , S last );
145
147 List( const List & other );
148
150 List( List && other ) noexcept;
151
153 List & operator=( List other ) noexcept;
154
157
160 void swap( List & other ) noexcept;
161
162/*--------------------------------------------------------------------------*/
163/*-------------------------- THE SEQUENCE ----------------------------------*/
164/*--------------------------------------------------------------------------*/
165
167 [[nodiscard]] bool empty( void ) const;
168
171 [[nodiscard]] bool finished( Pos p ) const;
172
174 [[nodiscard]] Pos head( void ) const;
175
177 [[nodiscard]] Pos tail( void ) const;
178
181 [[nodiscard]] Pos next( Pos p ) const;
182
185 [[nodiscard]] Pos prev( Pos p ) const;
186
208 Pos insert( Pos p , const T & v );
209
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 List & o ) const;
242
243/*------------------------- THE STANDARD INTERFACE -------------------------*/
244
245 // a bidirectional iterator walks the cells, the sentinel being the end;
246 // it stays valid until its cell is removed, and a change made through it
247 // bypasses the audits of write()
248
250 template< bool Const >
252 {
253 public:
255 using iterator_category = std::bidirectional_iterator_tag;
256 using value_type = T;
257 using difference_type = std::ptrdiff_t;
259 using reference = std::conditional_t< Const , const T & , T & >;
261 using pointer = std::conditional_t< Const , const T * , T * >;
262
264 Iterator( void ) = default;
265
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.cell() ) { }
274
276 reference operator*( void ) const { return( f_c->value ); }
278 pointer operator->( void ) const { return( &f_c->value ); }
281 f_c = f_c->succ;
282 return( *this );
283 }
284
286 auto t = *this;
287 ++*this;
288 return( t );
289 }
290
292 f_c = f_c->pred;
293 return( *this );
294 }
295
297 auto t = *this;
298 --*this;
299 return( t );
300 }
301
302 bool operator==( const Iterator & o ) const = default;
303
305 [[nodiscard]] Cell * cell( void ) const { return( f_c ); }
306
307 private:
308 Cell * f_c = nullptr;
309 };
310
312 using value_type = T;
314 using size_type = std::size_t;
316 using difference_type = std::ptrdiff_t;
318 using reference = T &;
320 using const_reference = const T &;
322 using iterator = Iterator< false >;
324 using const_iterator = Iterator< true >;
326 using reverse_iterator = std::reverse_iterator< iterator >;
328 using const_reverse_iterator = std::reverse_iterator< const_iterator >;
329
331 [[nodiscard]] iterator begin( void ) { return( iterator( head() ) ); }
333 [[nodiscard]] iterator end( void ) { return( iterator( sentinel() ) ); }
335 [[nodiscard]] const_iterator begin( void ) const {
336 return( const_iterator( head() ) );
337 }
338
339 [[nodiscard]] const_iterator end( void ) const {
340 return( const_iterator( sentinel() ) );
341 }
342
343 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
345 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
347 [[nodiscard]] reverse_iterator rbegin( void ) {
348 return( reverse_iterator( end() ) );
349 }
350
351 [[nodiscard]] reverse_iterator rend( void ) {
352 return( reverse_iterator( begin() ) );
353 }
354
355 [[nodiscard]] const_reverse_iterator rbegin( void ) const {
356 return( const_reverse_iterator( end() ) );
357 }
358
359 [[nodiscard]] const_reverse_iterator rend( void ) const {
360 return( const_reverse_iterator( begin() ) );
361 }
362
364 void push_back( const T & v );
365
367 void push_front( const T & v );
368
369/*------------------------- THE DLList OF MORIN ----------------------------*/
370
372 [[nodiscard]] std::size_t size( void ) const { return( f_size ); }
373
391 [[nodiscard]] Pos position( std::size_t i ) const;
392
395 [[nodiscard]] const T & get( std::size_t i ) const;
396
400 T set( std::size_t i , const T & x );
401
406 void add( std::size_t i , const T & x );
407
412 T erase( std::size_t i );
413
416 void checkSize( void ) const;
417
436 [[nodiscard]] bool isPalindrome( void ) const;
437
454 void rotate( std::size_t r );
455
475 [[nodiscard]] List truncate( std::size_t i );
476
481 void absorb( List & l2 );
482
499 [[nodiscard]] List deal( void );
500
505 void reverse( void );
506
511 void takeFirst( List & l2 );
512
533 template< class C = std::less<> >
534 [[nodiscard]] static List merge( List & l1 , List & l2 , C less = C() );
535
554 template< class C = std::less<> >
555 void mergeSort( C less = C() );
556
557/*--------------------------------------------------------------------------*/
558 private:
560 [[nodiscard]] bool linked( void ) const;
561
564 [[nodiscard]] bool owns( Pos p ) const;
565
567 [[nodiscard]] Cell * sentinel( void ) const;
568
571 void adopt( List & o ) noexcept;
572
575 struct Plain
576 {
578 template< class... Args >
579 Cell * make( Args &&... args ) {
580 return( new Cell{ std::forward< Args >( args )... } );
581 }
583 void free( Cell * c ) noexcept { delete c; }
585 void swap( Plain & ) noexcept { }
586 };
587
589 static void relink( Cell * c , Cell * p ) noexcept;
590
593 template< class C >
594 static Cell * sortChain( Cell * a , std::size_t k , C & less );
595
596 mutable Cell f_sentinel;
597 std::size_t f_size = 0;
600 std::conditional_t< Pooled , ObjectPool< Cell > , Plain > f_pool;
601
602 }; // end( class List )
603
604/*--------------------------------------------------------------------------*/
605
606} // end( namespace ADS )
607
608/*--------------------------------------------------------------------------*/
609/*-------------------------- IMPLEMENTATION --------------------------------*/
610/*--------------------------------------------------------------------------*/
611
612#include "ADS/sequence/impl/List.tpp"
613
614/*--------------------------------------------------------------------------*/
615
616#endif /* List.h included */
617
618/*--------------------------------------------------------------------------*/
619/*---------------------------- End File List.h -----------------------------*/
620/*--------------------------------------------------------------------------*/
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...
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
T value_type
the type of the items
Definition List.h:256
Iterator(Cell *c)
on the item of the cell , the end if is the sentinel
Definition List.h:268
std::conditional_t< Const, const T &, T & > reference
a reference to an item, constant if Const
Definition List.h:259
Iterator & operator++(void)
to the next cell
Definition List.h:280
Iterator & operator--(void)
to the previous cell
Definition List.h:291
Iterator(const Iterator< C > &o)
the const iterator from the other one
Definition List.h:273
std::bidirectional_iterator_tag iterator_category
the category of the iterator, bidirectional
Definition List.h:255
reference operator*(void) const
the item
Definition List.h:276
bool operator==(const Iterator &o) const =default
equal on the same cell
pointer operator->(void) const
the address of the item
Definition List.h:278
std::conditional_t< Const, const T *, T * > pointer
a pointer to an item, constant if Const
Definition List.h:261
Cell * cell(void) const
Definition List.h:305
std::ptrdiff_t difference_type
the type of the distances
Definition List.h:257
Iterator operator++(int)
to the next cell, returning the iterator before the step
Definition List.h:285
Iterator operator--(int)
to the previous cell, returning the iterator before the step
Definition List.h:296
Iterator(void)=default
an iterator on no cell
void write(Pos p, const T &v)
writes in the position
reverse_iterator rbegin(void)
the iterator on the last item, backwards
Definition List.h:347
iterator end(void)
the iterator past the last item
Definition List.h:333
reverse_iterator rend(void)
the iterator before the first item, backwards
Definition List.h:351
T erase(std::size_t i)
removes the item of rank and returns it,
List(std::initializer_list< T > items)
the list of the given items, in their order
void absorb(List &l2)
the items of l2 appended to this list in their order, l2 left empty, in by relinking (exercise 3....
bool operator==(const List &o) const
the same items in the same order
Pos remove(Pos p)
removes the item in the position , returning the position of its successor, which is now in its place
const_reverse_iterator rend(void) const
the constant iterator before the first item, backwards
Definition List.h:359
Iterator< true > const_iterator
Definition List.h:324
bool finished(Pos p) const
true if is or , i.e., the sentinel
Cell * Pos
Definition List.h:132
const Pair & const_reference
Definition List.h:320
List(const List &other)
a copy of other, cell by cell
List deal(void)
the items of odd rank moved, in their order, to the list returned (exercise 3.11 of Morin's book),
List & operator=(List other) noexcept
the items of other, by copy and swap
const_iterator begin(void) const
the constant iterator on the first item
Definition List.h:335
void mergeSort(C less=C())
sorts the list, stable, by the merge sort of exercise 3.13 of Morin's book: the list cut in two halve...
List truncate(std::size_t i)
the items of ranks cut off into the list returned, in by relinking (exercise 3.9 of Morin's book; i...
void reverse(void)
the order of the items reversed, by exchanging the two links of each cell and of the sentinel,...
Pos next(Pos p) const
the position of the item that follows
const_iterator cend(void) const
the constant iterator past the last item
Definition List.h:345
const_iterator end(void) const
the constant iterator past the last item
Definition List.h:339
List(List &&other) noexcept
the cells of other, which is left empty, in time
std::size_t size(void) const
the number of items, kept
Definition List.h:372
void add(std::size_t i, const T &x)
put at the rank , before the cell of rank ,
bool isPalindrome(void) const
true if the item of rank equals that of rank for every , by two walks from the two ends,...
static List merge(List &l1, List &l2, C less=C())
the merge of the two sorted lists l1 and l2, which are left empty, stable (the items of l1 first amon...
Pair & reference
Definition List.h:318
Pair value_type
Definition List.h:312
const_iterator cbegin(void) const
the constant iterator on the first item
Definition List.h:343
Pos head(void) const
the position of the first item
std::reverse_iterator< iterator > reverse_iterator
Definition List.h:326
Pos tail(void) const
the position of the last item
Pos insert(Pos p, const T &v)
puts in the position , i.e., before the item in , and returns its position
std::size_t size_type
Definition List.h:314
Pair Item
Definition List.h:122
const T & get(std::size_t i) const
the item of rank ,
void push_front(const T &v)
at the front, for std::front_inserter
Pos position(std::size_t i) const
the cell of rank , the sentinel for , reached from the nearer end in (Morin's getNode( i ))
void swap(List &other) noexcept
exchanges the cells with other, relinking them to the sentinels in time
bool empty(void) const
true if the list is empty, i.e., the sentinel is its own successor
const_reverse_iterator rbegin(void) const
the constant iterator on the last item, backwards
Definition List.h:355
List(I first, S last)
the list of the items of [ first , last ), in their order
void checkSize(void) const
counts the cells and throws std::logic_error if they are not the size kept (exercise 3....
const T & read(Pos p) const
the item in the position
void push_back(const T &v)
at the end, for std::back_inserter
List(void)
the empty list: the sentinel alone, its own predecessor and successor
Iterator< false > iterator
Definition List.h:322
void takeFirst(List &l2)
the first cell of l2 moved to the end of this list, by relinking (a new cell with the item moved if P...
void rotate(std::size_t r)
the item of rank moved to the rank by moving the sentinel before the item of rank ,...
Pos prev(Pos p) const
the position of the item that precedes
~List()
frees the cells
iterator begin(void)
the iterator on the first item
Definition List.h:331
std::ptrdiff_t difference_type
Definition List.h:316
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition List.h:328
T set(std::size_t i, const T &x)
written at the rank , the old item returned,
#define ADS_NO_UNIQUE_ADDRESS
an empty member that takes no storage, by the attribute that does so (the standard one is accepted bu...
Definition Portable.h:81
Definition ADT.h:96
a cell of the list: the sentinel is a Cell with no meaningful value
Definition List.h:126
Cell * pred
the predecessor
Definition List.h:127
T value
the item
Definition List.h:129
Cell * succ
the successor
Definition List.h:128