-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrieTable.cpp
More file actions
402 lines (350 loc) · 9.39 KB
/
TrieTable.cpp
File metadata and controls
402 lines (350 loc) · 9.39 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include <iostream>
#include <vector>
#define initialEntrySize 10
#define initialTableSize 10
#define incrEntrySize 10
#define incrTableSize 10
using namespace std;
/*Utility Functions*/
//get the bit of bitIndex MSB - helper to labelToIndex
int checkNthBit(int num, int bitIndex){
int bitToCheck = sizeof(int)*8 - bitIndex;
int bitStatus = (num >> bitToCheck) & 1;
if (bitStatus == 0) return 0;
return 1;
}
unsigned djb_hash(void *key, int len){
unsigned char *p = (unsigned char*)key;
unsigned h = 0;
int i;
for (i = 0; i < len; i++){
h = 33 * h + p[i];
}
return h % initialTableSize; //Table size never changes
}
unsigned int MurmurHash2 ( const void * key, int len, unsigned int seed )
{
// 'm' and 'r' are mixing constants generated offline.
// They're not really 'magic', they just happen to work well.
const unsigned int m = 0x5bd1e995;
const int r = 24;
// Initialize the hash to a 'random' value
unsigned int h = seed ^ len;
// Mix 4 bytes at a time into the hash
const unsigned char * data = (const unsigned char *)key;
while(len >= 4)
{
unsigned int k = *(unsigned int *)data;
k *= m;
k ^= k >> r;
k *= m;
h *= m;
h ^= k;
data += 4;
len -= 4;
}
// Handle the last few bytes of the input array
switch(len)
{
case 3: h ^= data[2] << 16;
case 2: h ^= data[1] << 8;
case 1: h ^= data[0];
h *= m;
};
// Do a few final mixes of the hash to ensure the last few
// bytes are well-incorporated.
h ^= h >> 13;
h *= m;
h ^= h >> 15;
return h % initialTableSize;
}
int setLabel(int label, int numBits){
int numBitsInt = sizeof(int)*8;
if (numBits == numBitsInt) return label;
int mask = 1;
for (int i = 1; i < numBitsInt - numBits; i++)
mask = (mask << 1) | 1;
mask = ~mask;
return (label & mask);
}
int getHash(int label, int numBits){
int intLabel[2];
intLabel[1] = numBits;
intLabel[0] = setLabel(label, numBits);
int hashValue = MurmurHash2(&intLabel[0], sizeof(int)*2, 23471);
/*
char strLabel[sizeof(int)*8+1];
for (int i = 0; i < numBitsInt; i++) strLabel[i] = 'X';
strLabel[numBitsInt] = '\0';
int bitIndex = 0;
while (bitIndex < numBits){
int bitStatus = checkNthBit(label,bitIndex);
if (bitStatus == 1) strLabel[bitIndex] = '1';
else strLabel[bitIndex] = '0';
bitIndex += 1;
}
int hashValue = MurmurHash2(&strLabel[0], numBitsInt,23471);
*/
return hashValue;
}
int getCommonLabel(int startTime, int endTime){
int x = (startTime ^ endTime);
//Count the number of uninterrupted 1's
int mask = 1, latest = 0;
for (int i = 1; i <= sizeof(int)*8; i++){
if ((x & mask) != 0) latest = i;
mask = mask << 1;
}
int numBits = sizeof(int)*8 - latest;
int ll = sizeof(int);
//ll = ll *8;
return numBits;
}
/**********************************************************/
struct TrieTableEntry{
int *edgeList;
int numEdges;
int listSize;
int label, numBits;
TrieTableEntry *next; //for chaining
TrieTableEntry(){
edgeList = NULL;
numEdges = 0;
listSize = 0;
label = -1;
numBits = -1;
next = NULL;
}
void initialize(){
edgeList = NULL;
numEdges = 0;
listSize = 0;
label = -1;
numBits = -1;
next = NULL;
}
void allocate(int isLabel, int isNumBits){
edgeList = (int*)malloc(sizeof(int)*initialEntrySize);
numEdges = 0;
listSize = initialEntrySize;
label = setLabel(isLabel, isNumBits);
numBits = isNumBits;
next = NULL;
}
void deallocate(){
free(edgeList);
listSize = 0;
numEdges = 0;
numBits = -1;
label = -1;
}
void copy(TrieTableEntry *other){
edgeList = other->edgeList;
numEdges = other->numEdges;
listSize = other->listSize;
label = other->label;
numBits = other->numBits;
next = other->next;
}
void addEdge(int destId){
if (listSize > numEdges){
edgeList[numEdges] = destId;
numEdges += 1;
}
else{
int *temp = (int*)malloc(sizeof(int)*(listSize + incrEntrySize));
for (int i = 0; i < listSize; i++)
temp[i] = edgeList[i];
free(edgeList);
edgeList = temp;
listSize += incrEntrySize;
edgeList[numEdges] = destId;
numEdges += 1;
}
}
int removeEdge(int destId){
int destIndex = -1;
for (int i = 0; i < numEdges; i++){
if (edgeList[i] == destId){
destIndex = i;
break;
}
}
if (destIndex == -1) return numEdges; //nothing to delete
if (destIndex < numEdges - 1)
edgeList[destIndex] = edgeList[numEdges - 1];
numEdges -= 1;
return numEdges;
}
bool compareLabel(int otherLabel, int otherNumBits){
if (numBits != otherNumBits) return false;
if (label != otherLabel) return false;
/* int bitIndex = 0;
while (bitIndex < numBits){
if (checkNthBit(label,bitIndex) != checkNthBit(otherLabel,bitIndex)) return false;
bitIndex += 1;
}
*/
return true;
}
int randomNeighbor(int presentSample, int numPresentEdges){
cout << "randomNeighbor: over here1"<< endl;
if (numEdges <= 0) return presentSample;
cout << "randomNeighbor: over here , numEdges = " << numEdges << endl;
int z = rand() % (numEdges + numPresentEdges);
if (z < numEdges) return edgeList[rand() % numEdges];
return presentSample;
}
void printEntry(){
for (int i = 0; i < numEdges; i ++){
cout << edgeList[i] << ", ";
}
cout << endl;
}
};
struct TrieTable{
TrieTableEntry *table;
TrieTable(){
table = (TrieTableEntry*)malloc(sizeof(TrieTableEntry)*initialTableSize);
for (int i = 0; i < initialTableSize; i++){
table[i].initialize();
}
}
void printTable(){
for (int i = 0; i < initialTableSize; i++){
cout << i << ":" << endl;
TrieTableEntry *ttE = &table[i];
if (ttE->label != -1){
while (ttE){
ttE->printEntry();
ttE = ttE->next;
}
}
}
}
TrieTableEntry *getEntryForLabel(int label, int numBits){
int hashIndex = getHash(label, numBits);
TrieTableEntry *temp = &table[hashIndex];
while (temp){
// cout << "GetEntryForLabel: 1" << temp << endl << flush;
if (temp->compareLabel(label,numBits)){
return temp;
}
// cout << "GetEntryForLabel: 2" << temp << endl << flush;
temp = temp->next;
// cout << "GetEntryForLabel: 3 " << temp << endl << flush;
}
return NULL;
}
void addEdge(int destId, int label, int numBits){
int hashIndex = getHash(label,numBits);
if ((table[hashIndex].label == -1)&&(table[hashIndex].numBits == -1)){
table[hashIndex].allocate(label, numBits);
table[hashIndex].addEdge(destId);
}
else if (!table[hashIndex].compareLabel(label,numBits)){
TrieTableEntry *t = table[hashIndex].next, *pt;
pt = &(table[hashIndex]);
int foundFlag = 0;
while (t){
if (t->compareLabel(label,numBits)){
t->addEdge(destId);
foundFlag = 1;
break;
}
pt = t;
t = t->next;
}
if (foundFlag == 0){
pt->next = new TrieTableEntry();
t = pt->next;
t->allocate(label, numBits);
t->addEdge(destId);
}
}
else table[hashIndex].addEdge(destId);
}
int removeEdge(int destId, int label, int numBits){
int hashIndex = getHash(label,numBits);
// cout << "DestId = " << destId << ", label = " << label << ", numBits = " << numBits << ", hashValue = " << hashIndex << endl;
if ((table[hashIndex].label == -1) && (table[hashIndex].numBits == -1)) return 0; //edge not found
TrieTableEntry *head = &(table[hashIndex]);
TrieTableEntry *temp = head, *pt = NULL;
int numE = -1;
while (temp){
if (temp->compareLabel(label,numBits)){
numE = temp->removeEdge(destId);
break;
}
pt = temp;
temp = temp->next;
}
if (numE != 0) return 0; //edge not found or no entry to delete
if (temp == head){ //or check if pt is NULL
temp = head->next;
head->deallocate();
if (temp){
head->copy(temp);
free(temp);
}
}
else{
pt->next = temp->next;
temp->deallocate();
free(temp);
}
return 1; //entryRemoved
}
int parentLabel(int label, int numBits){
int mask = 1 << (sizeof(int)*8 - numBits);
int nB = numBits;
while (nB > 0){
nB -= 1;
if ((label & mask) == 0) break;
}
return nB;
}
void shiftEdges(TrieTableEntry *t, int *presentEdges, int numPresentEdges, int numLevels){
//determine parent(index) to shift to
int parentLabelBits = parentLabel(t->label, t->numBits);
//for each edge, see if it is alive - can be very inefficient!
int i = 0;
while (i < t->numEdges){
int destId = t->edgeList[i], aliveFlag = 0;
for (int j = 0; j < numPresentEdges; j++){
if (destId == presentEdges[j]){
aliveFlag = 1;
break;
}
}
if (aliveFlag == 1){
//if yes, delete from t, insert into parent
int entryRemoved = removeEdge(destId, t->label, t->numBits);
if (entryRemoved == 1) return;
addEdge(destId, t->label, parentLabelBits);
}
else i++;
}
}
void traverseRightmostPath(int timestep, int numLevels, int *presentEdges, int numPresentEdges){
int t = timestep - 1, nB = sizeof(int)*8, ctr = 0;
while ((nB > 0) && (ctr < numLevels)){
// cout << "traverseRightmostPath CP 1" << endl << flush;
TrieTableEntry *ttE = getEntryForLabel(t, nB);
// cout << "traverseRightmostPath CP 1.2" << endl << flush;
if (ttE){
// cout << "traverseRightmostPath CP 1.5" << endl << flush;
shiftEdges(ttE, presentEdges, numPresentEdges, numLevels);
// cout << "traverseRightmostPath CP 1.6" << endl << flush;
}
// cout << "traverseRightmostPath CP 2" << endl << flush;
nB -= 1;
ctr += 1;
}
}
int randomNeighbor(int label, int numBits, int presentSample, int numPresentEdges){
TrieTableEntry *ttE = getEntryForLabel(label, numBits);
if (ttE == NULL) return presentSample;
return ttE->randomNeighbor(presentSample, numPresentEdges);
}
};