-
Notifications
You must be signed in to change notification settings - Fork 2
/
sopang.cpp
573 lines (459 loc) · 16.9 KB
/
sopang.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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#include "sopang.hpp"
#include <algorithm>
#include <cassert>
using namespace std;
namespace sopang
{
Sopang::Sopang(const std::string &alphabet)
:alphabet(alphabet)
{
dBuffer = new uint64_t[dBufferSize];
initCounterPositionMasks();
}
Sopang::~Sopang()
{
delete[] dBuffer;
}
unordered_set<int> Sopang::match(const string *const *segments,
int nSegments,
const int *segmentSizes,
const string &pattern)
{
assert(nSegments > 0 and pattern.size() > 0 and pattern.size() <= wordSize);
fillPatternMaskBuffer(pattern);
const uint64_t hitMask = (0x1ULL << (pattern.size() - 1));
uint64_t D = allOnes;
unordered_set<int> res;
for (int iS = 0; iS < nSegments; ++iS)
{
assert(segmentSizes[iS] > 0 and static_cast<size_t>(segmentSizes[iS]) <= dBufferSize);
for (int iD = 0; iD < segmentSizes[iS]; ++iD)
{
dBuffer[iD] = D;
for (size_t iC = 0; iC < segments[iS][iD].size(); ++iC)
{
const char c = segments[iS][iD][iC];
assert(c > 0 and static_cast<unsigned char>(c) < maskBufferSize);
assert(alphabet.find(c) != string::npos);
dBuffer[iD] <<= 1;
dBuffer[iD] |= maskBuffer[static_cast<unsigned char>(c)];
// Match occurred. Note: we still continue in order to fill the whole d-buffer.
if ((dBuffer[iD] & hitMask) == 0x0ULL)
{
res.insert(iS);
}
}
}
D = dBuffer[0];
for (int iD = 1; iD < segmentSizes[iS]; ++iD)
{
// As a join operation we want to preserve 0s (active states):
// a match can occur in any segment alternative.
D &= dBuffer[iD];
}
}
return res;
}
unordered_set<int> Sopang::matchApprox(const string *const *segments,
int nSegments,
const int *segmentSizes,
const string &pattern,
int k)
{
assert(nSegments > 0 and pattern.size() > 0 and pattern.size() <= maxPatternApproxSize);
assert(k > 0);
unordered_set<int> res;
fillPatternMaskBufferApprox(pattern);
// This is the initial position of each counter, after k + 1 errors the most significant bit will be set.
const uint64_t counterMask = 0xFULL - k;
// Hit mask indicates whether the most significant bit in the last counter is set.
const uint64_t hitMask = (0x1ULL << ((pattern.size() * saCounterSize) - 1));
uint64_t D = 0x0ULL;
for (size_t i = 0; i < pattern.size(); ++i)
{
// We initialize the state with full counters which allow us to effectively start matching
// after m characters.
D |= (saFullCounter << (i * saCounterSize));
}
for (int iS = 0; iS < nSegments; ++iS)
{
assert(segmentSizes[iS] > 0 and static_cast<size_t>(segmentSizes[iS]) <= dBufferSize);
for (int iD = 0; iD < segmentSizes[iS]; ++iD)
{
dBuffer[iD] = D;
for (size_t iC = 0; iC < segments[iS][iD].size(); ++iC)
{
const char c = segments[iS][iD][iC];
assert(c > 0 and static_cast<unsigned char>(c) < maskBufferSize);
assert(alphabet.find(c) != string::npos);
dBuffer[iD] <<= saCounterSize;
dBuffer[iD] += counterMask;
dBuffer[iD] += maskBuffer[static_cast<unsigned char>(c)];
if ((dBuffer[iD] & hitMask) == 0x0ULL)
{
res.insert(iS);
}
}
}
D = 0x0ULL;
// As a join operation, we take the minimum (the most promising alternative) from each counter.
for (size_t i = 0; i < pattern.size(); ++i)
{
uint64_t min = (dBuffer[0] & counterPosMasks[i]);
for (int iD = 1; iD < segmentSizes[iS]; ++iD)
{
uint64_t cur = (dBuffer[iD] & counterPosMasks[i]);
if (cur < min)
{
min = cur;
}
}
D |= min;
}
}
return res;
}
namespace
{
bool verifyMatch(const string *const *segments,
const int *segmentSizes,
const Sopang::SourceMap &sourceMap,
int sourceCount,
const string &pattern,
int matchIdx,
const pair<int, int> &match)
{
using SourceSet = Sopang::SourceSet;
const int patternCharIdx = static_cast<int>(pattern.size()) - match.second - 2;
if (patternCharIdx < 0) // The match is fully contained within a single segment.
return true;
vector<pair<SourceSet, int>> leaves;
SourceSet rootSources(sourceCount);
if (sourceMap.count(matchIdx) > 0)
{
rootSources = sourceMap.at(matchIdx)[match.first];
}
else
{
rootSources.set();
}
leaves.emplace_back(move(rootSources), patternCharIdx);
int segmentIdx = static_cast<int>(matchIdx - 1);
while (not leaves.empty() and segmentIdx >= 0)
{
if (segmentSizes[segmentIdx] == 1)
{
for (auto &leaf : leaves)
{
leaf.second -= segments[segmentIdx][0].size();
if (leaf.second < 0)
return true;
}
}
else
{
assert(sourceMap.count(segmentIdx) > 0 and sourceMap.at(segmentIdx).size() == static_cast<size_t>(segmentSizes[segmentIdx]));
vector<pair<SourceSet, int>> newLeaves;
newLeaves.reserve(leaves.size() * segmentSizes[segmentIdx]);
for (const auto &leaf : leaves)
{
for (int variantIdx = 0; variantIdx < segmentSizes[segmentIdx]; ++variantIdx)
{
const SourceSet &variantSources = sourceMap.at(segmentIdx)[variantIdx];
if (segments[segmentIdx][variantIdx].empty())
{
const SourceSet newSources = (variantSources & leaf.first);
if (newSources.any())
{
newLeaves.emplace_back(move(newSources), leaf.second);
}
}
else
{
int curCharIdx = static_cast<int>(segments[segmentIdx][variantIdx].size()) - 1;
int curPatternIdx = static_cast<int>(leaf.second);
while (curCharIdx >= 0)
{
if (curPatternIdx < 0)
{
const SourceSet newSources = (variantSources & leaf.first);
if (newSources.any())
return true;
}
if (pattern[curPatternIdx] != segments[segmentIdx][variantIdx][curCharIdx])
break;
curCharIdx -= 1;
curPatternIdx -= 1;
}
if (curPatternIdx < 0)
{
const SourceSet newSources = (variantSources & leaf.first);
if (newSources.any())
return true;
}
if (curCharIdx < 0)
{
const SourceSet newSources = (variantSources & leaf.first);
if (newSources.any())
{
newLeaves.emplace_back(move(newSources), curPatternIdx);
}
}
}
}
}
leaves = newLeaves;
}
segmentIdx -= 1;
}
return false;
}
Sopang::SourceSet calcMatchSources(const string *const *segments,
const int *segmentSizes,
const Sopang::SourceMap &sourceMap,
int sourceCount,
const string &pattern,
int matchIdx,
const pair<int, int> &match,
bool &deterministicSegmentMatch)
{
using SourceSet = Sopang::SourceSet;
const int patternCharIdx = static_cast<int>(pattern.size()) - match.second - 2;
if (patternCharIdx < 0) // The match is fully contained within a single segment.
{
if (sourceMap.count(matchIdx) > 0)
{
assert(match.first >= 0 and match.first < static_cast<int>(sourceMap.at(matchIdx).size()));
return sourceMap.at(matchIdx)[match.first];
}
deterministicSegmentMatch = true;
return SourceSet(sourceCount);
}
vector<pair<SourceSet, int>> leaves;
SourceSet rootSources(sourceCount);
if (sourceMap.count(matchIdx) > 0)
{
assert(match.first >= 0 and match.first < static_cast<int>(sourceMap.at(matchIdx).size()));
rootSources = sourceMap.at(matchIdx)[match.first];
}
else
{
rootSources.set();
}
leaves.emplace_back(move(rootSources), patternCharIdx);
int segmentIdx = static_cast<int>(matchIdx - 1);
SourceSet res(sourceCount);
while (not leaves.empty() and segmentIdx >= 0)
{
vector<pair<SourceSet, int>> newLeaves;
newLeaves.reserve(leaves.size() * segmentSizes[segmentIdx]);
if (segmentSizes[segmentIdx] == 1)
{
for (auto &leaf : leaves)
{
leaf.second -= segments[segmentIdx][0].size();
if (leaf.second < 0)
{
res |= leaf.first;
}
else
{
newLeaves.push_back(leaf);
}
}
leaves = newLeaves;
}
else
{
assert(sourceMap.count(segmentIdx) > 0 and sourceMap.at(segmentIdx).size() == static_cast<size_t>(segmentSizes[segmentIdx]));
for (const auto &leaf : leaves)
{
for (int variantIdx = 0; variantIdx < segmentSizes[segmentIdx]; ++variantIdx)
{
const SourceSet &variantSources = sourceMap.at(segmentIdx)[variantIdx];
if (segments[segmentIdx][variantIdx].empty())
{
const SourceSet newSources = (variantSources & leaf.first);
if (newSources.any())
{
newLeaves.emplace_back(move(newSources), leaf.second);
}
}
else
{
int curCharIdx = static_cast<int>(segments[segmentIdx][variantIdx].size()) - 1;
int curPatternIdx = static_cast<int>(leaf.second);
while (curCharIdx >= 0)
{
if (curPatternIdx < 0)
break;
if (pattern[curPatternIdx] != segments[segmentIdx][variantIdx][curCharIdx])
break;
curCharIdx -= 1;
curPatternIdx -= 1;
}
if (curPatternIdx < 0)
{
const SourceSet newSources = (variantSources & leaf.first);
if (newSources.any())
{
res |= newSources;
continue;
}
}
if (curCharIdx < 0)
{
const SourceSet newSources = (variantSources & leaf.first);
if (newSources.any())
{
newLeaves.emplace_back(move(newSources), curPatternIdx);
}
}
}
}
}
leaves = newLeaves;
}
segmentIdx -= 1;
}
return res;
}
} // namespace (anonymous)
unordered_set<int> Sopang::matchWithSourcesVerify(const string *const *segments,
int nSegments,
const int *segmentSizes,
const Sopang::SourceMap &sourceMap,
int sourceCount,
const string &pattern)
{
const IndexToMatchMap indexToMatch = calcIndexToMatchMap(segments, nSegments, segmentSizes, pattern);
unordered_set<int> res;
for (const auto &kv : indexToMatch)
{
for (const auto &match : kv.second)
{
if (verifyMatch(segments, segmentSizes, sourceMap, sourceCount, pattern, kv.first, match))
{
res.insert(kv.first);
break;
}
}
}
return res;
}
unordered_map<int, Sopang::SourceSet> Sopang::matchWithSources(const string *const *segments,
int nSegments,
const int *segmentSizes,
const Sopang::SourceMap &sourceMap,
int sourceCount,
const string &pattern)
{
const IndexToMatchMap indexToMatch = calcIndexToMatchMap(segments, nSegments, segmentSizes, pattern);
unordered_map<int, SourceSet> res;
for (const auto &kv : indexToMatch)
{
for (const auto &match : kv.second)
{
bool deterministicSegmentMatch = false;
const SourceSet curSources = calcMatchSources(segments, segmentSizes, sourceMap, sourceCount, pattern, kv.first, match, deterministicSegmentMatch);
if (not curSources.empty())
{
if (res.count(kv.first) > 0)
{
res.find(kv.first)->second |= curSources;
}
else
{
res.emplace(kv.first, move(curSources));
}
}
else if (deterministicSegmentMatch)
{
res.emplace(kv.first, sourceCount);
}
}
}
return res;
}
Sopang::IndexToMatchMap Sopang::calcIndexToMatchMap(const string *const *segments,
int nSegments,
const int *segmentSizes,
const string &pattern)
{
assert(nSegments > 0 and pattern.size() > 0 and pattern.size() <= wordSize);
fillPatternMaskBuffer(pattern);
const uint64_t hitMask = (0x1ULL << (pattern.size() - 1));
uint64_t D = allOnes;
// segment index -> [(variant index, char in variant index)]
IndexToMatchMap res;
res.reserve(matchMapReserveSize);
for (int iS = 0; iS < nSegments; ++iS)
{
assert(segmentSizes[iS] > 0 and static_cast<size_t>(segmentSizes[iS]) <= dBufferSize);
for (int iD = 0; iD < segmentSizes[iS]; ++iD)
{
dBuffer[iD] = D;
for (size_t iC = 0; iC < segments[iS][iD].size(); ++iC)
{
const char c = segments[iS][iD][iC];
assert(c > 0 and static_cast<unsigned char>(c) < maskBufferSize);
assert(alphabet.find(c) != string::npos);
dBuffer[iD] <<= 1;
dBuffer[iD] |= maskBuffer[static_cast<unsigned char>(c)];
if ((dBuffer[iD] & hitMask) == 0x0ULL)
{
res[iS].emplace_back(iD, static_cast<int>(iC));
}
}
}
D = dBuffer[0];
for (int iD = 1; iD < segmentSizes[iS]; ++iD)
{
D &= dBuffer[iD];
}
}
return res;
}
void Sopang::initCounterPositionMasks()
{
for (size_t i = 0; i < maxPatternApproxSize; ++i)
{
counterPosMasks[i] = (saCounterAllSet << (i * saCounterSize));
}
}
void Sopang::fillPatternMaskBuffer(const string &pattern)
{
assert(pattern.size() > 0 and pattern.size() <= wordSize);
assert(alphabet.size() > 0);
for (const char c : alphabet)
{
assert(c > 0 and static_cast<unsigned char>(c) < maskBufferSize);
maskBuffer[static_cast<unsigned char>(c)] = allOnes;
}
for (size_t iC = 0; iC < pattern.size(); ++iC)
{
assert(pattern[iC] > 0 and static_cast<unsigned char>(pattern[iC]) < maskBufferSize);
maskBuffer[static_cast<unsigned char>(pattern[iC])] &= (~(0x1ULL << iC));
}
}
void Sopang::fillPatternMaskBufferApprox(const string &pattern)
{
assert(pattern.size() > 0 and pattern.size() <= wordSize);
assert(alphabet.size() > 0);
for (const char c : alphabet)
{
assert(c > 0 and static_cast<unsigned char>(c) < maskBufferSize);
maskBuffer[static_cast<unsigned char>(c)] = 0x0ULL;
for (size_t iC = 0; iC < pattern.size(); ++iC)
{
maskBuffer[static_cast<unsigned char>(c)] |= (0x1ULL << (iC * saCounterSize));
}
}
for (size_t iC = 0; iC < pattern.size(); ++iC)
{
assert(pattern[iC] > 0 and static_cast<unsigned char>(pattern[iC]) < maskBufferSize);
// We zero the bit at the counter position corresponding to the current character in the pattern.
maskBuffer[static_cast<unsigned char>(pattern[iC])] &= (~(0x1ULL << (iC * saCounterSize)));
}
}
} // namespace sopang