-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspfc.cpp
559 lines (473 loc) · 14.7 KB
/
spfc.cpp
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
#include <cassert>
#include <cstdlib>
#include <cstdio>
#include <cassert>
#include <cstring>
#include <algorithm>
#include <stack>
#include <vector>
#include <limits>
#include "spfc.hpp"
/**
static const int npoints[NSTATES][N] = {{0, 1, 3, 2},
{0, 2, 3, 1},
{3, 1, 0, 2},
{3, 2, 0, 1}};
static const int tm1[NSTATES][N] = {{1, 0, 0, 2},
{0, 1, 1, 3},
{3, 2, 2, 0},
{2, 3, 3, 1}};
static const int seqnums[NSTATES][N] = {{0, 1, 3, 2},
{0, 3, 1, 2},
{2, 1, 3, 0},
{2, 3, 1, 0}};
static const int tm2[NSTATES][N] = {{1, 0, 2, 0},
{0, 3, 1, 1},
{2, 2, 0, 3},
{3, 1, 3, 2}};
**/
/* utility matrices for seqnum to npoint conversions */
/* use seqnum as index to lookup a npoint number */
/* each row is a different state */
static const int npoints[NSTATES][N] = {{0, 1, 3, 2, 6, 7, 5, 4},
{0, 2, 6, 4, 5, 7, 3, 1},
{0, 4, 5, 1, 3, 7, 6, 2},
{3, 2, 0, 1, 5, 4, 6, 7},
{5, 7, 3, 1, 0, 2, 6, 4},
{6, 2, 3, 7, 5, 1, 0, 4},
{3, 7, 6, 2, 0, 4, 5, 1},
{5, 4, 6, 7, 3, 2, 0, 1},
{6, 4, 0, 2, 3, 1, 5, 7},
{5, 1, 0, 4, 6, 2, 3, 7},
{6, 7, 5, 4, 0, 1, 3, 2},
{3, 1, 5, 7, 6, 4, 0, 2}};
static const int tm1[NSTATES][N] = {{1, 2, 2, 3, 3, 5, 5, 4},
{2, 0, 0, 8, 8, 7, 7, 6},
{0, 1, 1, 9, 9, 11, 11, 10},
{11, 6, 6, 0, 0, 9, 9, 8},
{9, 7, 7, 11, 11, 0, 0, 5},
{10, 8, 8, 6, 6, 4, 4, 0},
{3, 11, 11, 5, 5, 1, 1, 7},
{4, 9, 9, 10, 10, 6, 6, 1},
{5, 10, 10, 1, 1, 3, 3, 9},
{7, 4, 4, 2, 2, 8, 8, 3},
{8, 5, 5, 7, 7, 2, 2, 11},
{6, 3, 3, 4, 4, 10, 10, 2}};
/* utility matrices for npoint to seqnum conversions */
/* use npoint to lookup seqnum */
static const int seqnums[NSTATES][N] = {{0, 1, 3, 2, 7, 6, 4, 5},
{0, 7, 1, 6, 3, 4, 2, 5},
{0, 3, 7, 4, 1, 2, 6, 5},
{2, 3, 1, 0, 5, 4, 6, 7},
{4, 3, 5, 2, 7, 0, 6, 1},
{6, 5, 1, 2, 7, 4, 0, 3},
{4, 7, 3, 0, 5, 6, 2, 1},
{6, 7, 5, 4, 1, 0, 2, 3},
{2, 5, 3, 4, 1, 6, 0, 7},
{2, 1, 5, 6, 3, 0, 4, 7},
{4, 5, 7, 6, 3, 2, 0, 1},
{6, 1, 7, 0, 5, 2, 4, 3}};
static const int tm2[NSTATES][N] = {{1, 2, 3, 2, 4, 5, 3, 5},
{2, 6, 0, 7, 8, 8, 0, 7},
{0, 9, 10, 9, 1, 1, 11, 11},
{6, 0, 6, 11, 9, 0, 9, 8},
{11, 11, 0, 7, 5, 9, 0, 7},
{4, 4, 8, 8, 0, 6, 10, 6},
{5, 7, 5, 3, 1, 1, 11, 11},
{6, 1, 6, 10, 9, 4, 9, 10},
{10, 3, 1, 1, 10, 3, 5, 9},
{4, 4, 8, 8, 2, 7, 2, 3},
{7, 2, 11, 2, 7, 5, 8, 5},
{10, 3, 2, 6, 10, 3, 4, 4}};
int cmpseqnums(const Seqn &a, const Seqn &b){
for (int i=0;i<DIMS;i++){
if (a.arr[i] < b.arr[i])
return -1;
else if (a.arr[i] > b.arr[i])
return 1;
}
return 0;
}
bool is_contiguous(const Seqn a, const Seqn b){
return (b.arr[DIMS-1] - a.arr[DIMS-1] == 1 || b.arr[DIMS-1] - a.arr[DIMS-1] == 0xffffffff);
}
bool contains(Region r, Seqn s){
Point p;
spfc_decode(s, p);
for (int i=0;i<DIMS;i++){
if (p.arr[i] < r.lower[i] || p.arr[i] > r.upper[i])
return false;
}
return true;
}
/* set bit at bitpos */
static void setbit(uint64_t arr[], const int bitpos){
assert(bitpos >= 0 && bitpos < DIMS*64);
arr[bitpos/64] |= (0x8000000000000000ULL >> (bitpos % 64));
}
/* clear bits from [bitpos, end] */
static void clearbits(uint64_t arr[], const int bitpos){
assert(bitpos >= 0 && bitpos < DIMS*64);
arr[bitpos/64] &= ~(0xffffffffffffffffULL >> (bitpos%64));
for (int i=bitpos/64+1;i < DIMS;i++) arr[i] = 0;
}
/* get bit at bitpos */
static bool getbit(const uint64_t arr[], const int bitpos){
assert(bitpos >= 0 && bitpos < DIMS*64);
return ((arr[bitpos/64] & (0x8000000000000000ULL >> (bitpos % 64))) != 0);
}
int spfc_encode(const Point &p, Seqn &sn){
uint64_t coords[DIMS];
for (int i=0;i<DIMS;i++){
coords[i] = p.arr[i] << (64-ORDER);
sn.arr[i] = 0;
}
int level = 0;
int state = 0;
const uint64_t bitmask = 0x8000000000000000ULL;
int pos = 64*DIMS - ORDER*DIMS;
while (level < ORDER) {
int npoint = 0;
int coord_index = DIMS-1;
for (int i=63;i > 63-DIMS;i--){
npoint |= (int)((coords[coord_index--] & bitmask) >> i);
}
int s = seqnums[state][npoint];
//printf("level = %d, state = %d, npoint = %d, seq = %d, next = %d\n",
// level, state, npoint, s, tm2[state][npoint]);
uint32_t mask = 0x01 << (DIMS-1);
for (int i=0;i<DIMS;i++){
if (s & mask)
setbit(sn.arr, pos+i);
mask >>= 1;
}
for (int i=0;i<DIMS;i++){
coords[i] <<= 1;
}
state = tm2[state][npoint];
pos += DIMS;
level += 1;
}
return 0;
}
int spfc_decode(const Seqn &s, Point &p){
uint64_t coords[DIMS];
for (int i=0;i<DIMS;i++){
p.arr[i] = 0;
coords[i] = 0;
}
int level = 0, state = 0, pos = 64*DIMS - ORDER*DIMS;
while (level < ORDER){
int d = 0;
uint32_t mask = 0x01 << (DIMS-1);
for (int i=0;i<DIMS;i++){
if (getbit(s.arr, pos+i))
d |= mask;
mask >>= 1;
}
int npnt = npoints[state][d];
//printf("level = %d, state = %d, seq = %d, npoint = %d, next = %d\n",
// level, state, d, npnt, tm1[state][d]);
mask = 0x01;
int coord_index = DIMS-1;
for (int i=63;i>63-DIMS;i--){
if (i - level >= 0)
coords[coord_index--] |= (npnt & mask) << (i - level);
else
coords[coord_index--] |= (npnt & mask) >> abs(i-level);
mask <<= 1;
}
state = tm1[state][d];
pos += DIMS;
level += 1;
}
for (int i=0;i<DIMS;i++){
coords[i] >>= 64 - ORDER;
p.arr[i] = coords[i];
}
return 0;
}
static int extractnpoints(const Region &r, const int level, int &ql, int &qu){
assert(level >= 0 && level < ORDER);
int pos = 64 - ORDER + level;
ql = qu = 0;
int mask = 0x01 << (DIMS-1);
for (int i=0;i<DIMS;i++){
if (r.lower[i] & (0x8000000000000000ULL >> pos)) ql |= mask;
if (r.upper[i] & (0x8000000000000000ULL >> pos)) qu |= mask;
mask >>= 1;
}
return 0;
}
static int extractsubseqn(const Seqn s, const int pos, int &hkey){
assert(pos >= 64*DIMS - DIMS*ORDER && pos < 64*DIMS);
hkey = 0;
int mask = 0x01 << (DIMS-1);
for (int i=0;i<DIMS;i++){
if (getbit(s.arr, pos+i)) hkey |= mask;
mask >>= 1;
}
return 0;
}
static int restrict_search_region(Region &s, int quadr){
assert(quadr >= 0 && quadr < N);
int mask = 0x01 << (DIMS-1);
for (int i=0;i<DIMS;i++){
int64_t diff = (s.upper[i] - s.lower[i])/2 + 1;
if (quadr & mask){
s.lower[i] += diff;
} else {
s.upper[i] -= diff;
}
mask >>= 1;
}
return 0;
}
static bool regions_coincide(Region ss, Region qr){
for (int i=0;i<DIMS;i++){
if (ss.lower[i] != qr.lower[i] || ss.upper[i] != qr.upper[i])
return false;
}
return true;
}
static int restrict_search_region_by_half(Region &s, int pd, bool lower){
int i = 0;
int mask = 0x01 << (DIMS-1);
while (pd != mask) {
i++;
mask >>= 1;
}
assert(i >= 0 && i < DIMS);
int64_t diff = (s.upper[i] - s.lower[i])/2 + 1;
if (lower)
s.upper[i] -= diff;
else
s.lower[i] += diff;
return 0;
}
static int restrict_query_to_search_space(Region ss, Region &qr){
for (int i=0;i<DIMS;i++){
qr.lower[i] = max(qr.lower[i], ss.lower[i]);
qr.upper[i] = min(qr.upper[i], ss.upper[i]);
}
return 0;
}
typedef struct bt_record_t {
Region ss, qr;
int hkey;
int state;
int level;
int offset;
int n;
} Record;
static int select_quadrant2(Region ss, Region qr, int h, stack<Record> &bt,
int level, const int state,
int offset=0, int n=N){
int quad = -1;
while (n >= 1){
if (n == 1){
quad = npoints[state][offset];
//printf("return quad = %d\n", quad);
break;
}
int maxlower = n/2 - 1;
int minhigher = n/2;
int pd = npoints[state][offset+maxlower]^npoints[state][offset+minhigher];
int ql, qu;
extractnpoints(qr, level, ql, qu);
if ((((npoints[state][offset+maxlower] & pd) == (ql & pd)) ||
((npoints[state][offset+maxlower] & pd) == (qu & pd))) && (offset+maxlower >= h)){
if (((npoints[state][offset+minhigher] & pd) == (ql & pd)
||(npoints[state][offset+minhigher] & pd) == (qu & pd))) {
// save upper quadrant space in backup;
//printf("select: save upper [%d ...]\n", npoints[state][offset+minhigher]);
Region ssupper = ss, qrupper = qr;
restrict_search_region_by_half(ssupper, pd, false);
restrict_query_to_search_space(ssupper, qrupper);
Record rec;
rec.ss = ssupper;
rec.qr = qrupper;
rec.hkey = h;
rec.state = state;
rec.level = level;
rec.offset = offset + minhigher;
rec.n = minhigher;
bt.push(rec);
}
// check lower quadrant space
//printf("select: check lower [... %d]\n", npoints[state][offset+maxlower]);
restrict_search_region_by_half(ss, pd, true);
restrict_query_to_search_space(ss, qr);
n = minhigher;
} else {
if (((npoints[state][offset+minhigher] & pd) == (ql & pd)
||(npoints[state][offset+minhigher] & pd) == (qu & pd)) && offset+n-1 >= h){
// check upper quadrant space
//printf("select: check upper [%d...]\n", npoints[state][offset+minhigher]);
restrict_search_region_by_half(ss, pd, false);
restrict_query_to_search_space(ss, qr);
offset = offset+minhigher;
n = minhigher;
} else {
break;
}
}
}
return quad;;
}
static int select_quadrant(Region ss, Region qr, int h, stack<Record> &bt,
int level, const int state,
int offset=0, int n=N){
if (n == 1) return npoints[state][offset];
int quad = -1;
int maxlower = n/2 - 1;
int minhigher = n/2;
int pd = npoints[state][offset+maxlower]^npoints[state][offset+minhigher];
int ql, qu;
extractnpoints(qr, level, ql, qu);
if ((((npoints[state][offset+maxlower] & pd) == (ql & pd)) ||
((npoints[state][offset+maxlower] & pd) == (qu & pd))) && (offset+maxlower >= h)){
if (((npoints[state][offset+minhigher] & pd) == (ql & pd)
||(npoints[state][offset+minhigher] & pd) == (qu & pd))) {
// save upper quadrant space in backup;
Region ssupper = ss, qrupper = qr;
restrict_search_region_by_half(ssupper, pd, false);
restrict_query_to_search_space(ssupper, qrupper);
Record rec;
rec.ss = ssupper;
rec.qr = qrupper;
rec.hkey = h;
rec.state = state;
rec.level = level;
rec.offset = offset + minhigher;
rec.n = minhigher;
bt.push(rec);
}
// check lower quadrant space
Region sslower = ss;
Region qrlower = qr;
restrict_search_region_by_half(sslower, pd, true);
restrict_query_to_search_space(sslower, qrlower);
quad = select_quadrant(sslower, qrlower, h, bt, level, state, offset, minhigher);
} else {
if ((npoints[state][offset+minhigher] & pd) == (ql & pd)
||(npoints[state][offset+minhigher] & pd) == (qu & pd)){
// check upper quadrant space
Region ssupper = ss;
Region qrupper = qr;
restrict_search_region_by_half(ssupper, pd, false);
restrict_query_to_search_space(ssupper, qrupper);
quad = select_quadrant(ssupper, qrupper, h, bt, level, state, offset+minhigher, minhigher);
}
}
return quad;
}
static bool calc_next_match(Region ss, Region qr, const Seqn prev, Seqn &next){
stack<Record> bt;
vector<Region> sslist, qrlist;
int level = 0, state = 0, pos = 64*DIMS - ORDER*DIMS;
while (level < ORDER){
//printf("(level %d) state = %d, pos = %d\n", level, state, pos);
if (level >= (int)sslist.size()){
sslist.push_back(ss);
qrlist.push_back(qr);
} else {
ss = sslist[level];
qr = qrlist[level];
}
int h;
extractsubseqn(prev, pos, h);
//printf("h = %d\n", h);
int quad = select_quadrant2(ss, qr, h, bt, level, state);
//printf("quad = %d\n", quad);
if (quad < 0){
if (!bt.empty()){
Record rec = bt.top();
level = rec.level;
state = rec.state;
pos = 64*DIMS - ORDER*DIMS + level*DIMS;
h = rec.hkey;
ss = sslist[level];
qr = qrlist[level];
clearbits(next.arr, pos);
quad = select_quadrant2(rec.ss, rec.qr, 0, bt, level, state, rec.offset, rec.n);
//printf("BT: level = %d, pos = %d, h = %d, quad = %d\n", level, pos, h, quad);
//printf("BT: ss x %u to %u, y %u to %u\n", ss.lower[0], ss.upper[0], ss.lower[1], ss.upper[1]);
//printf("BT: qr x %u to %u, y %u to %u\n", qr.lower[0], qr.upper[0], qr.lower[1], qr.upper[1]);
} else {
//printf("No next match found\n");
next.arr[0] = next.arr[1] = next.arr[1] = numeric_limits<uint32_t>::max();
return false;
}
}
if (level < ORDER){
restrict_search_region(ss, quad);
restrict_query_to_search_space(ss, qr);
//printf("restrict search, query spaces\n");
//printf("ss: x %u to %u, y %u to %u\n", ss.lower[0], ss.upper[0], ss.lower[1], ss.upper[1]);
//printf("qr: x %u to %u, y %u to %u\n", qr.lower[0], qr.upper[0], qr.lower[1], qr.upper[1]);
}
int quads = seqnums[state][quad];
//printf("quads = %d\n", quads);
if (h == quads && regions_coincide(ss, qr)){
//printf("(h=%d) == (quads=%d) and ss == qr\n", h, quads);
next = prev;
return true;
}
//printf("quads = %d\n", quads);
uint32_t mask = 0x01 << (DIMS-1);
for (int i=0;i<DIMS;i++){
if (quads & mask)
setbit(next.arr, pos+i);
mask >>= 1;
}
state = tm2[state][quad];
level++;
pos += DIMS;
//printf("++++++++++++++++++++++++++++++++++++++\n");
if (h < quads){
//printf("quads =%d > h %d\n", quads, h);
break;
}
}
while (level < ORDER){
//printf("(level %d) state = %d, pos = %d\n", level, state, pos);
if (regions_coincide(ss, qr)){
//printf("ss == qr\n");
clearbits(next.arr, pos);
break;
}
int quad = select_quadrant2(ss, qr, 0, bt, level, state);
//printf("quad = %d\n", quad);
assert(quad >= 0 && quad < N);
if (level < ORDER){
restrict_search_region(ss, quad);
restrict_query_to_search_space(ss, qr);
//printf("restrict search, query spaces\n");
//printf("ss: x %u to %u, y %u to %u\n", ss.lower[0], ss.upper[0], ss.lower[1], ss.upper[1]);
//printf("qr: x %u to %u, y %u to %u\n", qr.lower[0], qr.upper[0], qr.lower[1], qr.upper[1]);
}
int quads = seqnums[state][quad];
//printf("quads = %d\n", quads);
uint32_t mask = 0x01 << (DIMS-1);
for (int i=0;i<DIMS;i++){
if (quads & mask)
setbit(next.arr, pos+i);
mask >>= 1;
}
state = tm2[state][quad];
level++;
pos += DIMS;
//printf("--------------------------------------\n");
}
return true;
}
bool next_match(Region qr, const Seqn prevkey, Seqn &nextkey){
memset(nextkey.arr, 0, DIMS*sizeof(uint64_t));
Region ss;
for (int i=0;i<DIMS;i++){
ss.lower[i] = 0;
ss.upper[i] = 0xffffffffffffffffULL >> (64-ORDER);
}
return calc_next_match(ss, qr, prevkey, nextkey);
}