Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
list.h
1 /*
2  * imm::list class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2013, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_IMM_LIST_H_
22 #define ELM_IMM_LIST_H_
23 
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>
29 
30 namespace elm { namespace imm {
31 
32 template <class T>
33 class list {
34  typedef struct node_t {
35  inline node_t(const T& h, node_t *t): hd(h), tl(t) { }
36  T hd;
37  node_t *tl;
38  } node_t;
39  class GC;
40 public:
41  class Collector {
42  friend class GC;
43  public:
44  virtual ~Collector(void);
45  inline void mark(list<T> l) { node_t *n = l.node; while(n && !gc->mark(n)) n = n->tl; }
46  virtual void collect(void) = 0;
47  private:
48  GC *gc;
49  };
50 
51 private:
52  class GC: public BlockAllocatorWithGC<node_t> {
53  public:
54  virtual void collect(void)
55  { for(typename List<Collector *>::Iter coll(colls); coll(); coll++) coll->collect(); }
56  inline void add(Collector& coll) { colls.add(&coll); coll.gc = this; }
57  inline void remove(Collector& coll) { colls.remove(&coll); }
58  inline bool mark(node_t *node) { return BlockAllocatorWithGC<node_t>::mark(node); }
59  private:
60  List<Collector *> colls;
61  };
62  static GC gc;
63 
64  inline list(node_t *n): node(n) { }
65 public:
66  static inline void add(Collector& coll) { gc.add(coll); }
67  static inline void remove(Collector& coll) { gc.remove(coll); }
68 
69  static list<T> null;
70  inline list(void): node(0) { }
71  inline list(const list<T>& l): node(l.node) { }
72  inline list<T>& operator=(list<T> l) { node = l.node; return *this; }
73  static list<T> cons(const T& h, list<T> t)
74  { node_t *n = list<T>::gc.allocate(); n->hd = h; n->tl = t.node; return list<T>(n); }
75 
76  inline const T& hd(void) const { ASSERT(node); return node->hd; }
77  inline list<T> tl(void) const { ASSERT(node); return node->tl; }
78  inline const T& operator*(void) const { return hd(); }
79  inline bool isEmpty(void) const { return !node; }
80  inline operator bool(void) const { return !isEmpty(); }
81 
82  inline int length(void) const
83  { int c = 0; for(node_t *n = node; n; n = n->tl) c++; return c; }
84  inline bool contains(const T& v)
85  { for(node_t *n = node; n; n = n->tl) if(Equiv<int>::equals(n->hd, v)) return true; return false; }
86  inline bool equals(list<T> l) const
87  { if(node == l.node) return true; if(!node || !l.node || !Equiv<T>::equals(hd(), l.hd())) return false; return tl().equals(l.tl()); }
88  inline bool operator==(list<T> l) const { return equals(l); }
89  inline bool operator!=(list<T> l) const { return !equals(l); }
90 
92  { if(isEmpty()) return l; else return cons(hd(), tl().concat(l)); }
93  inline list<T> remove(const T& h)
94  { if(!node) return *this; if(Equiv<T>::equals(h, hd())) return tl();
95  list<T> t = tl().remove(h); if(node == t.node) return *this; else return cons(hd(), t); }
96 
97 private:
98  node_t *node;
99 };
100 template <class T> typename list<T>::GC list<T>::gc;
101 template <class T> list<T> list<T>::null(0);
102 
103 template <class T> inline list<T> cons(const T& h, list<T> t) { return list<T>::cons(h, t); }
104 template <class T> inline list<T> operator+(const T& h, list<T> t) { return cons(h, t); }
105 template <class T> inline list<T> operator+(list<T> l1, list<T> l2) { return l1.concat(l2); }
106 template <class T> inline list<T> make(T a[], int n)
107  { if(!n) return list<T>::null; else return cons(*a, make(a + 1, n - 1)); }
108 
109 template <class T>
111  { bool f = true; out << "[ "; for(; l; l = l.tl()) { if(f) f = false; else out << ", "; out << l.hd(); } out << " ]"; return out; }
112 
113 } } // elm::imm
114 
115 #endif /* ELM_IMM_LIST_H_ */
elm::imm::list::operator=
list< T > & operator=(list< T > l)
Definition: list.h:72
elm::t::out
typename type_info< T >::out_t out
Definition: type_info.h:284
elm::imm::list::concat
list< T > concat(list< T > l)
Definition: list.h:91
elm::imm::list::Collector
Definition: list.h:41
elm::imm::operator+
list< T > operator+(const T &h, list< T > t)
Definition: list.h:104
elm::imm::list::Collector::collect
virtual void collect(void)=0
elm::imm::list::isEmpty
bool isEmpty(void) const
Definition: list.h:79
elm::List::Iter
Definition: List.h:64
elm::BlockAllocatorWithGC< node_t >::mark
bool mark(node_t *b)
Definition: BlockAllocatorWithGC.h:86
elm::imm::list::Collector::GC
friend class GC
Definition: list.h:42
bool
elm::imm::list::equals
bool equals(list< T > l) const
Definition: list.h:86
elm::imm::list::remove
list< T > remove(const T &h)
Definition: list.h:93
elm::imm::make
list< T > make(T a[], int n)
Definition: list.h:106
elm::imm::list::contains
bool contains(const T &v)
Definition: list.h:84
elm::imm::list::operator==
bool operator==(list< T > l) const
Definition: list.h:88
elm::imm::list::list
list(const list< T > &l)
Definition: list.h:71
elm::imm::list::hd
const T & hd(void) const
Definition: list.h:76
elm
Definition: adapter.h:26
elm::imm::list::tl
list< T > tl(void) const
Definition: list.h:77
elm::imm::list
Definition: list.h:33
elm::imm::list::remove
static void remove(Collector &coll)
Definition: list.h:67
elm::imm::cons
list< T > cons(const T &h, list< T > t)
Definition: list.h:103
elm::imm::list::list
list(void)
Definition: list.h:70
elm::AbstractBlockAllocatorWithGC::collect
virtual void collect(void)=0
elm::imm::list::Collector::mark
void mark(list< T > l)
Definition: list.h:45
elm::imm::operator<<
io::Output & operator<<(io::Output &out, list< T > l)
Definition: list.h:110
elm::imm::list::operator*
const T & operator*(void) const
Definition: list.h:78
elm::imm::list::operator!=
bool operator!=(list< T > l) const
Definition: list.h:89
elm::imm::list::null
static list< T > null
Definition: list.h:69
elm::imm::list::add
static void add(Collector &coll)
Definition: list.h:66
elm::imm::list::cons
static list< T > cons(const T &h, list< T > t)
Definition: list.h:73
elm::BlockAllocatorWithGC
Definition: BlockAllocatorWithGC.h:78
elm::io::Output
Definition: Output.h:179
elm::imm::list::Collector::~Collector
virtual ~Collector(void)
elm::Equiv
Definition: equiv.h:32
elm::imm::list::length
int length(void) const
Definition: list.h:82