forked from elnopintan/gorgone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPersistentVector.h
311 lines (280 loc) · 7.04 KB
/
PersistentVector.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* PersistentVector.h
*
* Created on: 20/12/2011
* Author: ignacio
*/
#ifndef PERSISTENTVECTOR_H_
#define PERSISTENTVECTOR_H_
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <vector>
class VectorException {};
template <typename T>
class PersistentVector {
public:
class Node {
public:
virtual ~Node() {};
};
class Leaf :public Node{
T value;
public:
Leaf(T newvalue) :value(newvalue){};
Leaf (){};
virtual ~Leaf() {};
T getValue() {return value;};
};
typedef boost::shared_ptr<Node > Node_ptr;
typedef std::vector<Node_ptr> Node_vector;
class Branch : public Node{
Node_vector array;
public:
Branch(Node_vector &other):
array(other) {};
Branch() {};
virtual ~Branch() {};
Node_vector getArray() {return array;};
};
mutable unsigned int _cnt;
mutable unsigned int _shift;
mutable Node_ptr _root;
mutable Node_vector _tail;
public:
PersistentVector<T>() :
_cnt(0), _shift(5),_root(new Branch()) {}
PersistentVector<T>(unsigned int cnt, unsigned int shift, Node_ptr root, Node_vector &tail):
_cnt(cnt),_shift(shift),_root(root),_tail(tail) {}
unsigned int tailoff() const{
if(_cnt < 32)
return 0;
return ((_cnt - 1) >> 5) << 5;
};
unsigned int size() const {return _cnt;}
T get(unsigned int i) const {
if(//i >= 0 &&
i < _cnt)
{
Node_vector array;
if(i < tailoff()) {
Node_ptr node = _root;
for(int level = _shift; level > 0; level -= 5)
node = static_cast<Branch *> (node.get())->getArray()[(i >> level) & 0x01f];
array=static_cast<Branch *> (node.get())->getArray();
}
else
array=_tail;
return static_cast<Leaf *> (array[i & 0x01f].get())->getValue();
}
throw VectorException();
};
inline T operator[] (unsigned int i) const { return get (i); }
PersistentVector<T> put(unsigned int pos, const T &val) const {
if(pos >= 0 && pos < _cnt)
{
PersistentVector<T> newVector(*this);
if(pos >= tailoff())
{
newVector._tail[pos & 0x01f]=Node_ptr(new Leaf(val));
return newVector;
}
newVector._root=doPut(_shift,_root,pos,val);
return newVector;
}
if(pos == _cnt)
return add(val);
throw VectorException();
}
Node_ptr doPut(unsigned int level, Node_ptr node,unsigned int pos, const T &val) {
Node_vector array= static_cast<Branch *>(node.get())->getArray();
if(level == 0)
{
array[pos & 0x01f] = Node_ptr(new Leaf(val));
}
else
{
int subidx = (pos >> level) & 0x01f;
array[subidx] = doPut(level-5, array[subidx], pos, val);
}
return Node_ptr(new Branch(array));
}
PersistentVector<T> push_back(const T &val) const {
//room in tail?
// if(tail.length < 32)
if(_cnt - tailoff() < 32)
{
Node_vector newTail=_tail;
newTail.push_back(Node_ptr(new Leaf(val)));
return PersistentVector(_cnt + 1, _shift, _root, newTail);
}
//full tail, push into tree
PersistentVector<T> ret=PersistentVector<T>();
ret._cnt=_cnt+1;
ret._shift=_shift;
ret._tail.push_back(Node_ptr(new Leaf(val)));
Node_ptr tailnode(new Branch(_tail));
//overflow root?
if((_cnt >> 5) > ((unsigned)1 << _shift))
{
Node_vector array;
array.push_back(_root);
array.push_back(createPath(_shift, tailnode));
ret._root=Node_ptr(new Branch(array));
ret._shift += 5;
}
else
{
ret._root=pushTail(_shift, _root, tailnode);
}
return ret;
}
Node_ptr createPath(unsigned int level, Node_ptr &node) const {
if (level==0)
return node;
Node_vector array;
array.push_back(node);
Node_ptr new_node(new Branch(array));
return createPath(level-5,new_node);
};
Node_ptr pushTail(unsigned int level, Node_ptr &parent, Node_ptr &tailnode) const {
unsigned int subidx = ((_cnt - 1) >> level) & 0x01f;
Branch *parent_branch= static_cast<Branch *> (parent.get());
Node_vector array = parent_branch->getArray();
Node_ptr newNode;
if(level == 5)
{
newNode=tailnode;
}
else
{
if (array.size() > subidx) {
Node_ptr child= parent_branch->getArray()[subidx];
newNode=pushTail(level-5,child, tailnode);
}
else
newNode=createPath(level-5, tailnode);
}
if (array.size()>subidx)
array[subidx]=newNode;
else
array.push_back(newNode);
return Node_ptr(new Branch(array));
};
PersistentVector<T> pop() const {
if (_cnt == 0)
throw VectorException();
if (_cnt == 1)
return PersistentVector<T>();
if (_tail.size() > 1)
{
PersistentVector<T> newVector (*this);
newVector._cnt=_cnt-1;
newVector._tail.pop_back();
return newVector;
}
PersistentVector<T> newVector;
newVector._cnt=_cnt-1;
newVector._tail=findTail();
newVector._root=popRoot(_shift,_root);
newVector._shift=_shift;
Branch * rootBranch=static_cast<Branch *> (newVector._root.get());
if(_shift > 5 && rootBranch->getArray().size()==1)
{
newVector._root=rootBranch->getArray()[0];
newVector._shift -= 5;
}
return newVector;
}
Node_vector findTail() const {
unsigned int pos = _cnt-2;
Node_ptr node=_root;
for (int i=_shift; i>0; i-=5)
node=static_cast<Branch *> (node.get())->getArray()[(pos >> i) & 0x01f];
return static_cast<Branch *> (node.get())->getArray();
};
Node_ptr popRoot(unsigned int level,Node_ptr &node) const {
unsigned int pos = _cnt-2;
unsigned int subidx = (pos >> level) & 0x01f;
Node_vector array = static_cast<Branch *> (node.get())->getArray();
if (level > 5) {
Node_ptr newNode=popRoot(level-5,array[subidx]);
if (static_cast<Branch *> (newNode.get())->getArray().size() == 0)
array.pop_back();
}
else
array.pop_back();
return Node_ptr(new Branch(array));
};
bool operator == (const PersistentVector &other) const {
for (int i = 0; i < size (); i++) {
if (get(i) != other.get(i))
return false;
}
return true;
}
};
template<typename T>
class PersistentVectorCopy {
std::vector<T> array;
public:
PersistentVectorCopy<T> add(T data) {
PersistentVectorCopy<T> newVector=*this;
newVector.array.push_back(data);
return newVector;
}
T get(unsigned int i) {
if (i<array.size())
return array[i];
throw VectorException();
}
};
template<typename T>
class PersistentVectorSPtr {
std::vector<boost::shared_ptr<T> > array;
public:
PersistentVectorSPtr<T> add(T data) {
PersistentVectorSPtr<T> newVector=*this;
newVector.array.push_back(boost::shared_ptr<T>(new T(data)));
return newVector;
}
T get(unsigned int i) {
if (i<array.size())
return *(array[i]);
throw VectorException();
}
};
template<typename T>
class TransientVectorSptr {
std::vector<boost::shared_ptr<T> > array;
public:
void add(T data) {
array.push_back(boost::shared_ptr<T>(new T(data)));
}
const T &get(unsigned int i) {
if (i<array.size())
return *(array[i]);
throw VectorException();
}
unsigned int size() {return array.size();}
void pop() {
array.pop_back();
}
};
template<typename T>
class TransientVectorCopy {
std::vector<T> array;
public:
void add(T data) {
array.push_back(data);
}
T get(unsigned int i) {
if (i<array.size())
return array[i];
throw VectorException();
}
unsigned int size() {return array.size();}
void pop() {
array.pop_back();
}
};
#endif