-
Notifications
You must be signed in to change notification settings - Fork 0
/
ITeM.h
604 lines (566 loc) · 16.4 KB
/
ITeM.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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
#pragma once
#include <iostream>
#include <vector>
#include <bitset>
#include <queue>
#include <map>
#include "hash_function.h"
using namespace std;
struct bucket
{
unsigned short src;
unsigned short dst;
short weight;
unsigned short subs;
unsigned int idx;
};
class ITeM
{
private:
int width;
int f_length;
int allocated_size;
int cur;
public:
bucket *matrix;
vector<bucket *> variable_array;
ITeM(int w, int f);
ITeM(ITeM &it) : width(it.width), f_length(it.f_length), allocated_size(it.allocated_size), cur(0), matrix(it.matrix)
{
}
~ITeM()
{
delete matrix;
vector<bucket *>::iterator IT = variable_array.begin();
bucket *tmp;
for (; IT != variable_array.end(); ++IT)
{
tmp = *IT;
delete tmp;
}
}
void insert(string s1, string s2, short weight);
void insert(int pos, string s1, string s2, short weight);
void insert(int pos, unsigned short g1, unsigned short g2, short weight, bool flag);
void checkFSIT(int num, int pos, unsigned short g1, unsigned short g2, short weight);
bool findDeepestNode(int num, int pos, unsigned short g1, unsigned short g2, short weight);
int edgeWeightQuery(string s1, string s2);
int edgeWeightQuery(int pos, unsigned short g1, unsigned short g2);
int checkEdgeWeight(int num, int pos, unsigned short g1, unsigned short g2);
int nodeWeightQuery(string s1, int type);
int checkNodeWeight(int num, int pos, unsigned short g, int type);
bool reachabilityQuery(string s1, string s2);
bool checkReachability(int num, int pos, unsigned short g1, unsigned short g2);
void getReachabilityNode(int num, int pos, int col, unsigned short g1, vector<unsigned int> &set);
int getMaxSameLen(unsigned short g1, unsigned short g2);
};
ITeM::ITeM(int w, int f)
{
width = w;
f_length = f;
allocated_size = 131072;
cur = 0;
matrix = new bucket[width * width];
memset(matrix, 0, sizeof(bucket) * width * width);
}
void ITeM::insert(string s1, string s2, short weight)
{
unsigned int hash1 = (*hfunc[0])((unsigned char *)(s1.c_str()), s1.length());
unsigned int hash2 = (*hfunc[0])((unsigned char *)(s2.c_str()), s2.length());
unsigned int tmp = (1 << f_length) - 1;
unsigned short g1 = hash1 & tmp;
if (g1 == 0)
g1 += 1;
unsigned int h1 = (hash1 >> f_length) % width;
unsigned short g2 = hash2 & tmp;
if (g2 == 0)
g2 += 1;
unsigned int h2 = (hash2 >> f_length) % width;
int pos = h1 * width + h2;
if (matrix[pos].src == 0)
{
matrix[pos].src = g1;
matrix[pos].dst = g2;
matrix[pos].weight = weight;
matrix[pos].idx = (f_length << 4) + f_length;
matrix[pos].subs = 0;
}
else
checkFSIT(-1, pos, g1, g2, weight);
}
void ITeM::insert(int pos, string s1, string s2, short weight)
{
unsigned int hash1 = (*hfunc[0])((unsigned char *)(s1.c_str()), s1.length());
unsigned int hash2 = (*hfunc[0])((unsigned char *)(s2.c_str()), s2.length());
unsigned int tmp = (1 << f_length) - 1;
unsigned short g1 = hash1 & tmp;
if (g1 == 0)
g1 += 1;
unsigned short g2 = hash2 & tmp;
if (g2 == 0)
g2 += 1;
if (matrix[pos].src == 0)
{
matrix[pos].src = g1;
matrix[pos].dst = g2;
matrix[pos].weight = weight;
matrix[pos].idx = (f_length << 4) + f_length;
matrix[pos].subs = 0;
}
else
checkFSIT(-1, pos, g1, g2, weight);
}
void ITeM::insert(int pos, unsigned short g1, unsigned short g2, short weight, bool flag)
{
if (matrix[pos].src == 0 || flag == false)
{
matrix[pos].src = g1;
matrix[pos].dst = g2;
matrix[pos].weight = weight;
matrix[pos].idx = (f_length << 4) + f_length;
matrix[pos].subs = 0;
}
else
checkFSIT(-1, pos, g1, g2, weight);
}
void ITeM::checkFSIT(int num, int pos, unsigned short g1, unsigned short g2, short weight)
{
bucket *op;
if (num == -1)
op = matrix;
else
op = variable_array[num];
unsigned short og1 = op[pos].src;
unsigned short og2 = op[pos].dst;
int olen1 = op[pos].idx & ((1 << 4) - 1);
int olen2 = (op[pos].idx >> 4) & ((1 << 4) - 1);
int len1 = getMaxSameLen(g1, og1);
int len2 = getMaxSameLen(g2, og2);
if (len1 < olen1)
{
len2 = min(len2, olen2);
if (cur == allocated_size || variable_array.size() == 0)
{
bucket *tmp = new bucket[allocated_size];
memset(tmp, 0, sizeof(bucket) * allocated_size);
variable_array.push_back(tmp);
cur = 0;
}
int wp = variable_array.size() - 1;
bucket *b1 = &variable_array[wp][cur + ((g1 >> len1) & 1)];
b1->src = g1;
b1->dst = g2;
b1->weight = weight;
b1->idx = (f_length << 4) + f_length;
b1->subs = 0;
bucket *b2 = &variable_array[wp][cur + ((og1 >> len1) & 1)];
b2->src = og1;
b2->dst = og2;
b2->weight = op[pos].weight;
b2->idx = op[pos].idx;
b2->subs = op[pos].subs;
op[pos].subs = cur / 2;
op[pos].weight = 0;
op[pos].idx = (wp << 8) + (len2 << 4) + len1;
cur += 2;
return;
}
else if (len2 < olen2)
{
len1 = olen1;
if (cur == allocated_size || variable_array.size() == 0)
{
bucket *tmp = new bucket[allocated_size];
memset(tmp, 0, sizeof(bucket) * allocated_size);
variable_array.push_back(tmp);
cur = 0;
}
int wp = variable_array.size() - 1;
bucket *b1 = &variable_array[wp][cur + ((g2 >> len2) & 1)];
b1->src = g1;
b1->dst = g2;
b1->weight = weight;
b1->idx = (f_length << 4) + f_length;
b1->subs = 0;
bucket *b2 = &variable_array[wp][cur + ((og2 >> len2) & 1)];
b2->src = og1;
b2->dst = og2;
b2->weight = op[pos].weight;
b2->idx = op[pos].idx;
b2->subs = op[pos].subs;
op[pos].subs = cur / 2;
op[pos].weight = 0;
op[pos].idx = (wp << 8) + (len2 << 4) + len1;
cur += 2;
return;
}
else if (og1 == g1 && og2 == g2 && op[pos].weight != 0)
{
op[pos].weight += weight;
return;
}
else if (op[pos].weight == 0)
{
findDeepestNode(num, pos, g1, g2, weight);
return;
}
else
{
len1 = olen1;
len2 = olen2;
int nnum = op[pos].idx >> 8;
int npos = op[pos].subs * 2;
int n1 = variable_array[nnum][npos].idx & ((1 << 4) - 1);
int n2 = variable_array[nnum][npos + 1].idx & ((1 << 4) - 1);
if (len1 != n1 && len1 != n2)
{
checkFSIT(nnum, npos + ((g1 >> olen1) & 1), g1, g2, weight);
}
else
{
checkFSIT(nnum, npos + ((g2 >> olen2) & 1), g1, g2, weight);
}
return;
}
}
bool ITeM::findDeepestNode(int num, int pos, unsigned short g1, unsigned short g2, short weight)
{
bucket *op;
if (num == -1)
op = matrix;
else
op = variable_array[num];
unsigned short og1 = op[pos].src;
unsigned short og2 = op[pos].dst;
int olen1 = op[pos].idx & ((1 << 4) - 1);
int olen2 = (op[pos].idx >> 4) & ((1 << 4) - 1);
int len1 = getMaxSameLen(g1, og1);
int len2 = getMaxSameLen(g2, og2);
if (len1 < olen1 || len2 < olen2)
{
return false;
}
else if (og1 == g1 && og2 == g2 && op[pos].weight != 0)
{
op[pos].weight += weight;
return true;
}
else
{
len1 = olen1;
len2 = olen2;
int nnum = op[pos].idx >> 8;
int npos = op[pos].subs * 2;
int n1 = variable_array[nnum][npos].idx & ((1 << 4) - 1);
int n2 = variable_array[nnum][npos + 1].idx & ((1 << 4) - 1);
if ((len1 != n1 && len1 != n2) && findDeepestNode(nnum, npos + ((g1 >> olen1) & 1), g1, g2, weight))
{
return true;
}
else if ((len1 == n1 || len1 == n2) && findDeepestNode(nnum, npos + ((g2 >> olen2) & 1), g1, g2, weight))
{
return true;
}
else if (op[pos].weight == 0)
{
op[pos].src = g1;
op[pos].dst = g2;
op[pos].weight = weight;
return true;
}
else
return false;
}
}
int ITeM::edgeWeightQuery(string s1, string s2)
{
unsigned int hash1 = (*hfunc[0])((unsigned char *)(s1.c_str()), s1.length());
unsigned int hash2 = (*hfunc[0])((unsigned char *)(s2.c_str()), s2.length());
unsigned int tmp = (1 << f_length) - 1;
unsigned short g1 = hash1 & tmp;
if (g1 == 0)
g1 += 1;
unsigned int h1 = (hash1 >> f_length) % width;
unsigned short g2 = hash2 & tmp;
if (g2 == 0)
g2 += 1;
unsigned int h2 = (hash2 >> f_length) % width;
int pos = h1 * width + h2;
if (matrix[pos].src == 0)
{
return 0;
}
else if (matrix[pos].src == g1 && matrix[pos].dst == g2 && matrix[pos].weight != 0)
return matrix[pos].weight;
else
return checkEdgeWeight(-1, pos, g1, g2);
}
int ITeM::edgeWeightQuery(int pos, unsigned short g1, unsigned short g2)
{
if (matrix[pos].src == 0)
{
return 0;
}
else if (matrix[pos].src == g1 && matrix[pos].dst == g2 && matrix[pos].weight != 0)
return matrix[pos].weight;
else
return checkEdgeWeight(-1, pos, g1, g2);
}
int ITeM::checkEdgeWeight(int num, int pos, unsigned short g1, unsigned short g2)
{
bucket *op;
if (num == -1)
op = matrix;
else
op = variable_array[num];
unsigned short og1 = op[pos].src;
unsigned short og2 = op[pos].dst;
int olen1 = op[pos].idx & ((1 << 4) - 1);
int olen2 = (op[pos].idx >> 4) & ((1 << 4) - 1);
int len1 = getMaxSameLen(g1, og1);
int len2 = getMaxSameLen(g2, og2);
if (len1 < olen1 || len2 < olen2)
{
return 0;
}
else if (og1 == 0)
{
return 0;
}
else if (og1 == g1 && og2 == g2 && op[pos].weight != 0)
{
return op[pos].weight;
}
else
{
len1 = olen1;
len2 = olen2;
int nnum = op[pos].idx >> 8;
int npos = op[pos].subs * 2;
int n1 = variable_array[nnum][npos].idx & ((1 << 4) - 1);
int n2 = variable_array[nnum][npos + 1].idx & ((1 << 4) - 1);
if (len1 != n1 && len1 != n2)
{
return checkEdgeWeight(nnum, npos + ((g1 >> olen1) & 1), g1, g2);
}
else
{
return checkEdgeWeight(nnum, npos + ((g2 >> olen2) & 1), g1, g2);
}
}
}
int ITeM::nodeWeightQuery(string s1, int type)
{
unsigned int hash = (*hfunc[0])((unsigned char *)(s1.c_str()), s1.length());
unsigned int tmp = (1 << f_length) - 1;
unsigned short g = hash & tmp;
if (g == 0)
g += 1;
unsigned int h = (hash >> f_length) % width;
int res = 0;
if (type == 0)
{
for (int i = 0; i < width; i++)
{
int pos = h * width + i;
res += checkNodeWeight(-1, pos, g, type);
}
}
else
{
for (int i = 0; i < width; i++)
{
int pos = i * width + h;
res += checkNodeWeight(-1, pos, g, type);
}
}
return res;
}
int ITeM::checkNodeWeight(int num, int pos, unsigned short g, int type)
{
bucket *op;
if (num == -1)
op = matrix;
else
op = variable_array[num];
int res = 0;
if (type == 0)
{
unsigned short og1 = op[pos].src;
int olen1 = op[pos].idx & ((1 << 4) - 1);
int olen2 = (op[pos].idx >> 4) & ((1 << 4) - 1);
int len1 = getMaxSameLen(g, og1);
if (len1 < olen1 || og1 == 0)
{
return 0;
}
if (og1 == g)
{
res += op[pos].weight;
}
if (olen1 != f_length || olen2 != f_length)
{
int nnum = op[pos].idx >> 8;
int npos = op[pos].subs * 2;
res += checkNodeWeight(nnum, npos, g, type);
res += checkNodeWeight(nnum, npos + 1, g, type);
}
}
else
{
unsigned short og2 = op[pos].dst;
int olen1 = op[pos].idx & ((1 << 4) - 1);
int olen2 = (op[pos].idx >> 4) & ((1 << 4) - 1);
int len2 = getMaxSameLen(g, og2);
if (len2 < olen2 || og2 == 0)
{
return 0;
}
if (og2 == g)
{
res += op[pos].weight;
}
if (olen1 != f_length || olen2 != f_length)
{
int nnum = op[pos].idx >> 8;
int npos = op[pos].subs * 2;
res += checkNodeWeight(nnum, npos, g, type);
res += checkNodeWeight(nnum, npos + 1, g, type);
}
}
return res;
}
bool ITeM::reachabilityQuery(string s1, string s2)
{
unsigned int hash1 = (*hfunc[0])((unsigned char *)(s1.c_str()), s1.length());
unsigned int hash2 = (*hfunc[0])((unsigned char *)(s2.c_str()), s2.length());
unsigned int tmp = (1 << f_length) - 1;
unsigned short g1 = hash1 & tmp;
if (g1 == 0)
g1 += 1;
unsigned int h1 = (hash1 >> f_length) % width;
unsigned short g2 = hash2 & tmp;
if (g2 == 0)
g2 += 1;
unsigned int h2 = (hash2 >> f_length) % width;
int pos;
map<unsigned int, bool> checked;
queue<unsigned int> q;
unsigned int e = (h1 << f_length) + g1;
q.push(e);
checked[e] = true;
map<unsigned int, bool>::iterator IT;
while (!q.empty())
{
e = q.front();
h1 = e >> f_length;
g1 = e % (1 << f_length);
int pos = h1 * width + h2;
if (checkReachability(-1, pos, g1, g2))
return true;
vector<unsigned int> nodeset;
for (int i = 0; i < width; i++)
{
pos = h1 * width + i;
getReachabilityNode(-1, pos, i, g1, nodeset);
}
for (int i = 0; i < nodeset.size(); i++)
{
IT = checked.find(nodeset[i]);
if (IT == checked.end())
{
q.push(nodeset[i]);
checked[nodeset[i]] = true;
}
}
q.pop();
}
return false;
}
bool ITeM::checkReachability(int num, int pos, unsigned short g1, unsigned short g2)
{
bucket *op;
if (num == -1)
op = matrix;
else
op = variable_array[num];
unsigned short og1 = op[pos].src;
unsigned short og2 = op[pos].dst;
int olen1 = op[pos].idx & ((1 << 4) - 1);
int olen2 = (op[pos].idx >> 4) & ((1 << 4) - 1);
int len1 = getMaxSameLen(g1, og1);
int len2 = getMaxSameLen(g2, og2);
if (len1 < olen1 || len2 < olen2)
{
return false;
}
else if (og1 == 0)
{
return false;
}
else if (og1 == g1 && og2 == g2 && op[pos].weight != 0)
{
return true;
}
else
{
len1 = olen1;
len2 = olen2;
int nnum = op[pos].idx >> 8;
int npos = op[pos].subs * 2;
int n1 = variable_array[nnum][npos].idx & ((1 << 4) - 1);
int n2 = variable_array[nnum][npos + 1].idx & ((1 << 4) - 1);
if (len1 != n1 && len1 != n2)
{
return checkReachability(nnum, npos + ((g1 >> olen1) & 1), g1, g2);
}
else
{
return checkReachability(nnum, npos + ((g2 >> olen2) & 1), g1, g2);
}
}
}
void ITeM::getReachabilityNode(int num, int pos, int col, unsigned short g1, vector<unsigned int> &set)
{
bucket *op;
if (num == -1)
op = matrix;
else
op = variable_array[num];
unsigned short og1 = op[pos].src;
unsigned short og2 = op[pos].dst;
int olen1 = op[pos].idx & ((1 << 4) - 1);
int olen2 = (op[pos].idx >> 4) & ((1 << 4) - 1);
int len1 = getMaxSameLen(g1, og1);
if (len1 < olen1 || og1 == 0)
{
return;
}
if (og1 == g1 && op[pos].weight != 0)
{
unsigned int tmpk = (col << f_length) + og2;
set.push_back(tmpk);
}
if (olen1 != f_length || olen2 != f_length)
{
int nnum = op[pos].idx >> 8;
int npos = op[pos].subs * 2;
getReachabilityNode(nnum, npos, col, g1, set);
getReachabilityNode(nnum, npos + 1, col, g1, set);
}
return;
}
int ITeM::getMaxSameLen(unsigned short g1, unsigned short g2)
{
int ret = 0;
for (int i = 0; i < f_length; i++)
{
if (((g1 >> i) & 1) == ((g2 >> i) & 1))
{
ret++;
}
else
break;
}
return ret;
}