forked from migumar2/libCSD
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStringDictionaryPFC.cpp
532 lines (433 loc) · 15.6 KB
/
StringDictionaryPFC.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
/* StringDictionaryPFC.cpp
* Copyright (C) 2014, Francisco Claude & Rodrigo Canovas & Miguel A.
* Martinez-Prieto all rights reserved.
*
* This class implements a Compressed String Dictionary which differentially
* encodes the strings using (Plain) Front-Coding.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*
* Contacting the authors:
* Francisco Claude: [email protected]
* Rodrigo Canovas: [email protected]
* Miguel A. Martinez-Prieto: [email protected]
*/
#include "StringDictionaryPFC.h"
#include "iterators/IteratorDictStringPFC.h"
StringDictionaryPFC::StringDictionaryPFC() {
this->type = PFC;
this->elements = 0;
this->maxlength = 0;
this->buckets = 0;
this->bucketsize = 0;
this->bytesStrings = 0;
}
StringDictionaryPFC::StringDictionaryPFC(IteratorDictString *it,
uint bucketsize) {
this->type = PFC;
this->elements = 0;
this->maxlength = 0;
if (bucketsize < 2) {
std::cerr << "[WARNING] The bucketsize value must be greater than 1. ";
std::cerr << "The dictionary is built using buckets of size 2" << std::endl;
this->bucketsize = 2;
} else
this->bucketsize = bucketsize;
this->buckets = 0;
this->bytesStrings = 0;
// Bulding the Front-Coding representation
uchar *strCurrent = NULL, *strPrev = NULL;
uint lenCurrent = 0, lenPrev = 0;
// Variables for strings management
size_t reservedStrings = MEMALLOC * bucketsize;
textStrings = new uchar[reservedStrings];
std::vector<size_t> xblStrings;
xblStrings.push_back(bytesStrings);
while (it->hasNext()) {
strCurrent = it->next(&lenCurrent);
if (lenCurrent >= maxlength)
maxlength = lenCurrent + 1;
// Checking the available space in textStrings and
// realloc if required
while ((bytesStrings + (2 * lenCurrent)) > reservedStrings)
reservedStrings = Reallocate(&textStrings, reservedStrings);
if ((elements % bucketsize) == 0) {
// First string in the current bucket!
// ===================================
// Updating the positional index
xblStrings.push_back(bytesStrings);
buckets++;
// The string is explicitly copied
strcpy((char *)(textStrings + bytesStrings), (char *)strCurrent);
bytesStrings += lenCurrent;
} else {
// Regular string
// ==============
// Obtaining the long common prefix: lcp
uint lcp = 0;
longestCommonPrefix(strPrev, strCurrent, lenPrev, &lcp);
// The lcp value is encoded (VByte)
bytesStrings += VByte::encode(lcp, textStrings + bytesStrings);
// The remaining suffix is explicitly copied
strncpy((char *)(textStrings + bytesStrings), (char *)strCurrent + lcp,
lenCurrent - lcp);
bytesStrings += lenCurrent - lcp;
}
textStrings[bytesStrings] = '\0';
bytesStrings++;
// New string processed
elements++;
strPrev = strCurrent;
lenPrev = lenCurrent;
}
delete it;
// Obtaining the positional indexes
xblStrings.push_back(bytesStrings);
blStrings = new LogSequence(&xblStrings, bits(bytesStrings));
}
unsigned long StringDictionaryPFC::locate(uchar *str, uint) {
unsigned long id = NORESULT;
// Locating the candidate bucket for the string
size_t idbucket;
bool cmp = locateBucket(str, &idbucket);
// The string is the header of the bucket
if (cmp)
return ((idbucket - 1) * bucketsize) + 1;
else {
// The string is previous to any other one in the dictionary
if (idbucket != NORESULT) {
// The bucket is sequentially scanned to find the string
uchar *decoded;
uint decLen;
uchar *ptr = getHeader(idbucket, &decoded, &decLen);
uint scanneable = bucketsize;
if ((idbucket == buckets) && ((elements % bucketsize) != 0))
scanneable = (elements % bucketsize);
if (scanneable > 1) {
uint sharedCurr = 0, sharedPrev = 0;
int cmp = 0;
// Processing the first internal string
ptr += VByte::decode(&sharedPrev, ptr);
decodeNextString(&ptr, sharedPrev, decoded, &decLen);
cmp = longestCommonPrefix(decoded + sharedCurr, str + sharedCurr,
decLen - sharedCurr + 1, &sharedCurr);
if (cmp != 0) {
for (uint i = 2; i < scanneable; i++) {
ptr += VByte::decode(&sharedPrev, ptr);
if (sharedPrev < sharedCurr)
break;
decodeNextString(&ptr, sharedPrev, decoded, &decLen);
if (sharedPrev == sharedCurr)
cmp = longestCommonPrefix(decoded + sharedCurr, str + sharedCurr,
decLen - sharedCurr + 1, &sharedCurr);
if (cmp == 0) {
id = ((idbucket - 1) * bucketsize) + i + 1;
delete[] decoded;
return id;
} else if (cmp > 0)
break;
}
} else
id = ((idbucket - 1) * bucketsize) + 2;
}
delete[] decoded;
}
}
return id;
}
uchar *StringDictionaryPFC::extract(size_t id, uint *strLen) {
if ((id > 0) && (id <= elements)) {
uint idbucket = 1 + ((id - 1) / bucketsize);
uint pos = ((id - 1) % bucketsize);
uchar *decoded;
uint decLen;
uchar *ptr = getHeader(idbucket, &decoded, &decLen);
uint lenPrefix;
if (pos > 0)
for (uint i = 1; i <= pos; i++) {
ptr += VByte::decode(&lenPrefix, ptr);
decodeNextString(&ptr, lenPrefix, decoded, &decLen);
}
*strLen = decLen;
return decoded;
} else {
*strLen = 0;
return NULL;
}
}
IteratorDictID *StringDictionaryPFC::locatePrefix(uchar *str, uint strLen) {
size_t leftBucket = 1, rightBucket = buckets;
size_t leftID = 0, rightID = 0;
// Locating the candidate buckets for the prefix
locateBoundaryBuckets(str, strLen, &leftBucket, &rightBucket);
if (leftBucket > NORESULT) {
uchar *decoded;
uint decLen;
uchar *ptr = getHeader(leftBucket, &decoded, &decLen);
uint scanneable = bucketsize;
if ((leftBucket == buckets) && ((elements % bucketsize) != 0))
scanneable = (elements % bucketsize);
if (leftBucket == rightBucket) {
// All candidate results are in the same bucket
leftID = searchPrefix(&ptr, scanneable, decoded, &decLen, str, strLen);
// No strings use the required prefix
if (leftID == NORESULT)
return new IteratorDictIDContiguous(NORESULT, NORESULT);
else
rightID = leftID +
searchDistinctPrefix(ptr, scanneable - leftID, decoded,
&decLen, str, strLen) -
1;
leftID += (leftBucket - 1) * bucketsize;
rightID += (rightBucket - 1) * bucketsize;
} else {
// All prefixes exceed (possibly) a single bucket
{
// Searching the left limit
leftID = searchPrefix(&ptr, scanneable, decoded, &decLen, str, strLen);
// The first prefix is the next bucket header
if (leftID == NORESULT)
leftID = leftBucket * bucketsize + 1;
// The first prefix is an internal string of the leftBucket
else
leftID += (leftBucket - 1) * bucketsize;
}
{
// Searching the right limit
delete[] decoded;
ptr = getHeader(rightBucket, &decoded, &decLen);
scanneable = bucketsize;
if ((rightBucket == buckets) && ((elements % bucketsize) != 0))
scanneable = (elements % bucketsize);
rightID = searchDistinctPrefix(ptr, scanneable - 1, decoded, &decLen,
str, strLen);
rightID += (rightBucket - 1) * bucketsize;
}
}
delete[] decoded;
return new IteratorDictIDContiguous(leftID, rightID);
} else {
// No strings use the required prefix
return new IteratorDictIDContiguous(NORESULT, NORESULT);
}
}
IteratorDictID *StringDictionaryPFC::locateSubstr(uchar *, uint) {
std::cerr << "This dictionary does not provide substring location"
<< std::endl;
return NULL;
}
uint StringDictionaryPFC::locateRank(uint rank) { return rank; }
IteratorDictString *StringDictionaryPFC::extractPrefix(uchar *str,
uint strLen) {
IteratorDictIDContiguous *it =
(IteratorDictIDContiguous *)locatePrefix(str, strLen);
if (it->getLeftLimit() != NORESULT) {
// Positioning the LEFT Limit
size_t left = it->getLeftLimit();
uint leftbucket = 1 + ((left - 1) / bucketsize);
uint leftpos = ((left - 1) % bucketsize);
// Positioning the RIGHT Limit
size_t right = it->getRightLimit();
delete it;
size_t ptrS = blStrings->getField(leftbucket);
return new IteratorDictStringPFC(textStrings + ptrS, leftpos, bucketsize,
right - left + 1, maxlength);
} else
return NULL;
}
IteratorDictString *StringDictionaryPFC::extractSubstr(uchar *, uint) {
std::cerr << "This dictionary does not provide substring extraction"
<< std::endl;
return 0;
}
uchar *StringDictionaryPFC::extractRank(uint rank, uint *strLen) {
return extract(rank, strLen);
}
IteratorDictString *StringDictionaryPFC::extractTable() {
size_t ptrS = blStrings->getField(1);
return new IteratorDictStringPFC(textStrings + ptrS, 0, bucketsize, elements,
maxlength);
}
size_t StringDictionaryPFC::getSize() {
return (bytesStrings * sizeof(uchar)) + blStrings->getSize() +
sizeof(StringDictionaryPFC);
}
void StringDictionaryPFC::save(std::ostream &out) {
saveValue<uint32_t>(out, type);
saveValue<uint64_t>(out, elements);
saveValue<uint32_t>(out, maxlength);
saveValue<uint32_t>(out, buckets);
saveValue<uint32_t>(out, bucketsize);
saveValue<uint64_t>(out, bytesStrings);
saveValue<uchar>(out, textStrings, bytesStrings);
blStrings->save(out);
}
StringDictionary *StringDictionaryPFC::load(std::istream &in) {
size_t type = loadValue<uint32_t>(in);
if (type != PFC)
return NULL;
StringDictionaryPFC *dict = new StringDictionaryPFC();
dict->type = PFC;
dict->elements = loadValue<uint64_t>(in);
dict->maxlength = loadValue<uint32_t>(in);
dict->buckets = loadValue<uint32_t>(in);
dict->bucketsize = loadValue<uint32_t>(in);
dict->bytesStrings = loadValue<uint64_t>(in);
dict->textStrings = loadValue<uchar>(in, dict->bytesStrings);
dict->blStrings = new LogSequence(in);
return dict;
}
inline uchar *StringDictionaryPFC::getHeader(size_t idbucket, uchar **str,
uint *strLen) {
uchar *ptr = textStrings + blStrings->getField(idbucket);
*strLen = strlen((char *)ptr);
*str = new uchar[maxlength];
memcpy((char *)*str, (char *)ptr, *strLen + 1);
return ptr + (*strLen) + 1;
}
void StringDictionaryPFC::decodeNextString(uchar **ptr, uint lenPrefix,
uchar *str, uint *strLen) {
uint lenSuffix;
lenSuffix = strlen((char *)*ptr);
memcpy((char *)(str + lenPrefix), (char *)*ptr, lenSuffix + 1);
*ptr += lenSuffix + 1;
*strLen = lenPrefix + lenSuffix;
}
bool StringDictionaryPFC::locateBucket(uchar *str, size_t *idbucket) {
size_t left = 1, right = buckets, center = 0;
int cmp = 0;
while (left <= right) {
center = (left + right) / 2;
cmp = strcmp((char *)(textStrings + blStrings->getField(center)),
(char *)str);
// The string is in any preceding bucket
if (cmp > 0)
right = center - 1;
// The string is in any subsequent bucket
else if (cmp < 0)
left = center + 1;
// The string is the first one in the c-th bucket
else {
*idbucket = center;
return true;
}
}
// c is the candidate bucket for the string
if (cmp < 0)
*idbucket = center;
// c-1 is the candidate bucket for the string
else
*idbucket = center - 1;
return false;
}
void StringDictionaryPFC::locateBoundaryBuckets(uchar *str, uint strLen,
size_t *left, size_t *right) {
size_t center = 0;
int cmp = 0;
while (*left <= *right) {
center = (*left + *right) / 2;
cmp = strncmp((char *)(textStrings + blStrings->getField(center)),
(char *)str, strLen);
if (cmp > 0)
*right = center - 1;
else if (cmp < 0)
*left = center + 1;
else
break;
}
if (cmp != 0) {
// All prefixes are in the same block
if (cmp < 0) {
*left = center;
*right = center;
} else {
*left = center - 1;
*right = center - 1;
}
return;
}
if (center > 1) {
// Looking for the left boundary
uint ll = *left, lr = center - 1, lc;
while (ll <= lr) {
lc = (ll + lr) / 2;
cmp = strncmp((char *)(textStrings + blStrings->getField(lc)),
(char *)str, strLen);
if (cmp == 0)
lr = lc - 1;
else
ll = lc + 1;
}
if (lr > NORESULT)
*left = lr;
else
*left = 1;
}
if (center < buckets) {
// Looking for the right boundary
uint rl = center, rr = *right + 1, rc;
while (rl < (rr - 1)) {
rc = (rl + rr) / 2;
cmp = strncmp((char *)(textStrings + blStrings->getField(rc)),
(char *)str, strLen);
if (cmp == 0)
rl = rc;
else
rr = rc;
}
*right = rl;
}
}
uint StringDictionaryPFC::searchPrefix(uchar **ptr, uint scanneable,
uchar *decoded, uint *decLen, uchar *str,
uint strLen) {
uint sharedCurr = 0, sharedPrev = 0;
int cmp = 0;
uint id = 1;
while (true) {
cmp = longestCommonPrefix(decoded + sharedCurr, str + sharedCurr,
*decLen - sharedCurr, &sharedCurr);
if (sharedCurr == strLen)
break;
else {
id++;
if ((cmp > 0) || (id > scanneable))
break;
*ptr += VByte::decode(&sharedPrev, *ptr);
if (sharedPrev < sharedCurr)
break;
decodeNextString(ptr, sharedPrev, decoded, decLen);
}
}
return id;
}
uint StringDictionaryPFC::searchDistinctPrefix(uchar *ptr, uint scanneable,
uchar *decoded, uint *decLen,
uchar *, uint strLen) {
uint id = 1;
uint lenPrefix;
for (id = 1; id <= scanneable; id++) {
ptr += VByte::decode(&lenPrefix, ptr);
if (lenPrefix < strLen)
break;
decodeNextString(&ptr, lenPrefix, decoded, decLen);
}
return id;
}
StringDictionaryPFC::~StringDictionaryPFC() {
delete[] textStrings;
delete blStrings;
}