ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
CellHeap.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File CellHeap.h -----------------------------*/
3/*--------------------------------------------------------------------------*/
38/*--------------------------------------------------------------------------*/
39/*----------------------------- DEFINITIONS --------------------------------*/
40/*--------------------------------------------------------------------------*/
41
42#ifndef __ADS_CellHeap
43 #define __ADS_CellHeap
44/* self-identification: #endif constant */
45
46/*--------------------------------------------------------------------------*/
47/*------------------------------ INCLUDES ----------------------------------*/
48/*--------------------------------------------------------------------------*/
49
50#include "ADS/Contract.h"
51
52#include <cstddef>
53#include <set>
54#include <vector>
55
56/*--------------------------------------------------------------------------*/
57/*--------------------------- NAMESPACE ------------------------------------*/
58/*--------------------------------------------------------------------------*/
59
60namespace ADS
61{
62
63/*--------------------------------------------------------------------------*/
64/*---------------------------- CLASS CellHeap ------------------------------*/
65/*--------------------------------------------------------------------------*/
66
69{
70
71/*--------------------------------------------------------------------------*/
72 public:
75 using Value = long;
76
78 static constexpr Value nil = -1;
79
82 static Value atom( long k );
83
85 [[nodiscard]] static bool isCell( Value v );
86
89 explicit CellHeap( std::size_t n );
90
108
111 [[nodiscard]] Value car( Value c ) const;
112
115 [[nodiscard]] Value cdr( Value c ) const;
116
119 void setCar( Value c , Value v );
120
123 void setCdr( Value c , Value v );
124
127 void addRoot( Value c );
128
130 void removeRoot( Value c );
131
173 std::size_t collect( bool reversal = true );
174
176 [[nodiscard]] std::size_t freeCells( void ) const;
177
178/*--------------------------------------------------------------------------*/
179 private:
180 struct Cell
181 {
183 Value car = nil , cdr = nil;
185 bool mark = false;
186 bool cdrSide = false;
188 bool free = true;
189 };
190
191 void markReversal( Value root );
192 void markStack( Value root );
193
195 [[nodiscard]] std::vector< bool > reachable( void ) const;
196
197 std::vector< Cell > f_C;
198 Value f_free = nil;
199 std::size_t f_nfree = 0;
200 std::multiset< Value > f_roots;
201
202 }; // end( class CellHeap )
203
204/*--------------------------------------------------------------------------*/
205
206} // end( namespace ADS )
207
208/*--------------------------------------------------------------------------*/
209/*-------------------------- IMPLEMENTATION --------------------------------*/
210/*--------------------------------------------------------------------------*/
211
212#include "ADS/memory/impl/CellHeap.tpp"
213
214/*--------------------------------------------------------------------------*/
215
216#endif /* CellHeap.h included */
217
218/*--------------------------------------------------------------------------*/
219/*-------------------------- End File CellHeap.h ---------------------------*/
220/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
void setCdr(Value c, Value v)
the field cdr of the cell set to
void addRoot(Value c)
the cell made a root, once more
Value cons(Value car, Value cdr)
a new cell with the fields car and cdr, after a collection if no cell is free
static constexpr Value nil
the empty field
Definition CellHeap.h:78
Value cdr(Value c) const
the field cdr of the cell
void setCar(Value c, Value v)
the field car of the cell set to
long Value
a field: nil, an atom (atom( k ), , for ) or a cell (its address, )
Definition CellHeap.h:75
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; ...
void removeRoot(Value c)
one of the roots removed, if there is one
Value car(Value c) const
the field car of the cell
static bool isCell(Value v)
whether is the address of a cell
CellHeap(std::size_t n)
the heap of cells, all free
static Value atom(long k)
the atom
std::size_t freeCells(void) const
the number of the free cells
Definition ADT.h:96