ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
List.h File Reference

The LIST of the book: a SEQUENCE [see ADT.h] realized as a bidirectional circular list of cells with a sentinel. More...

#include "ADS/Contract.h"
#include "ADS/Portable.h"
#include "ADS/memory/ObjectPool.h"
#include <cstddef>
#include <functional>
#include <initializer_list>
#include <iterator>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <utility>
#include "ADS/sequence/impl/List.tpp"

Go to the source code of this file.

Classes

class  ADS::List< T, Pooled >
 a SEQUENCE realized as a bidirectional circular list with a sentinel, its cells from a pool if Pooled More...
struct  ADS::List< T, Pooled >::Cell
 a cell of the list: the sentinel is a Cell with no meaningful value More...
class  ADS::List< T, Pooled >::Iterator< Const >
 the iterator, on items const if Const More...

Namespaces

namespace  ADS

Detailed description

The LIST of the book: a SEQUENCE [see ADT.h] realized as a bidirectional circular list of cells with a sentinel.

A list of \( n \) items has \( n + 1 \) cells, the sentinel holding no item; the position \( \mathit{pos}_i \) is the address of the cell of the item of index \( i \), for \( 0 \leq i < n \), while \( \mathit{pos}_{-1} \) and \( \mathit{pos}_n \) are both the address of the sentinel, which is therefore what finished() looks for. Every operation takes \( O( 1 ) \) time, the list \( \Theta( n ) \) space.

The sentinel is a member of the list rather than a cell of the heap, so that moving a list relinks its first and last cells to the new sentinel in \( O( 1 ) \) time and allocates nothing: the list moved from is left empty, and still usable. Copying a list copies its cells (the rule of five, with the copy and swap idiom for the assignment). 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(): 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 a block at a time; the price is that the list keeps the memory of its largest size until it dies, and that a move takes the pool along with the cells. A pool is no gain for a list of a few cells, which still takes a block per cell and a header per block: a structure of many short lists (the chains of ChainedHashTable) takes List< T , false >, whose cells come one by one from new and delete.

Against Morin's DLList, the claims for the benchmarks: the cells come from the pool, with no call to the allocator after the first ones and next to each other in memory, where Morin's are each a new and a delete; position() walks from the nearer end as Morin's getNode().

The contracts [see Contract.h]: a position is a cell of this list, which costs a scan of the list and is therefore an audit, checked by the operations that change the list (insert(), remove() and write()) but not by next(), prev() and read(), where it would make every scan quadratic; read(), write() and remove() need a position that holds an item, i.e., not the sentinel; insert() and write() ensure that \( v \) is read back in its position. The invariant is that each cell is the predecessor of its successor, which also makes the walk from the sentinel come back to it. The operations are defined in List.tpp.

It is also the DLList of section 3.2 of Morin's book: the list keeps its size, and has the operations by rank of a LIST [see IndexedList.h], get( i ), set( i , x ), add( i , x ) and erase( i ), which reach the cell of rank \( i \) from the nearer end (position( i ), Morin's getNode( i )) in \( O( 1 + \min\{ i , n - i \} ) \) time, and those of the exercises of chapter 3 of Morin: checkSize() (3.5), insert(), which is the addBefore() of exercise 3.6, isPalindrome() (3.7), rotate(), which moves the sentinel and no item (3.8), truncate() (3.9), absorb() (3.10), deal() (3.11), reverse() (3.12), and takeFirst(), merge() and mergeSort() (3.13). The operations that move cells from a list to another relink them when the cells come from new (Pooled false), as Morin's do; between pooled lists a cell cannot change pool, so that truncate(), deal(), takeFirst() and merge() move the items into cells of the other list (in time linear in the items moved), while absorb() takes the pool of the other list along with its cells [see ObjectPool::adopt()], in \( O( \log n ) \), and mergeSort() and reverse() relink the cells of one list in both cases.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, section 4.1, https://algoritmicamente.it/materiali/libro/capitoli/cap04.pdf
B. Stroustrup, The C++ Programming Language, 4th ed., Addison-Wesley, 2013, section 17.5 (copy and move)
P. Morin, Open Data Structures (in C++), Edition 0.1G, 2013, https://opendatastructures.org/ods-cpp.pdf, section 3.2 and exercises 3.5 to 3.13
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa