-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactionlist.cc
261 lines (235 loc) · 6 KB
/
actionlist.cc
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
#include "actionlist.h"
#include "action.h"
#include <string.h>
#include "stl-model.h"
#include <limits.h>
actionlist::actionlist() :
root(),
head(NULL),
tail(NULL),
_size(0)
{
}
actionlist::~actionlist() {
clear();
}
allnode::allnode() :
parent(NULL),
count(0) {
bzero(children, sizeof(children));
}
allnode::~allnode() {
if (count != 0)
for(int i=0;i<ALLNODESIZE;i++) {
if (children[i] != NULL && !(((uintptr_t) children[i]) & ISACT))
delete children[i];
}
}
sllnode<ModelAction *> * allnode::findPrev(modelclock_t index) {
allnode * ptr = this;
modelclock_t increment = 1;
int totalshift = 0;
index -= increment;
while(1) {
modelclock_t currindex = (index >> totalshift) & ALLMASK;
//See if we went negative...
if (currindex != ALLMASK) {
if (ptr->children[currindex] == NULL) {
//need to keep searching at this level
index -= increment;
continue;
} else {
//found non-null...
if (totalshift == 0)
return reinterpret_cast<sllnode<ModelAction *> *>(((uintptr_t)ptr->children[currindex])& ACTMASK);
//need to increment here...
ptr = ptr->children[currindex];
increment = increment >> ALLBITS;
totalshift -= ALLBITS;
break;
}
}
//If we get here, we already did the decrement earlier...no need to decrement again
ptr = ptr->parent;
increment = increment << ALLBITS;
totalshift += ALLBITS;
if (ptr == NULL) {
return NULL;
}
}
while(1) {
while(1) {
modelclock_t currindex = (index >> totalshift) & ALLMASK;
if (ptr->children[currindex] != NULL) {
if (totalshift != 0) {
ptr = ptr->children[currindex];
break;
} else {
allnode * act = ptr->children[currindex];
sllnode<ModelAction *> * node = reinterpret_cast<sllnode<ModelAction *>*>(((uintptr_t)act) & ACTMASK);
return node;
}
}
index -= increment;
}
increment = increment >> ALLBITS;
totalshift -= ALLBITS;
}
}
void actionlist::addAction(ModelAction * act) {
_size++;
int shiftbits = MODELCLOCKBITS - ALLBITS;
modelclock_t clock = act->get_seq_number();
allnode * ptr = &root;
do {
modelclock_t currindex = (clock >> shiftbits) & ALLMASK;
allnode * tmp = ptr->children[currindex];
if (shiftbits == 0) {
sllnode<ModelAction *> * llnode = new sllnode<ModelAction *>();
llnode->val = act;
if (tmp == NULL) {
sllnode<ModelAction *> * llnodeprev = ptr->findPrev(clock);
if (llnodeprev != NULL) {
llnode->next = llnodeprev->next;
llnode->prev = llnodeprev;
//see if we are the new tail
if (llnode->next != NULL)
llnode->next->prev = llnode;
else
tail = llnode;
llnodeprev->next = llnode;
} else {
//We are the begining
llnode->next = head;
llnode->prev = NULL;
if (head != NULL) {
head->prev = llnode;
} else {
//we are the first node
tail = llnode;
}
head = llnode;
}
ptr->children[currindex] = reinterpret_cast<allnode *>(((uintptr_t) llnode) | ISACT);
//need to find next link
ptr->count++;
} else {
//handle case where something else is here
sllnode<ModelAction *> * llnodeprev = reinterpret_cast<sllnode<ModelAction *>*>(((uintptr_t) tmp) & ACTMASK);
llnode->next = llnodeprev->next;
llnode->prev = llnodeprev;
if (llnode->next != NULL)
llnode->next->prev = llnode;
else
tail = llnode;
llnodeprev->next = llnode;
ptr->children[currindex] = reinterpret_cast<allnode *>(((uintptr_t) llnode) | ISACT);
}
return;
} else if (tmp == NULL) {
tmp = new allnode();
ptr->children[currindex] = tmp;
tmp->parent=ptr;
ptr->count++;
}
shiftbits -= ALLBITS;
ptr = tmp;
} while(1);
}
void decrementCount(allnode * ptr) {
ptr->count--;
if (ptr->count == 0) {
if (ptr->parent != NULL) {
for(uint i=0;i<ALLNODESIZE;i++) {
if (ptr->parent->children[i]==ptr) {
ptr->parent->children[i] = NULL;
decrementCount(ptr->parent);
break;
}
}
delete ptr;
}
}
}
void actionlist::removeAction(ModelAction * act) {
int shiftbits = MODELCLOCKBITS;
modelclock_t clock = act->get_seq_number();
allnode * ptr = &root;
allnode * oldptr;
modelclock_t currindex;
while(shiftbits != 0) {
shiftbits -= ALLBITS;
currindex = (clock >> shiftbits) & ALLMASK;
oldptr = ptr;
ptr = ptr->children[currindex];
if (ptr == NULL)
return;
}
sllnode<ModelAction *> * llnode = reinterpret_cast<sllnode<ModelAction *> *>(((uintptr_t) ptr) & ACTMASK);
bool first = true;
do {
if (llnode->val == act) {
//found node to remove
sllnode<ModelAction *> * llnodeprev = llnode->prev;
sllnode<ModelAction *> * llnodenext = llnode->next;
if (llnodeprev != NULL) {
llnodeprev->next = llnodenext;
} else {
head = llnodenext;
}
if (llnodenext != NULL) {
llnodenext->prev = llnodeprev;
} else {
tail = llnodeprev;
}
if (first) {
//see if previous node has same clock as us...
if (llnodeprev != NULL && llnodeprev->val->get_seq_number() == clock) {
oldptr->children[currindex] = reinterpret_cast<allnode *>(((uintptr_t)llnodeprev) | ISACT);
} else {
//remove ourselves and go up tree
oldptr->children[currindex] = NULL;
decrementCount(oldptr);
}
}
delete llnode;
_size--;
return;
}
llnode = llnode->prev;
first = false;
} while(llnode != NULL && llnode->val->get_seq_number() == clock);
//node not found in list... no deletion
return;
}
void actionlist::clear() {
for(uint i = 0;i < ALLNODESIZE;i++) {
if (root.children[i] != NULL) {
delete root.children[i];
root.children[i] = NULL;
}
}
while(head != NULL) {
sllnode<ModelAction *> *tmp=head->next;
delete head;
head = tmp;
}
tail = NULL;
root.count = 0;
_size = 0;
}
bool actionlist::isEmpty() {
return root.count == 0;
}
/**
* Fix the parent pointer of root when root address changes (possible
* due to vector<action_list_t> resize)
*/
void actionlist::fixupParent()
{
for (int i = 0;i < ALLNODESIZE;i++) {
allnode * child = root.children[i];
if (child != NULL && child->parent != &root)
child->parent = &root;
}
}