NDDEM
LinkedList.h
Go to the documentation of this file.
1 
2 
3 #include <vector>
4 #include <cassert>
5 #include <cstdint>
6 #include <cmath>
7 
8 
9 namespace fg {
10 
11 template <typename T>
12 class list {
13 public:
14  list(size_t nelements) {
15  assert((sizeof(1ULL)*8 == 64)) ;
16  pool = (E*) malloc(sizeof(E)*nelements) ;
17  freed.resize(ceil(nelements/64.), ~0ULL) ;
18  }
19  ~list() { free(pool) ; }
20 
21 
22  struct E {
23  T val ;
24  E* fwd ;
25  E* bwd ;
26  } ;
27 
28  class iterator
29  {
30  public:
31  E* el ;
32  public:
33  iterator() {el=nullptr ; }
34  iterator(E* element) {el=element;}
35  bool operator==(iterator e) { return e.el==el ; }
36  bool operator!=(iterator e) { return !(e.el==el) ; }
37  iterator & operator++(int v) {el=el->fwd; return *this ; } // Not testing nullptr in increment and decrement for speed...
38  iterator & operator--(int v) {el=el->bwd; return *this ; }
39  T& operator*() {return el->val ; }
40  E* operator->() {return el ; }
41  E* raw() { return el ; }
42 
43  } ;
44 
45  E * pool ;
46  E* beg = nullptr ;
47  E* lastelement = nullptr ;
48  E* ending = nullptr ;
49  size_t n = 0 ;
50 
51  size_t size() { return n ; }
52  iterator begin() {return {beg} ; }
53  iterator end() {return {ending} ; }
54  iterator insert(iterator pos, T && value)
55  { // Insert BEFORE the element
56  auto new_element = allocate() ;
57  n ++ ;
58  new_element->val = value ;
59 
60  if (pos.raw() == ending)
61  {
62  new_element->fwd = nullptr ;
63  new_element->bwd = lastelement ;
64  if (n==1)
65  {
66  beg=new_element ;
67  lastelement = new_element ;
68  }
69  else
70  {
71  lastelement->fwd = new_element ;
72  lastelement = new_element ;
73  }
74  }
75  else if (pos.raw() == beg)
76  {
77  new_element->fwd = beg ;
78  new_element->bwd = nullptr ;
79  pos->bwd = new_element ;
80  beg = new_element ;
81  }
82  else
83  {
84  new_element->fwd = pos.raw() ;
85  new_element->bwd = pos->bwd ;
86  pos->bwd = new_element ;
87  (new_element->bwd)->fwd = new_element ;
88  }
89 
90  return {new_element} ;
91  }
93  {
94  E* ret = nullptr ;
95  if (el==nullptr) return nullptr ;
96 
97  if (n==0 && el != nullptr)
98  {
99  printf("ERR: cannot remove from empty list\n") ;
100  return nullptr ;
101  }
102  else if (n==1)
103  {
105  }
106  else
107  {
108  if (el->fwd == ending)
109  {
110  (el->bwd)->fwd = ending ;
111  lastelement = el->bwd ;
112  }
113  else if (el->bwd == nullptr)
114  {
115  (el->fwd)->bwd = nullptr ;
116  beg = el->fwd ;
117  }
118  else
119  {
120  (el->fwd)->bwd = el->bwd ;
121  (el->bwd)->fwd = el->fwd ;
122  }
123  ret = el->fwd ;
124  }
125 
126  freemem(el) ;
127  n-- ;
128 
129  return ret ;
130  }
131 
132 
133 //private:
134  std::vector<uint64_t> freed ;
135 
136  E * allocate() {
137  size_t i,j ;
138  // TODO use a mutex. This need to be atomic from here ...
139  for (i = 0 ; i<freed.size() && freed[i]==0 ; ++i) ;
140  if (i==freed.size()) {printf("ERROR: no more space to allocate element in the list\n") ; return nullptr ; }
141  uint64_t v = freed[i] ;
142  for (j = 0 ; (v & 1ULL) == 0 ; v>>=1, j++) ;
143  freed[i] &= ~(1ULL << j) ;
144  // ... to there
145  return pool + (i*64+j) ;
146  }
147 
148  void freemem (iterator el)
149  {
150  size_t delta = el.raw()-pool ;
151  int i = delta/64 ;
152  int j = delta%64 ;
153  freed[i] |= 1ULL<<j ;
154  }
155 
156 
157 } ;
158 
159 
160 
161 
162 
163 
164 
165 
166 
167 
168 
169 
170 } ;
171 
172 
173 
174 
175 
176 
177 
Definition: LinkedList.h:29
iterator()
Definition: LinkedList.h:33
iterator & operator++(int v)
Definition: LinkedList.h:37
E * operator->()
Definition: LinkedList.h:40
iterator(E *element)
Definition: LinkedList.h:34
bool operator!=(iterator e)
Definition: LinkedList.h:36
iterator & operator--(int v)
Definition: LinkedList.h:38
bool operator==(iterator e)
Definition: LinkedList.h:35
E * raw()
Definition: LinkedList.h:41
T & operator*()
Definition: LinkedList.h:39
E * el
Definition: LinkedList.h:31
Definition: LinkedList.h:12
iterator insert(iterator pos, T &&value)
Definition: LinkedList.h:54
void freemem(iterator el)
Definition: LinkedList.h:148
E * ending
Definition: LinkedList.h:48
E * allocate()
Definition: LinkedList.h:136
list(size_t nelements)
Definition: LinkedList.h:14
~list()
Definition: LinkedList.h:19
E * pool
Definition: LinkedList.h:45
E * erase(iterator el)
Definition: LinkedList.h:92
iterator end()
Definition: LinkedList.h:53
iterator begin()
Definition: LinkedList.h:52
size_t n
Definition: LinkedList.h:49
E * lastelement
Definition: LinkedList.h:47
size_t size()
Definition: LinkedList.h:51
E * beg
Definition: LinkedList.h:46
std::vector< uint64_t > freed
Definition: LinkedList.h:134
@ pos
Definition: Typedefs.h:19
Definition: LinkedList.h:9
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1282
unsigned __int64 uint64_t
Definition: stdint.h:136
Definition: LinkedList.h:22
T val
Definition: LinkedList.h:23
E * bwd
Definition: LinkedList.h:25
E * fwd
Definition: LinkedList.h:24