|
ADS
Algorithms and Data Structures
|
A SEQUENCE [see ADT.h] realized as a singly linked list, not circular and with no sentinel (exercise 4.6 of the book). More...
#include "ADS/Contract.h"#include "ADS/memory/ObjectPool.h"#include <cstddef>#include <initializer_list>#include <iterator>#include <stdexcept>#include <string>#include <type_traits>#include <utility>#include "ADS/sequence/impl/SinglyList.tpp"Go to the source code of this file.
Classes | |
| class | ADS::SinglyList< T > |
| a SEQUENCE realized as a singly linked list with no sentinel More... | |
| struct | ADS::SinglyList< T >::Cell |
| a cell of the list More... | |
| class | ADS::SinglyList< T >::Iterator< Const > |
| the iterator, on items const if Const More... | |
Namespaces | |
| namespace | ADS |
A SEQUENCE [see ADT.h] realized as a singly linked list, not circular and with no sentinel (exercise 4.6 of the book).
The position \( \mathit{pos}_i \) is the address of the cell of \( s_{i-1} \) for \( i > 0 \) and nil for \( i = 0 \), so that the cell before an item, which insert() and remove() change, is always at hand: every operation takes \( O( 1 ) \) time but tail() and prev(), which scan the list and take \( O( n ) \). \( \mathit{pos}_n \) is the address of the last cell (nil if the list is empty), and \( \mathit{pos}_{-1} \) a distinguished address of the list itself. This is the convention of the realizations of a list with pointers or with cursors in which a position is the cell that precedes the item, e.g., in the book by Aho, Hopcroft and Ullman.
The contracts [see Contract.h]: read(), write() and remove() need a position that holds an item, i.e., one that is not finished(); insert() and write() ensure that \( v \) is read back in its position. A list is copied cell by cell and moved in \( O( 1 ) \) time, the one moved from left empty. Beyond the book, the cells come from an ObjectPool [see ObjectPool.h] of the list, which takes back the cell of remove() for the next insert(), so that a cell costs no call to the allocator, the cells made one after another are next to each other in memory, and a list of items with a trivial destructor dies in one step per block; the price is that the list keeps the memory of its largest size until it dies. The operations are defined in SinglyList.tpp.
Against Morin's SLList, the claim for the benchmarks: the cells come from the pool, where Morin's are each a new and a delete.
It is also the SLList of section 3.1 of Morin's book: the list keeps its last cell and its size, so that push_back() (Morin's add( x )), push_front() and pop_front() (his push( x ), pop() and remove()) and size() take \( O( 1 ) \) time, and it has the operations of the exercises of chapter 3 of Morin: get( i ), set( i , x ), add( i , x ) and erase( i ) by rank in \( O( 1 + i ) \) (exercise 3.3), secondLast() by a walk that needs neither the size nor the last cell (exercise 3.2), reverse() in place by relinking the cells (exercise 3.4) and checkSize(), which counts the cells against the size (exercise 3.5). A dummy cell before the first one cannot remove the special cases of the queue as it does for a doubly linked list (exercise 3.1): the operations at the back need the cell before the last one to remove it, which a singly linked list does not reach in \( O( 1 ) \), and an empty list must still set the last cell apart from the dummy.