-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathStringDictionaryHHTFC.cpp
855 lines (688 loc) · 22.3 KB
/
StringDictionaryHHTFC.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
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
/* StringDictionaryHHTFC.cpp
* Copyright (C) 2014, Francisco Claude & Rodrigo Canovas & Miguel A. Martinez-Prieto
* all rights reserved.
*
* This class implements a compressed StringDictionaryHHTFC combining Huffman
* and Hu-Tucker compression over a dictionary of strings encoded with
* FrontCoding.
*
* 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 "StringDictionaryHHTFC.h"
StringDictionaryHHTFC::StringDictionaryHHTFC()
{
this->type = HHTFC;
this->elements = 0;
this->maxlength = 0;
this->maxcomplength = 0;
this->buckets = 0;
this->bucketsize = 0;
this->bytesStrings = 0;
}
StringDictionaryHHTFC::StringDictionaryHHTFC(IteratorDictString *it, uint bucketsize)
{
this->type = HHTFC;
if (bucketsize < 2)
{
cerr << "[WARNING] The bucketsize value must be greater than 1. ";
cerr << "The dictionary is built using buckets of size 2" << endl;
this->bucketsize = 2;
}
else this->bucketsize = bucketsize;
// 1) Bulding the Front-Coding representation
StringDictionaryPFC *dict = new StringDictionaryPFC(it, this->bucketsize);
this->maxlength = dict->maxlength;
this->elements = dict->elements;
this->buckets = dict->buckets;
this->maxcomplength = 0;
// 2) Obtaining the char frequencies and the corresponding Hu-Tucker and Huffman
// trees for the headers and the internal strings respectively
uint *freqsHT = new uint[256];
uint *freqsHU = new uint[256];
// Initializing counters
for (uint i=0; i<256; i++) { freqsHT[i]=1; freqsHU[i]=1; }
size_t pbeg = 0, pend = 0;
uint bucket = 1;
while (bucket <= dict->buckets)
{
// Recollecting statistics for the headers
pbeg = dict->blStrings->getField(bucket);
bucket++;
pend = dict->blStrings->getField(bucket);
for (; dict->textStrings[pbeg] != 0; pbeg++) freqsHT[(int)(dict->textStrings[pbeg])]++;
freqsHT[0]++; pbeg++;
// Recollecting statistics for the internal strings
for (; pbeg < pend; pbeg++) freqsHU[(int)(dict->textStrings[pbeg])]++;
}
// Obtaining the codes
HuTucker *ht = new HuTucker(freqsHT);
Huffman *hu = new Huffman(freqsHU);
// Initializing the HuTucker builder
DecodingTableBuilder *builderHT = new DecodingTableBuilder();
builderHT->initializeFromHuTucker(ht);
codewordsHT = builderHT->getCodewords();
delete [] freqsHT; delete ht;
// Initializing the Huffman builder
DecodingTableBuilder *builderHU = new DecodingTableBuilder();
builderHU->initializeFromHuffman(hu);
codewordsHU = builderHU->getCodewords();
delete [] freqsHU; delete hu;
// 3) Compressing the dictionary and building the decoding table
{
vector<size_t> xblStrings;
size_t ptr = 0; uint offset = 0, bytes = 0;
uchar *tmp = new uchar[4*maxlength];
size_t reservedStrings = MEMALLOC*bucketsize;
textStrings = new uchar[reservedStrings];
bytesStrings = 0; textStrings[bytesStrings] = 0;
xblStrings.push_back(bytesStrings);
// Auxiliar variables for managing the substrings indexed in
// the Decoding Table.
vector<uchar> textSubstr; vector<ushort> lenSubstr;
ushort ptrSubstr=0; uint codeSubstr=0;
coderHT = new StatCoder(codewordsHT);
coderHU = new StatCoder(codewordsHU);
for (uint current=1; current<=elements; current++)
{
// Checking the available space in textStrings and
// realloc if required
while ((bytesStrings+(2*maxlength)) > reservedStrings)
reservedStrings = Reallocate(&textStrings, reservedStrings);
if (((current-1)%bucketsize) == 0)
{
// NEW BLOCK: updating counters and storing the
// pointer to the current bucket
bytes = 0; tmp[bytes] = 0; offset = 0;
xblStrings.push_back(bytesStrings);
do
{
// Encoding the header in tmp
uchar symbol = (dict->textStrings[ptr]);
bytes += coderHT->encodeSymbol(symbol, &(tmp[bytes]), &offset);
ptr++;
builderHT->insertDecodeableSubstr(symbol, &codeSubstr, &ptrSubstr, &textSubstr, &lenSubstr);
if (bytes > maxcomplength) maxcomplength = bytes;
}
while (dict->textStrings[ptr-1] != '\0');
{
// Encoding the compressed header length
if (offset > 0) bytes++;
memcpy(textStrings+bytesStrings, tmp, bytes);
bytesStrings += bytes;
if (bytes > maxcomplength) maxcomplength = bytes;
}
// Adding an ending decodeable string (if required)
if (textSubstr.size() > 0)
{
// #######################
// It is necessary to read up to TABLEBITSO bits for indexing the
// substring in the DecodingTable
if (offset > 0)
{
// The substring is also padded
codeSubstr = (codeSubstr << (8-offset));
ptrSubstr += (8-offset); offset = 0;
}
if (ptrSubstr > TABLEBITSO)
{
codeSubstr = codeSubstr >> (ptrSubstr - TABLEBITSO);
ptrSubstr = TABLEBITSO;
}
else
{
if (current == elements)
{
// The last element is directly padded
codeSubstr = (codeSubstr << (TABLEBITSO-ptrSubstr));
ptrSubstr = TABLEBITSO;
break;
}
uint read = 0;
uint nextst = current+1;
while (TABLEBITSO > ptrSubstr)
{
{
// The VByte value is independently encoded to avoid confusions due to '\0' bytes
uint value = 0;
size_t xptr = VByte::decode(&value, &(dict->textStrings[ptr]));
for (uint i=0; i<xptr; i++)
{
uint symbol = dict->textStrings[ptr+read]; read++;
uint bits = codewordsHU[(int)symbol].bits;
uint codeword = codewordsHU[(int)symbol].codeword;
if ((bits+ptrSubstr) <= TABLEBITSO)
{
codeSubstr = (codeSubstr << bits) | codeword;
ptrSubstr += bits;
offset = (offset+bits) % 8;
}
else
{
uint remaining = TABLEBITSO-ptrSubstr;
codeSubstr = (codeSubstr << remaining) | (codeword >> (bits-remaining));
ptrSubstr = TABLEBITSO;
break;
}
}
}
while (TABLEBITSO > ptrSubstr)
{
uint symbol = dict->textStrings[ptr+read]; read++;
uint bits = codewordsHU[(int)symbol].bits;
uint codeword = codewordsHU[(int)symbol].codeword;
if ((bits+ptrSubstr) <= TABLEBITSO)
{
codeSubstr = (codeSubstr << bits) | codeword;
ptrSubstr += bits;
offset += bits;
if (offset > 8) offset -= 8;
// The next string has fully read!
if (symbol == 0)
{
if ((nextst % bucketsize) == 0)
{
if (((ptrSubstr+(8-offset)) > TABLEBITSO))
{
offset = offset % 8;
// The padding bits are enough...
codeSubstr = (codeSubstr << (TABLEBITSO-ptrSubstr));
ptrSubstr = TABLEBITSO;
}
else
{
while (true)
{
uint symbol = dict->textStrings[ptr+read]; read++;
uint bits = codewordsHT[(int)symbol].bits;
uint codeword = codewordsHT[(int)symbol].codeword;
if ((bits+ptrSubstr) <= TABLEBITSO)
{
codeSubstr = (codeSubstr << bits) | codeword;
ptrSubstr += bits;
offset += bits;
if (offset > 8) offset -= 8;
}
else
{
uint remaining = TABLEBITSO-ptrSubstr;
codeSubstr = (codeSubstr << remaining) | (codeword >> (bits-remaining));
ptrSubstr = TABLEBITSO;
break;
}
}
}
}
}
}
else
{
uint remaining = TABLEBITSO-ptrSubstr;
codeSubstr = (codeSubstr << remaining) | (codeword >> (bits-remaining));
ptrSubstr = TABLEBITSO;
break;
}
}
nextst++;
}
}
builderHT->insertEndingSubstr(&codeSubstr, &ptrSubstr, &textSubstr, &lenSubstr);
}
offset = 0; textStrings[bytesStrings] = 0;
// Clearing decodeable substrings
textSubstr.clear(); lenSubstr.clear();
ptrSubstr=0, codeSubstr=0;
}
else
{
// The VByte is independently encoded to avoid that any
// possible 0 to be confused with the end of a string
uint value = 0;
size_t xptr = VByte::decode(&value, &(dict->textStrings[ptr]));
for (uint i=0; i<xptr; i++)
{
uchar code = (dict->textStrings[ptr]);
bytesStrings += coderHU->encodeSymbol(code, &(textStrings[bytesStrings]), &offset);
ptr++;
builderHU->insertDecodeableSubstr(code, &codeSubstr, &ptrSubstr, &textSubstr, &lenSubstr);
}
do
{
// Encoding the string
uchar symbol = (dict->textStrings[ptr]);
bytesStrings += coderHU->encodeSymbol(symbol, &(textStrings[bytesStrings]), &offset);
ptr++;
builderHU->insertDecodeableSubstr(symbol, &codeSubstr, &ptrSubstr, &textSubstr, &lenSubstr);
}
while (dict->textStrings[ptr-1] != '\0');
if ((current%bucketsize) == 0)
{
// Ending the block
if (offset > 0) { offset=0; bytesStrings++; textStrings[bytesStrings] = 0; }
// Adding an ending decodeable string (if required)
if (textSubstr.size() > 0)
builderHU->insertEndingSubstr(&codeSubstr, &ptrSubstr, &textSubstr, &lenSubstr);
// Clearing decodeable substrings
textSubstr.clear(); lenSubstr.clear();
ptrSubstr=0, codeSubstr=0;
}
}
}
delete [] tmp;
bytesStrings++;
xblStrings.push_back(bytesStrings);
blStrings = new LogSequence(&xblStrings, bits(bytesStrings));
maxcomplength += 4; // The value is increased because advanced readings in decoding...
}
delete dict;
tableHT = builderHT->getTable(); delete builderHT;
tableHU = builderHU->getTable(); delete builderHU;
}
uint
StringDictionaryHHTFC::locate(uchar *str, uint strLen)
{
uint id = NORESULT;
// Encoding the string
uint encLen, offset;
uchar *encoded = coderHT->encodeString(str, strLen+1, &encLen, &offset);
// Locating the candidate bucket for the string
size_t idbucket;
bool cmp = locateBucket(encoded, encLen, &idbucket);
delete [] encoded;
// The string is the header of the bucket
if (cmp) id = ((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
ChunkScan c = decodeHeader(idbucket);
resetScan(&c, idbucket);
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
sharedPrev = coderHU->decodeString(&c);
cmp=longestCommonPrefix(c.str+sharedCurr, str+sharedCurr, c.strLen-sharedCurr, &sharedCurr);
if (cmp != 0)
{
for (uint i=2; i<scanneable; i++)
{
sharedPrev = coderHU->decodeString(&c);
if (sharedPrev < sharedCurr) break;
cmp=longestCommonPrefix(c.str+sharedCurr, str+sharedCurr, c.strLen-sharedCurr, &sharedCurr);
if (cmp==0)
{
id = ((idbucket-1)*bucketsize)+i+1;
break;
}
else if (cmp > 0) break;
}
}
else id = ((idbucket-1)*bucketsize)+2;
}
delete [] c.str;
}
}
return id;
}
uchar *
StringDictionaryHHTFC::extract(size_t id, uint *strLen)
{
if ((id > 0) && (id <= elements))
{
uint idbucket = 1+((id-1)/bucketsize);
uint pos = ((id-1)%bucketsize);
ChunkScan c = decodeHeader(idbucket);
if (pos > 0)
{
resetScan(&c, idbucket);
for (uint i=1; i<=pos; i++) coderHU->decodeString(&c);
}
*strLen = c.strLen-1;
return c.str;
}
else
{
*strLen = 0;
return NULL;
}
}
IteratorDictID*
StringDictionaryHHTFC::locatePrefix(uchar *str, uint strLen)
{
// Encoding the string
uint encLen, offset;
uchar *encoded = coderHT->encodeString(str, strLen, &encLen, &offset);
size_t leftBucket = 1, rightBucket = buckets;
size_t leftID = 0, rightID = 0;
// Locating the candidate buckets for the prefix
locateBoundaryBuckets(encoded, encLen, offset, &leftBucket, &rightBucket);
IteratorDictIDContiguous *itResult;
if (leftBucket > NORESULT)
{
ChunkScan c = decodeHeader(leftBucket);
resetScan(&c, leftBucket);
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(&c, scanneable, str, strLen);
// No strings use the required prefix
if (leftID == NORESULT) itResult = new IteratorDictIDContiguous(NORESULT, NORESULT);
else
{
rightID = leftID+searchDistinctPrefix(&c, scanneable-leftID+1, str, strLen)-1;
leftID += (leftBucket-1)*bucketsize;
rightID += (rightBucket-1)*bucketsize;
}
}
else
{
// All prefixes exceeds (possibly) a single bucket
{
// Searching the left limit
leftID = searchPrefix(&c, scanneable, 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;
delete [] c.str;
}
{
// Searching the right limit
c = decodeHeader(rightBucket);
resetScan(&c, rightBucket);
scanneable = bucketsize;
if ((rightBucket == buckets) && ((elements%bucketsize) != 0)) scanneable = (elements%bucketsize);
rightID = searchDistinctPrefix(&c, scanneable, str, strLen);
rightID += (rightBucket-1)*bucketsize;
}
}
itResult = new IteratorDictIDContiguous(leftID, rightID);
delete [] c.str;
}
else
{
// No strings use the required prefix
itResult = new IteratorDictIDContiguous(NORESULT, NORESULT);
}
delete [] encoded;
return itResult;
}
IteratorDictID*
StringDictionaryHHTFC::locateSubstr(uchar *str, uint strLen)
{
cerr << "This dictionary does not provide substring location" << endl;
return NULL;
}
uint
StringDictionaryHHTFC::locateRank(uint rank)
{
return rank;
}
IteratorDictString*
StringDictionaryHHTFC::extractPrefix(uchar *str, uint strLen)
{
IteratorDictIDContiguous *it = (IteratorDictIDContiguous*)locatePrefix(str, strLen);
size_t left = it->getLeftLimit();
if (left != NORESULT)
{
// Positioning the LEFT Limit
uint leftbucket = 1+((left-1)/bucketsize);
uint leftpos = ((left-1)%bucketsize);
// Positioning the RIGHT Limit
size_t right = it->getRightLimit();
delete it;
return new IteratorDictStringHHTFC(tableHT, tableHU, codewordsHT, textStrings, blStrings, leftbucket, leftpos, bucketsize, right-left+1, maxlength, maxcomplength);
}
else return NULL;
}
IteratorDictString*
StringDictionaryHHTFC::extractSubstr(uchar *str, uint strLen)
{
cerr << "This dictionary does not provide substring extraction" << endl;
return 0;
}
uchar *
StringDictionaryHHTFC::extractRank(uint rank, uint *strLen)
{
return extract(rank, strLen);
}
IteratorDictString*
StringDictionaryHHTFC::extractTable()
{
return new IteratorDictStringHHTFC(tableHT, tableHU, codewordsHT, textStrings, blStrings, 1, 0, bucketsize, elements, maxlength, maxcomplength);
}
size_t
StringDictionaryHHTFC::getSize()
{
return bytesStrings*sizeof(uchar)+blStrings->getSize()+256*2*sizeof(Codeword)+tableHT->getSize()+tableHU->getSize()+sizeof(StringDictionaryHHTFC);
}
void
StringDictionaryHHTFC::save(ofstream &out)
{
saveValue<uint32_t>(out, type);
saveValue<uint64_t>(out, elements);
saveValue<uint32_t>(out, maxlength);
saveValue<uint32_t>(out, maxcomplength);
saveValue<uint32_t>(out, buckets);
saveValue<uint32_t>(out, bucketsize);
saveValue<uint64_t>(out, bytesStrings);
saveValue<uchar>(out, textStrings, bytesStrings);
blStrings->save(out);
saveValue<Codeword>(out, codewordsHT, 256);
tableHT->save(out);
saveValue<Codeword>(out, codewordsHU, 256);
tableHU->save(out);
}
StringDictionary*
StringDictionaryHHTFC::load(ifstream &in)
{
size_t type = loadValue<uint32_t>(in);
if(type != HHTFC) return NULL;
StringDictionaryHHTFC *dict = new StringDictionaryHHTFC();
dict->type = HHTFC;
dict->elements = loadValue<uint64_t>(in);
dict->maxlength = loadValue<uint32_t>(in);
dict->maxcomplength = 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);
dict->codewordsHT = loadValue<Codeword>(in, 256);
dict->tableHT = DecodingTable::load(in);
dict->coderHT = new StatCoder(dict->tableHT, dict->codewordsHT);
dict->codewordsHU = loadValue<Codeword>(in, 256);
dict->tableHU = DecodingTable::load(in);
dict->coderHU = new StatCoder(dict->tableHU, dict->codewordsHU);
return dict;
}
uchar*
StringDictionaryHHTFC::getHeader(size_t idbucket)
{
size_t ptrH = blStrings->getField(idbucket);
uchar *header = textStrings+ptrH;
return header;
}
ChunkScan
StringDictionaryHHTFC::decodeHeader(size_t idbucket)
{
uchar* ptr = textStrings+blStrings->getField(idbucket);
ChunkScan chunk = {0, 0, ptr, maxcomplength, new uchar[4*maxlength+tableHT->getK()], 0, 0, 1};
// Variables used for adjusting purposes
uint plen = 0;
uint pvalid = 0;
uchar *pptr = chunk.b_ptr;
while (true)
{
if (tableHT->processChunk(&chunk)) break;
plen = chunk.strLen;
pvalid = chunk.c_valid;
pptr = chunk.b_ptr;
}
uint bits = 0;
for (uint i=1; i<=chunk.strLen-plen; i++)
{
uchar c = chunk.str[chunk.strLen-i];
bits += codewordsHT[c].bits;
}
chunk.c_valid = 8*(chunk.b_ptr-pptr) - bits + pvalid;
chunk.b_ptr = chunk.b_ptr - (chunk.c_valid/8);
return chunk;
}
bool
StringDictionaryHHTFC::locateBucket(uchar *str, uint strLen, size_t *idbucket)
{
size_t left = 1, right = buckets, center = 0;
int cmp = 0;
uchar *header;
while (left <= right)
{
center = (left+right)/2;
header = getHeader(center);
cmp = memcmp(header, str, strLen);
// 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;
else
{
// The string is the first one in the c-th bucket
*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
StringDictionaryHHTFC::locateBoundaryBuckets(uchar *str, uint strLen, uint offset, size_t *left, size_t *right)
{
size_t center = 0;
int cmp = 0;
uchar *header = new uchar[4*strLen];
uchar cmask = (uchar)(~(mask(8) >> offset));
while (*left <= *right)
{
center = (*left+*right)/2;
memcpy(header, getHeader(center), strLen);
if (offset != 0) header[strLen-1] = header[strLen-1] & cmask;
cmp = memcmp(header, 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; }
delete [] header;
return;
}
if (center > 1)
{
// Looking for the left boundary
uint ll = *left, lr = center-1, lc;
while (ll <= lr)
{
lc = (ll+lr)/2;
memcpy(header, getHeader(lc), strLen);
if (offset != 0) header[strLen-1] = header[strLen-1] & cmask;
cmp = memcmp(header, 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;
memcpy(header, getHeader(rc), strLen);
if (offset != 0) header[strLen-1] = header[strLen-1] & cmask;
cmp = memcmp(header, str, strLen);
if (cmp == 0) rl = rc;
else rr = rc;
}
*right = rl;
}
delete [] header;
}
uint
StringDictionaryHHTFC::searchPrefix(ChunkScan* c, uint scanneable, uchar *str, uint strLen)
{
uint id = NORESULT;
uint sharedCurr=0, sharedPrev=0;
int cmp=0;
uint i=1;
while (true)
{
cmp=longestCommonPrefix(c->str+sharedCurr, str+sharedCurr, c->strLen-sharedCurr-1, &sharedCurr);
if (sharedCurr==strLen) { id = i; break; }
else
{
if ((cmp > 0) || (i == scanneable)) break;
sharedPrev = coderHU->decodeString(c);
i++;
if (sharedPrev < sharedCurr) break;
}
}
return id;
}
uint
StringDictionaryHHTFC::searchDistinctPrefix(ChunkScan* c, uint scanneable, uchar *str, uint strLen)
{
uint id = 1;
for (id=1; id<scanneable; id++)
{
if (coderHU->decodeString(c) < strLen) break;
}
return id;
}
void
StringDictionaryHHTFC::resetScan(ChunkScan *c, size_t idbucket)
{
c->c_chunk = 0;
c->c_valid = 0;
c->b_remain = textStrings+blStrings->getField(idbucket+1)-c->b_ptr;
c->advanced = 0;
}
StringDictionaryHHTFC::~StringDictionaryHHTFC()
{
delete [] textStrings; delete blStrings;
delete coderHT; delete coderHU;
delete [] codewordsHT; delete [] codewordsHU;
delete tableHT; delete tableHU;
}