|
ADS
Algorithms and Data Structures
|
a heap of cells of two fields, with a mark-and-sweep collector More...
#include <ADS/memory/CellHeap.h>
Public types | |
| using | Value = long |
| a field: nil, an atom (atom( k ), \( -2 - k \), for \( k \ge 0 \)) or a cell (its address, \( \ge 0 \)) | |
Public member functions | |
| CellHeap (std::size_t n) | |
| the heap of \( n \) cells, all free | |
| Value | cons (Value car, Value cdr) |
| a new cell with the fields car and cdr, after a collection if no cell is free | |
| Value | car (Value c) const |
| the field car of the cell \( c \) | |
| Value | cdr (Value c) const |
| the field cdr of the cell \( c \) | |
| void | setCar (Value c, Value v) |
| the field car of the cell \( c \) set to \( v \) | |
| void | setCdr (Value c, Value v) |
| the field cdr of the cell \( c \) set to \( v \) | |
| void | addRoot (Value c) |
| the cell \( c \) made a root, once more | |
| void | removeRoot (Value c) |
| one of the roots \( c \) removed, if there is one | |
| std::size_t | collect (bool reversal=true) |
| marks the cells reachable from the roots, by pointer reversal or with a stack, and frees the others; the number of the cells freed | |
| std::size_t | freeCells (void) const |
| the number of the free cells | |
Static public member functions | |
| static Value | atom (long k) |
| the atom \( k \) | |
| static bool | isCell (Value v) |
| whether \( v \) is the address of a cell | |
Static public attributes | |
| static constexpr Value | nil = -1 |
| the empty field | |
a heap of cells of two fields, with a mark-and-sweep collector
| using ADS::CellHeap::Value = long |
a field: nil, an atom (atom( k ), \( -2 - k \), for \( k \ge 0 \)) or a cell (its address, \( \ge 0 \))
|
explicit |
the heap of \( n \) cells, all free
| void ADS::CellHeap::addRoot | ( | Value | c | ) |
the cell \( c \) made a root, once more
|
static |
the atom \( k \)
the field car of the cell \( c \)
the field cdr of the cell \( c \)
| std::size_t ADS::CellHeap::collect | ( | bool | reversal = true | ) |
marks the cells reachable from the roots, by pointer reversal or with a stack, and frees the others; the number of the cells freed
In pseudocode (Algorithm 2.3.5E of TAOCP, the marking of Deutsch, Schorr and Waite, beyond the book), \( \mathit{side}( x ) \) true when the reversed pointer of the cell \( x \) is in its \( \mathit{cdr} \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{collect}() \cr 2 & \quad \textbf{foreach}\ \text{root } r\ \textbf{do} \ \mathrm{mark}( r ) \cr 3 & \quad F \gets \textbf{nil} \cr 4 & \quad \textbf{for}\ i \gets n - 1\ \textbf{downto}\ 0 \ \textbf{do}\quad \triangleright\ \text{the sweep} \cr 5 & \qquad \textbf{if}\ i \text{ is marked}\ \textbf{then}\ \text{unmark} \text{ } i\ \textbf{else}\ i.\mathit{car} \gets F;\ F \gets i \cr 6 & \quad \textbf{return}\ \text{the cells freed now} \cr 7 & \textbf{procedure}\ \mathrm{mark}( c ) \cr 8 & \quad p \gets \textbf{nil} \cr 9 & \quad \textbf{loop} \cr 10 & \qquad \textbf{while}\ c \text{ is a cell not marked}\ \textbf{do} \quad \triangleright\ \text{down the car} \cr 11 & \qquad\quad \text{mark } c;\ \mathit{side}( c ) \gets \textbf{false} \cr 12 & \qquad\quad ( c.\mathit{car} , p , c ) \gets ( p , c , c.\mathit{car} ) \cr 13 & \qquad \textbf{while}\ p \ne \textbf{nil}\ \textbf{and} \ \mathit{side}( p )\ \textbf{do}\quad \triangleright\ \text{up from} \text{ a cdr} \cr 14 & \qquad\quad ( p.\mathit{cdr} , c , p ) \gets ( c , p , p.\mathit{cdr} ) \cr 15 & \qquad \textbf{if}\ p = \textbf{nil}\ \textbf{then return} \cr 16 & \qquad \mathit{side}( p ) \gets \textbf{true}\quad \triangleright\ \text{from the car to the cdr} \cr 17 & \qquad ( p.\mathit{car} , p.\mathit{cdr} , c ) \gets ( c , p.\mathit{car} , p.\mathit{cdr} ) \end{array}} \]
a new cell with the fields car and cdr, after a collection if no cell is free
In pseudocode (section 2.3.5 of TAOCP, beyond the book), \( F \) the free list, linked through the field \( \mathit{car} \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{cons}( a , d ) \cr 2 & \quad \textbf{if}\ F = \textbf{nil}\ \textbf{then} \ \mathrm{collect}() \cr 3 & \quad c \gets F;\ F \gets c.\mathit{car} \cr 4 & \quad c.\mathit{car} \gets a;\ c.\mathit{cdr} \gets d; \ \textbf{return}\ c \end{array}} \]
|
nodiscard |
the number of the free cells
|
staticnodiscard |
whether \( v \) is the address of a cell
| void ADS::CellHeap::removeRoot | ( | Value | c | ) |
one of the roots \( c \) removed, if there is one
the field car of the cell \( c \) set to \( v \)
the field cdr of the cell \( c \) set to \( v \)
|
staticconstexpr |
the empty field