-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathStringDictionaryHASHHF.cpp
419 lines (342 loc) · 10.5 KB
/
StringDictionaryHASHHF.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
/* StringDictionaryHASHHF.cpp
* Copyright (C) 2014, Francisco Claude & Rodrigo Canovas & Miguel A. Martinez-Prieto
* all rights reserved.
*
* This class implements a Compressed String Dictionary based on double hashing.
*
* 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 "StringDictionaryHASHHF.h"
StringDictionaryHASHHF::StringDictionaryHASHHF()
{
this->type = HASHHF;
this->elements = 0;
this->maxlength = 0;
this->maxcomplength = 0;
}
StringDictionaryHASHHF::StringDictionaryHASHHF(IteratorDictString *it, uint len, int overhead)
{
this->type = HASHHF;
this->elements = 0;
this->maxlength = 0;
this->maxcomplength = 0;
{
// Counting the elements in Tdict for building the hash table structure
uint lenCurrent=0;
while (it->hasNext())
{
it->next(&lenCurrent);
if (lenCurrent >= maxlength) maxlength = lenCurrent+1;
elements++;
}
}
// Obtaining the Huffman code
uchar *text = ((IteratorDictStringPlain*)it)->getPlainText();
Huffman *huff = new Huffman(text, len);
// Initializing the hash table
uint hash_size = (uint)(elements*(1+(overhead*1.0/100.0)));
hash = new Hashdh(hash_size);
// Initializing the builder for the decoding table and the coder for
// Huffman compression
DecodingTableBuilder *builder = new DecodingTableBuilder();
builder->initializeFromHuffman(huff);
delete huff;
codewords = builder->getCodewords();
coder = new StatCoder(codewords);
// Auxiliar variables
size_t ptr = 0; uint offset = 0, bytes = 0;
uchar *tmp = new uchar[6*maxlength];
vector<uchar> textSubstr; vector<ushort> lenSubstr;
ushort ptrSubstr=0; uint codeSubstr=0;
// Simulating the hash representation
vector<SortString> sorting(elements);
for (uint current=1; current<=elements; current++)
{
// Resetting variables for the next string
bytes = 0; tmp[bytes] = 0; offset = 0;
// Storing the sting position in Tdict
sorting[current-1].original = ptr;
// Encoding the string
do
{
uchar symbol = (text[ptr]);
bytes += coder->encodeSymbol(symbol, &(tmp[bytes]), &offset);
ptr++;
}
while (text[ptr-1] != '\0');
{
// Padding the last byte (if neccesary)
if (offset > 0) bytes++;
// Simulating the string insertion in the hash table
sorting[current-1].hash = hash->insert(tmp, bytes);
if (bytes > maxcomplength) maxcomplength = bytes;
}
}
// Sorting Tdict into Tdict*
std::sort(sorting.begin(), sorting.end(), sortTdict);
// Building the Hash representation
size_t reservedStrings = MEMALLOC;
textStrings = new uchar[reservedStrings];
bytesStrings = 0; textStrings[bytesStrings] = 0;
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);
// Resetting variables for the next string
{
bytes = 0; tmp[bytes] = 0; offset = 0;
ptrSubstr=0, codeSubstr=0;
textSubstr.clear(); lenSubstr.clear();
ptr = sorting[current-1].original;
}
// Encoding the string
do
{
uchar symbol = (text[ptr]);
bytes += coder->encodeSymbol(symbol, &(tmp[bytes]), &offset);
ptr++;
builder->insertDecodeableSubstr(symbol, &codeSubstr, &ptrSubstr, &textSubstr, &lenSubstr);
}
while (text[ptr-1] != '\0');
{
// Padding the last byte (if neccesary)
if (offset > 0) bytes++;
// Inserting the string in the hash table
hash->setOffset(sorting[current-1].hash, bytesStrings);
// Copying the encoded string into the compressed sequence
memcpy(textStrings+bytesStrings, tmp, bytes);
bytesStrings += bytes;
}
// Adding an ending decodeable string
if (textSubstr.size() > 0)
{
// #######################
// It is necessary to read up to TABLEBITO 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;
ptr = sorting[current].original;
while (TABLEBITSO > ptrSubstr)
{
uint symbol = text[ptr+read]; read++;
uint bits = codewords[(int)symbol].bits;
uint codeword = codewords[(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 (((ptrSubstr+(8-offset)) <= TABLEBITSO))
{
// The next string must be parsed...
codeSubstr = (codeSubstr << (8-offset));
ptrSubstr += (8-offset); offset = 0;
ptr = sorting[current+1].original;
read = 0;
}
else
{
offset = offset % 8;
// The padding bits are enough...
codeSubstr = (codeSubstr << (TABLEBITSO-ptrSubstr));
ptrSubstr = TABLEBITSO;
}
}
}
else
{
uint remaining = TABLEBITSO-ptrSubstr;
codeSubstr = (codeSubstr << remaining) | (codeword >> (bits-remaining));
ptrSubstr = TABLEBITSO;
}
}
}
builder->insertEndingSubstr(&codeSubstr, &ptrSubstr, &textSubstr, &lenSubstr);
}
}
delete it;
delete [] tmp;
textStrings[bytesStrings] = 0; bytesStrings++;
textStrings[bytesStrings] = 0; bytesStrings++;
if (textSubstr.size() > 0)
builder->insertEndingSubstr(&codeSubstr, &ptrSubstr, &textSubstr, &lenSubstr);
bytesStrings++;
table = builder->getTable();
hash->finish(bytesStrings);
delete builder;
}
uint
StringDictionaryHASHHF::locate(uchar *str, uint strLen)
{
uint id = NORESULT;
// Encoding the string
uint encLen, offset;
uchar *encoded = coder->encodeString(str, strLen+1, &encLen, &offset);
id = hash->search(encoded, encLen)+1;
delete [] encoded;
return id;
}
uchar *
StringDictionaryHASHHF::extract(size_t id, uint *strLen)
{
if ((id > 0) && (id <= elements))
{
uchar *tmp = new uchar[4*maxlength+table->getK()];
uint remain = maxcomplength+4;
uint pos = hash->getValue(id);
ChunkScan chunk = {0, 0, textStrings+pos, remain, tmp, 0, 0, 1};
while (!(table->processChunk(&chunk)));
tmp[chunk.strLen] = '\0';
*strLen = chunk.strLen-1;
return tmp;
}
else
{
*strLen = 0;
return NULL;
}
}
IteratorDictID*
StringDictionaryHASHHF::locatePrefix(uchar *str, uint strLen)
{
cout << "This dictionary does not provide prefix location" << endl;
return NULL;
}
IteratorDictID*
StringDictionaryHASHHF::locateSubstr(uchar *str, uint strLen)
{
cout << "This dictionary does not provide substring location" << endl;
return NULL;
}
uint
StringDictionaryHASHHF::locateRank(uint rank)
{
cout << "This dictionary does not provide rank location" << endl;
return 0;
}
IteratorDictString*
StringDictionaryHASHHF::extractPrefix(uchar *str, uint strLen)
{
cout << "This dictionary does not provide prefix extraction" << endl;
return NULL;
}
IteratorDictString*
StringDictionaryHASHHF::extractSubstr(uchar *str, uint strLen)
{
cout << "This dictionary does not provide substring extraction" << endl;
return NULL;
}
uchar *
StringDictionaryHASHHF::extractRank(uint rank, uint *strLen)
{
cout << "This dictionary does not provide rank extraction" << endl;
return NULL;
}
IteratorDictString*
StringDictionaryHASHHF::extractTable()
{
vector<uchar*> tabledec(elements);
uchar *tmp = new uchar[4*maxlength+table->getK()];
for (uint i=1; i<=elements; i++)
{
uint remain = maxlength;
uint pos = hash->getValue(i);
ChunkScan chunk = {0, 0, textStrings+pos, remain, tmp, 0, 0, 1};
while (!(table->processChunk(&chunk)));
tabledec[i-1] = new uchar[chunk.strLen+1];
strncpy((char*)tabledec[i-1], (char*)chunk.str, chunk.strLen);
tabledec[i-1][chunk.strLen] = '\0';
}
delete [] tmp;
return new IteratorDictStringVector(&tabledec, elements);
}
size_t
StringDictionaryHASHHF::getSize()
{
return bytesStrings*sizeof(uchar)+hash->getSize()+256*sizeof(Codeword)+table->getSize()+sizeof(StringDictionaryHASHHF)+256*sizeof(bool);
}
void
StringDictionaryHASHHF::save(ofstream &out)
{
saveValue<uint32_t>(out, type);
saveValue<uint64_t>(out, elements);
saveValue<uint32_t>(out, maxlength);
saveValue<uint32_t>(out, maxcomplength);
// Information for Hash and Huffman encoding
hash->save(out);
saveValue<uint64_t>(out, bytesStrings);
saveValue<uchar>(out, textStrings, bytesStrings);
// Decoding Table
saveValue<Codeword>(out, codewords, 256);
table->save(out);
}
StringDictionary*
StringDictionaryHASHHF::load(ifstream &in, uint technique)
{
size_t type = loadValue<uint32_t>(in);
if (type != HASHHF) return NULL;
if ((technique != HASHUFF) && (technique != HASHBHUFF) && (technique != HASHBBHUFF)) return NULL;
StringDictionaryHASHHF *dict = new StringDictionaryHASHHF();
dict->type = technique;
dict->elements = loadValue<uint64_t>(in);
dict->maxlength = loadValue<uint32_t>(in);
dict->maxcomplength = loadValue<uint32_t>(in);
dict->hash = Hash::load(in, dict->type);
dict->bytesStrings = loadValue<uint64_t>(in);
dict->textStrings = loadValue<uchar>(in, dict->bytesStrings);
dict->hash->setData(dict->textStrings);
dict->codewords = loadValue<Codeword>(in, 256);
dict->table = DecodingTable::load(in);
dict->coder = new StatCoder(dict->table, dict->codewords);
return dict;
}
StringDictionaryHASHHF::~StringDictionaryHASHHF()
{
delete hash;
delete [] textStrings;
delete [] codewords;
delete table; delete coder;
}