Class CellHeap

Nested Relationships

Nested Types

Class Documentation

class CellHeap

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

Public Types

using Value = long

a field: nil, an atom (atom( k ) for k >= 0) or a cell (its address)

Public Functions

explicit CellHeap(std::size_t n)

a heap of n cells, all free

Pre:

n >= 1

Value cons(Value car, Value cdr)

a new cell with the fields car and cdr, after a collection if no cell is free

Pre:

a cell is free after the collection

Value car(Value c) const
Pre:

c is a cell of the heap

Value cdr(Value c) const
void setCar(Value c, Value v)
void setCdr(Value c, Value v)
void addRoot(Value c)

c is a root

void removeRoot(Value c)
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 cells freed

Post:

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

std::size_t freeCells(void) const

Public Static Functions

static Value atom(long k)

the atom k

Pre:

k >= 0

static bool isCell(Value v)

Public Static Attributes

static constexpr Value nil = -1