ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::CellHeap Class Reference

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

Detailed description

a heap of cells of two fields, with a mark-and-sweep collector

Member typedef documentation

◆ Value

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 \))

Constructor and destructor documentation

◆ CellHeap()

ADS::CellHeap::CellHeap ( std::size_t n)
explicit

the heap of \( n \) cells, all free

Precondition
\( n \ge 1 \)

Member function documentation

◆ addRoot()

void ADS::CellHeap::addRoot ( Value c)

the cell \( c \) made a root, once more

Precondition
\( c \) is a cell of the heap, not free

◆ atom()

Value ADS::CellHeap::atom ( long k)
static

the atom \( k \)

Precondition
\( k \ge 0 \)

◆ car()

Value ADS::CellHeap::car ( Value c) const
nodiscard

the field car of the cell \( c \)

Precondition
\( c \) is a cell of the heap, not free

◆ cdr()

Value ADS::CellHeap::cdr ( Value c) const
nodiscard

the field cdr of the cell \( c \)

Precondition
\( c \) is a cell of the heap, not free

◆ collect()

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}} \]

Postcondition
the cells freed are the unreachable ones, the fields of the others unchanged (audit)

◆ cons()

Value ADS::CellHeap::cons ( Value car,
Value cdr )

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}} \]

Precondition
a cell is free after the collection

◆ freeCells()

std::size_t ADS::CellHeap::freeCells ( void ) const
nodiscard

the number of the free cells

◆ isCell()

bool ADS::CellHeap::isCell ( Value v)
staticnodiscard

whether \( v \) is the address of a cell

◆ removeRoot()

void ADS::CellHeap::removeRoot ( Value c)

one of the roots \( c \) removed, if there is one

◆ setCar()

void ADS::CellHeap::setCar ( Value c,
Value v )

the field car of the cell \( c \) set to \( v \)

Precondition
\( c \) is a cell of the heap, not free

◆ setCdr()

void ADS::CellHeap::setCdr ( Value c,
Value v )

the field cdr of the cell \( c \) set to \( v \)

Precondition
\( c \) is a cell of the heap, not free

Member data documentation

◆ nil

Value ADS::CellHeap::nil = -1
staticconstexpr

the empty field


The documentation for this class was generated from the following file: