|
ADS
Algorithms and Data Structures
|
a SEQUENCE realized as a bidirectional circular list with a sentinel, its cells from a pool if Pooled More...
#include <ADS/sequence/List.h>
Classes | |
| struct | Cell |
| a cell of the list: the sentinel is a Cell with no meaningful value More... | |
| class | Iterator |
| the iterator, on items const if Const More... | |
Public types | |
| using | Item = T |
| the type of the items | |
| using | Pos = Cell * |
| a position is the address of a cell | |
| using | value_type = T |
| the type of the items | |
| using | size_type = std::size_t |
| the type of the sizes | |
| using | difference_type = std::ptrdiff_t |
| the type of the distances | |
| using | reference = T & |
| a reference to an item | |
| using | const_reference = const T & |
| a constant reference to an item | |
| using | iterator = Iterator< false > |
| the iterator on the items | |
| using | const_iterator = Iterator< true > |
| the iterator on the constant items | |
| using | reverse_iterator = std::reverse_iterator< iterator > |
| the iterator on the items, backwards | |
| using | const_reverse_iterator = std::reverse_iterator< const_iterator > |
| the iterator on the constant items, backwards | |
Public member functions | |
| List (void) | |
| the empty list: the sentinel alone, its own predecessor and successor | |
| List (std::initializer_list< T > items) | |
| the list of the given items, in their order | |
| template<std::input_iterator I, std::sentinel_for< I > S> | |
| List (I first, S last) | |
| the list of the items of [ first , last ), in their order | |
| List (const List &other) | |
| a copy of other, cell by cell | |
| List (List &&other) noexcept | |
| the cells of other, which is left empty, in \( O( 1 ) \) time | |
| List & | operator= (List other) noexcept |
| the items of other, by copy and swap | |
| ~List () | |
| frees the cells | |
| void | swap (List &other) noexcept |
| exchanges the cells with other, relinking them to the sentinels in \( O( 1 ) \) time | |
| bool | empty (void) const |
| true if the list is empty, i.e., the sentinel is its own successor | |
| bool | finished (Pos p) const |
| true if \( p \) is \( \mathit{pos}_{-1} \) or \( \mathit{pos}_n \), i.e., the sentinel | |
| Pos | head (void) const |
| the position of the first item | |
| Pos | tail (void) const |
| the position of the last item | |
| Pos | next (Pos p) const |
| the position of the item that follows \( p \) | |
| Pos | prev (Pos p) const |
| the position of the item that precedes \( p \) | |
| Pos | insert (Pos p, const T &v) |
| puts \( v \) in the position \( p \), i.e., before the item in \( p \), and returns its position | |
| Pos | remove (Pos p) |
| removes the item in the position \( p \), returning the position of its successor, which is now in its place | |
| const T & | read (Pos p) const |
| the item in the position \( p \) | |
| void | write (Pos p, const T &v) |
| writes \( v \) in the position \( p \) | |
| bool | operator== (const List &o) const |
| the same items in the same order | |
| iterator | begin (void) |
| the iterator on the first item | |
| iterator | end (void) |
| the iterator past the last item | |
| const_iterator | begin (void) const |
| the constant iterator on the first item | |
| const_iterator | end (void) const |
| the constant iterator past the last item | |
| const_iterator | cbegin (void) const |
| the constant iterator on the first item | |
| const_iterator | cend (void) const |
| the constant iterator past the last item | |
| reverse_iterator | rbegin (void) |
| the iterator on the last item, backwards | |
| reverse_iterator | rend (void) |
| the iterator before the first item, backwards | |
| const_reverse_iterator | rbegin (void) const |
| the constant iterator on the last item, backwards | |
| const_reverse_iterator | rend (void) const |
| the constant iterator before the first item, backwards | |
| void | push_back (const T &v) |
| \( v \) at the end, for std::back_inserter | |
| void | push_front (const T &v) |
| \( v \) at the front, for std::front_inserter | |
| std::size_t | size (void) const |
| the number \( n \) of items, kept | |
| Pos | position (std::size_t i) const |
| the cell of rank \( i \), the sentinel for \( i = n \), reached from the nearer end in \( O( 1 + \min\{ i , n - i \} ) \) (Morin's getNode( i )) | |
| const T & | get (std::size_t i) const |
| the item of rank \( i \), \( O( 1 + \min\{ i , n - i \} ) \) | |
| T | set (std::size_t i, const T &x) |
| \( x \) written at the rank \( i \), the old item returned, \( O( 1 + \min\{ i , n - i \} ) \) | |
| void | add (std::size_t i, const T &x) |
| \( x \) put at the rank \( i \), before the cell of rank \( i \), \( O( 1 + \min\{ i , n - i \} ) \) | |
| T | erase (std::size_t i) |
| removes the item of rank \( i \) and returns it, \( O( 1 + \min\{ i , n - i \} ) \) | |
| void | checkSize (void) const |
| counts the cells and throws std::logic_error if they are not the size kept (exercise 3.5 of Morin's book), \( O( n ) \) | |
| bool | isPalindrome (void) const |
| true if the item of rank \( i \) equals that of rank \( n - 1 - i \) for every \( i \), by two walks from the two ends, \( \lfloor n / 2 \rfloor \) comparisons at most (exercise 3.7 of Morin's book) | |
| void | rotate (std::size_t r) |
| the item of rank \( i \) moved to the rank \( ( i + r ) \bmod n \) by moving the sentinel before the item of rank \( n - r \), in \( O( 1 + \min\{ r , n - r \} ) \), no item moved and no cell made (exercise 3.8 of Morin's book) | |
| List | truncate (std::size_t i) |
| the items of ranks \( i , \ldots , n - 1 \) cut off into the list returned, in \( O( 1 + \min\{ i , n - i \} ) \) by relinking (exercise 3.9 of Morin's book; in \( O( 1 + n - i ) \) by moving the items if Pooled) | |
| void | absorb (List &l2) |
| the items of l2 appended to this list in their order, l2 left empty, in \( O( 1 ) \) by relinking (exercise 3.10 of Morin's book; in \( O( \log n ) \) if Pooled, the pool of l2 taken) | |
| List | deal (void) |
| the items of odd rank moved, in their order, to the list returned (exercise 3.11 of Morin's book), \( O( n ) \) | |
| void | reverse (void) |
| the order of the items reversed, by exchanging the two links of each cell and of the sentinel, \( O( n ) \) (exercise 3.12 of Morin's book) | |
| 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 Pooled), \( O( 1 ) \) (exercise 3.13 of Morin's book) | |
| template<class C = std::less<>> | |
| 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 halves by a walk to its middle, each sorted recursively and the two merged, all on the cells of this list, which are relinked and never copied, \( O( n \log n ) \) time and \( O( \log n ) \) space for the recursion [see ListSort.h for the natural merge sort of the runs, bottom up] | |
Static public member functions | |
| template<class C = std::less<>> | |
| 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 among equal ones), in \( O( |l1| + |l2| ) \) (exercise 3.13 of Morin's book) | |
a SEQUENCE realized as a bidirectional circular list with a sentinel, its cells from a pool if Pooled
| using ADS::List< T, Pooled >::const_iterator = Iterator< true > |
the iterator on the constant items
| using ADS::List< T, Pooled >::const_reference = const T & |
a constant reference to an item
| using ADS::List< T, Pooled >::const_reverse_iterator = std::reverse_iterator< const_iterator > |
the iterator on the constant items, backwards
| using ADS::List< T, Pooled >::difference_type = std::ptrdiff_t |
the type of the distances
| using ADS::List< T, Pooled >::Item = T |
the type of the items
the iterator on the items
a position is the address of a cell
| using ADS::List< T, Pooled >::reference = T & |
a reference to an item
| using ADS::List< T, Pooled >::reverse_iterator = std::reverse_iterator< iterator > |
the iterator on the items, backwards
| using ADS::List< T, Pooled >::size_type = std::size_t |
the type of the sizes
| using ADS::List< T, Pooled >::value_type = T |
the type of the items
| ADS::List< T, Pooled >::List | ( | void | ) |
the empty list: the sentinel alone, its own predecessor and successor
| ADS::List< T, Pooled >::List | ( | std::initializer_list< T > | items | ) |
the list of the given items, in their order
| ADS::List< T, Pooled >::List | ( | I | first, |
| S | last ) |
the list of the items of [ first , last ), in their order
| ADS::List< T, Pooled >::List | ( | const List< T, Pooled > & | other | ) |
a copy of other, cell by cell
|
noexcept |
the cells of other, which is left empty, in \( O( 1 ) \) time
the items of l2 appended to this list in their order, l2 left empty, in \( O( 1 ) \) by relinking (exercise 3.10 of Morin's book; in \( O( \log n ) \) if Pooled, the pool of l2 taken)
| void ADS::List< T, Pooled >::add | ( | std::size_t | i, |
| const T & | x ) |
\( x \) put at the rank \( i \), before the cell of rank \( i \), \( O( 1 + \min\{ i , n - i \} ) \)
|
inlinenodiscard |
the iterator on the first item
|
inlinenodiscard |
the constant iterator on the first item
|
inlinenodiscard |
the constant iterator on the first item
|
inlinenodiscard |
the constant iterator past the last item
| void ADS::List< T, Pooled >::checkSize | ( | void | ) | const |
counts the cells and throws std::logic_error if they are not the size kept (exercise 3.5 of Morin's book), \( O( n ) \)
the items of odd rank moved, in their order, to the list returned (exercise 3.11 of Morin's book), \( O( n ) \)
In pseudocode (Morin, exercise 3.11):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{deal}() \cr 2 & \quad L \gets \text{an empty list};\ p \gets s.\mathit{succ} \cr 3 & \quad \textbf{while}\ p \ne s\ \textbf{and}\ p.\mathit{succ} \ne s\ \textbf{do} \cr 4 & \qquad q \gets p.\mathit{succ};\ \text{unlink } q;\ \text{link } q \text{ at the end of } L;\ p \gets p.\mathit{succ} \cr 5 & \quad \textbf{return}\ L \end{array}} \]
|
nodiscard |
true if the list is empty, i.e., the sentinel is its own successor
the iterator past the last item
|
inlinenodiscard |
the constant iterator past the last item
| T ADS::List< T, Pooled >::erase | ( | std::size_t | i | ) |
removes the item of rank \( i \) and returns it, \( O( 1 + \min\{ i , n - i \} ) \)
|
nodiscard |
true if \( p \) is \( \mathit{pos}_{-1} \) or \( \mathit{pos}_n \), i.e., the sentinel
|
nodiscard |
the item of rank \( i \), \( O( 1 + \min\{ i , n - i \} ) \)
the position of the first item
puts \( v \) in the position \( p \), i.e., before the item in \( p \), and returns its position
In pseudocode (section 4.1), a new cell \( t \) linked between \( p \) and its predecessor, which the sentinel guarantees:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{insert}( p , v )\quad \triangleright \ \text{before the cell } p \cr 2 & \quad t \gets \text{a new cell of value } v;\ q \gets p.\mathit{pred} \cr 3 & \quad t.\mathit{succ} \gets p;\ t.\mathit{pred} \gets q \cr 4 & \quad q.\mathit{succ} \gets t;\ p.\mathit{pred} \gets t\quad \triangleright\ \text{the sentinel if } q \text{ or } p \text{ is none} \cr 5 & \quad \textbf{return}\ t \end{array}} \]
|
nodiscard |
true if the item of rank \( i \) equals that of rank \( n - 1 - i \) for every \( i \), by two walks from the two ends, \( \lfloor n / 2 \rfloor \) comparisons at most (exercise 3.7 of Morin's book)
In pseudocode (Morin, exercise 3.7):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{isPalindrome}() \cr 2 & \quad p \gets s.\mathit{succ};\ q \gets s.\mathit{pred} \cr 3 & \quad \textbf{for}\ k \gets 1\ \textbf{to}\ \lfloor n / 2 \rfloor\ \textbf{do} \cr 4 & \qquad \textbf{if}\ p.\mathit{value} \ne q.\mathit{value} \ \textbf{then return false} \cr 5 & \qquad p \gets p.\mathit{succ};\ q \gets q.\mathit{pred} \cr 6 & \quad \textbf{return true} \end{array}} \]
|
staticnodiscard |
the merge of the two sorted lists l1 and l2, which are left empty, stable (the items of l1 first among equal ones), in \( O( |l1| + |l2| ) \) (exercise 3.13 of Morin's book)
In pseudocode (Morin, exercise 3.13):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{merge}( l_1 , l_2 ) \cr 2 & \quad L \gets \text{an empty list} \cr 3 & \quad \textbf{while}\ l_1 \ne \emptyset\ \textbf{and}\ l_2 \ne \emptyset\ \textbf{do} \cr 4 & \qquad \textbf{if}\ \mathrm{first}( l_2 ) < \mathrm{first}( l_1 ) \ \textbf{then}\ L.\mathrm{takeFirst}( l_2 )\ \textbf{else} \ L.\mathrm{takeFirst}( l_1 ) \cr 5 & \quad L.\mathrm{absorb}( l_1 );\ L.\mathrm{absorb}( l_2 ); \ \textbf{return}\ L \end{array}} \]
| void ADS::List< T, Pooled >::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 halves by a walk to its middle, each sorted recursively and the two merged, all on the cells of this list, which are relinked and never copied, \( O( n \log n ) \) time and \( O( \log n ) \) space for the recursion [see ListSort.h for the natural merge sort of the runs, bottom up]
In pseudocode (Morin, exercise 3.13):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{mergeSort}( L ) \cr 2 & \quad \textbf{if}\ |L| \le 1\ \textbf{then return} \cr 3 & \quad R \gets L.\mathrm{truncate}( \lfloor |L| / 2 \rfloor ) \cr 4 & \quad \mathrm{mergeSort}( L );\ \mathrm{mergeSort}( R ) \cr 5 & \quad L \gets \mathrm{merge}( L , R ) \end{array}} \]
the position of the item that follows \( p \)
|
noexcept |
the items of other, by copy and swap
|
nodiscard |
the same items in the same order
|
nodiscard |
the cell of rank \( i \), the sentinel for \( i = n \), reached from the nearer end in \( O( 1 + \min\{ i , n - i \} ) \) (Morin's getNode( i ))
In pseudocode (Morin, section 3.2), \( s \) the sentinel:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{position}( i ) \cr 2 & \quad \textbf{if}\ i < n / 2\ \textbf{then}\ p \gets s.\mathit{succ};\ \textbf{repeat}\ i\ \text{ times: } p \gets p.\mathit{succ} \cr 3 & \quad \textbf{else}\ p \gets s;\ \textbf{repeat}\ n - i \ \text{ times: } p \gets p.\mathit{pred} \cr 4 & \quad \textbf{return}\ p \end{array}} \]
the position of the item that precedes \( p \)
| void ADS::List< T, Pooled >::push_back | ( | const T & | v | ) |
\( v \) at the end, for std::back_inserter
| void ADS::List< T, Pooled >::push_front | ( | const T & | v | ) |
\( v \) at the front, for std::front_inserter
|
inlinenodiscard |
the iterator on the last item, backwards
|
inlinenodiscard |
the constant iterator on the last item, backwards
|
nodiscard |
the item in the position \( p \)
removes the item in the position \( p \), returning the position of its successor, which is now in its place
In pseudocode (section 4.1), the cell unlinked from its two neighbors:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{remove}( p ) \cr 2 & \quad q \gets p.\mathit{pred};\ t \gets p.\mathit{succ} \cr 3 & \quad q.\mathit{succ} \gets t;\ t.\mathit{pred} \gets q\quad \triangleright\ p \text{ is out of the chain} \cr 4 & \quad \text{free the cell } p;\ \textbf{return}\ t \end{array}} \]
|
inlinenodiscard |
the iterator before the first item, backwards
|
inlinenodiscard |
the constant iterator before the first item, backwards
| void ADS::List< T, Pooled >::reverse | ( | void | ) |
the order of the items reversed, by exchanging the two links of each cell and of the sentinel, \( O( n ) \) (exercise 3.12 of Morin's book)
| void ADS::List< T, Pooled >::rotate | ( | std::size_t | r | ) |
the item of rank \( i \) moved to the rank \( ( i + r ) \bmod n \) by moving the sentinel before the item of rank \( n - r \), in \( O( 1 + \min\{ r , n - r \} ) \), no item moved and no cell made (exercise 3.8 of Morin's book)
In pseudocode (Morin, exercise 3.8), \( r \) reduced modulo \( n \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{rotate}( r ) \cr 2 & \quad \textbf{if}\ r = 0\ \textbf{then return} \cr 3 & \quad u \gets \mathrm{position}( n - r ) \cr 4 & \quad \text{unlink } s;\ \text{link } s \text{ between } u.\mathit{pred} \text{ and } u \end{array}} \]
| T ADS::List< T, Pooled >::set | ( | std::size_t | i, |
| const T & | x ) |
\( x \) written at the rank \( i \), the old item returned, \( O( 1 + \min\{ i , n - i \} ) \)
|
inlinenodiscard |
the number \( n \) of items, kept
|
noexcept |
exchanges the cells with other, relinking them to the sentinels in \( O( 1 ) \) time
the position of the last item
| void ADS::List< T, Pooled >::takeFirst | ( | List< T, Pooled > & | l2 | ) |
the first cell of l2 moved to the end of this list, by relinking (a new cell with the item moved if Pooled), \( O( 1 ) \) (exercise 3.13 of Morin's book)
|
nodiscard |
the items of ranks \( i , \ldots , n - 1 \) cut off into the list returned, in \( O( 1 + \min\{ i , n - i \} ) \) by relinking (exercise 3.9 of Morin's book; in \( O( 1 + n - i ) \) by moving the items if Pooled)
In pseudocode (Morin, exercise 3.9):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{truncate}( i ) \cr 2 & \quad u \gets \mathrm{position}( i );\ L \gets \text{an empty list} \cr 3 & \quad \textbf{if}\ u \ne s\ \textbf{then}\ \text{link } u , \ldots , s.\mathit{pred} \text{ between the ends of } L;\ \text{link } u.\mathit{pred} \text{ to } s \cr 4 & \quad |L| \gets n - i;\ n \gets i;\ \textbf{return}\ L \end{array}} \]
writes \( v \) in the position \( p \)