|
Elm
2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
|
21 #ifndef ELM_IMM_LIST_H_
22 #define ELM_IMM_LIST_H_
24 #include <elm/assert.h>
25 #include <elm/alloc/DefaultAllocator.h>
26 #include <elm/alloc/BlockAllocatorWithGC.h>
27 #include <elm/compare.h>
28 #include <elm/data/List.h>
30 namespace elm {
namespace imm {
34 typedef struct node_t {
35 inline node_t(
const T& h, node_t *t):
hd(h),
tl(t) { }
45 inline void mark(
list<T> l) { node_t *n = l.node;
while(n && !gc->mark(n)) n = n->tl; }
54 virtual void collect(
void)
56 inline void add(Collector& coll) { colls.add(&coll); coll.gc =
this; }
57 inline void remove(Collector& coll) { colls.remove(&coll); }
60 List<Collector *> colls;
64 inline list(node_t *n): node(n) { }
70 inline list(
void): node(0) { }
76 inline const T&
hd(
void)
const { ASSERT(node);
return node->hd; }
77 inline list<T> tl(
void)
const { ASSERT(node);
return node->tl; }
79 inline bool isEmpty(
void)
const {
return !node; }
83 {
int c = 0;
for(node_t *n = node; n; n = n->tl) c++;
return c; }
85 {
for(node_t *n = node; n; n = n->tl)
if(
Equiv<int>::equals(n->hd, v))
return true;
return false; }
87 {
if(node == l.node)
return true;
if(!node || !l.node || !
Equiv<T>::equals(
hd(), l.
hd()))
return false;
return tl().equals(l.
tl()); }
95 list<T> t =
tl().remove(h);
if(node == t.node)
return *
this;
else return cons(
hd(), t); }
100 template <
class T>
typename list<T>::GC list<T>::gc;
111 {
bool f =
true;
out <<
"[ ";
for(; l; l = l.
tl()) {
if(f) f =
false;
else out <<
", ";
out << l.
hd(); }
out <<
" ]";
return out; }
list< T > & operator=(list< T > l)
Definition: list.h:72
typename type_info< T >::out_t out
Definition: type_info.h:284
list< T > concat(list< T > l)
Definition: list.h:91
list< T > operator+(const T &h, list< T > t)
Definition: list.h:104
virtual void collect(void)=0
bool isEmpty(void) const
Definition: list.h:79
bool mark(node_t *b)
Definition: BlockAllocatorWithGC.h:86
friend class GC
Definition: list.h:42
bool equals(list< T > l) const
Definition: list.h:86
list< T > remove(const T &h)
Definition: list.h:93
list< T > make(T a[], int n)
Definition: list.h:106
bool contains(const T &v)
Definition: list.h:84
bool operator==(list< T > l) const
Definition: list.h:88
list(const list< T > &l)
Definition: list.h:71
const T & hd(void) const
Definition: list.h:76
list< T > tl(void) const
Definition: list.h:77
static void remove(Collector &coll)
Definition: list.h:67
list< T > cons(const T &h, list< T > t)
Definition: list.h:103
list(void)
Definition: list.h:70
virtual void collect(void)=0
void mark(list< T > l)
Definition: list.h:45
io::Output & operator<<(io::Output &out, list< T > l)
Definition: list.h:110
const T & operator*(void) const
Definition: list.h:78
bool operator!=(list< T > l) const
Definition: list.h:89
static list< T > null
Definition: list.h:69
static void add(Collector &coll)
Definition: list.h:66
static list< T > cons(const T &h, list< T > t)
Definition: list.h:73
Definition: BlockAllocatorWithGC.h:78
int length(void) const
Definition: list.h:82