-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathStringDictionaryHASHRPDAC.cpp
326 lines (262 loc) · 7.05 KB
/
StringDictionaryHASHRPDAC.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
/* StringDictionaryHASHRPDAC.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 "StringDictionaryHASHRPDAC.h"
StringDictionaryHASHRPDAC::StringDictionaryHASHRPDAC()
{
this->type = HASHRPDAC;
this->elements = 0;
this->maxlength = 0;
}
StringDictionaryHASHRPDAC::StringDictionaryHASHRPDAC(IteratorDictString *it, uint len, int overhead)
{
this->type = HASHRPDAC;
this->elements = 0;
this->maxlength = 0;
uchar maxchar = 0;
{
// Counting the elements in Tdict for building the hash table structure
uchar *strCurrent;
uint lenCurrent=0;
while (it->hasNext())
{
strCurrent = it->next(&lenCurrent);
if (lenCurrent >= maxlength) maxlength = lenCurrent+1;
elements++;
for (uint i=0; i<lenCurrent; i++)
{
if (strCurrent[i] > maxchar) maxchar = strCurrent[i];
}
}
maxchar++;
((IteratorDictStringPlain*)it)->restart();
}
{
// Initializing the hash table
uint hash_size = (uint)(elements*(1+(overhead*1.0/100.0)));
hash = new HashDAC(hash_size);
}
// Performing Tdict reorganization
vector<SortString> sorting(elements);
uchar *strCurrent;
uint lenCurrent=0;
uint processed=0;
for (uint current=0; current<elements; current++)
{
strCurrent= it->next(&lenCurrent);
// Storing the string position in Tdict
sorting[current].original = processed;
// Simulating the string insertion in the hash table
sorting[current].hash = hash->insert(strCurrent, lenCurrent);
processed += lenCurrent+1;
}
// String sorting for Tdict*
std::sort(sorting.begin(), sorting.end(), sortTdict);
// Obtaining Tdict*
uchar *text = ((IteratorDictStringPlain*)it)->getPlainText();
int *dict = new int[it->size()+elements];
processed=0;
for (uint current=0; current<elements; current++)
{
uint ptr = sorting[current].original;
do
{
dict[processed] = text[ptr];
processed++; ptr++;
}
while (text[ptr] != 0);
dict[processed] = 0;
processed++;
}
delete it;
rp = new RePair(dict, processed, maxchar);
{
// Compacting the sequence (a -i value is inserted after the i-th string).
int *cdict = new int[processed];
uint io = 0, ic = 0, strings = 0;
uint maxseq = 0, currentseq = 0;
hash->setOffset(sorting[0].hash, 0);
while (io<processed)
{
if (dict[io] >= 0)
{
if (dict[io] == 0)
{
if (currentseq > maxseq) maxseq = currentseq;
hash->setOffset(sorting[strings].hash, ic);
strings++;
cdict[ic] = -strings;
io++; ic++;
currentseq = 0;
}
else
{
cdict[ic] = dict[io];
io++; ic++;
currentseq++;
}
}
else
{
if (io < processed) io = -(dict[io]+1);
}
}
delete [] dict;
// Post-processing the hash
hash->finish(ic);
// Building the array for the sequence
rp->Cdac = new DAC_VLS(cdict, ic-2, bits(rp->rules+rp->terminals), maxseq);
delete [] cdict;
}
}
uint
StringDictionaryHASHRPDAC::locate(uchar *str, uint strLen)
{
uint id = NORESULT;
size_t hval = bitwisehash(str, strLen, hash->tsize);
size_t next;
if(!hash->b_ht->access(hval))
return id;
uint pos = hash->b_ht->rank1(hval);
if (rp->extractStringAndCompareDAC(pos, str, strLen) == 0)
return pos;
// using double hashing
size_t h2 = step_value(str, strLen, hash->tsize);
for(uint i=1; i<hash->tsize ; i++ )
{
next = (hval + i*h2)%hash->tsize;
if(!hash->b_ht->access(next))
return id;
pos = hash->b_ht->rank1(next);
if(rp->extractStringAndCompareDAC(pos, str, strLen) == 0)
return hash->b_ht->rank1(next);
}
return id;
}
uchar *
StringDictionaryHASHRPDAC::extract(size_t id, uint *strLen)
{
if ((id > 0) && (id <= elements))
{
uint *rules;
uint len = rp->Cdac->access(id, &rules);
uchar *s = new uchar[maxlength+1];
*strLen = 0;
for (uint i=0; i<len; i++)
{
if (rules[i] >= rp->terminals) (*strLen) += rp->expandRule(rules[i]-rp->terminals, (s+(*strLen)));
else
{
s[*strLen] = (uchar)rules[i];
(*strLen)++;
}
}
s[*strLen] = (uchar)'\0';
delete [] rules;
return s;
}
else
{
*strLen = 0;
return NULL;
}
}
IteratorDictID*
StringDictionaryHASHRPDAC::locatePrefix(uchar *str, uint strLen)
{
cout << "This dictionary does not provide prefix location" << endl;
return NULL;
}
IteratorDictID*
StringDictionaryHASHRPDAC::locateSubstr(uchar *str, uint strLen)
{
cout << "This dictionary does not provide substring location" << endl;
return NULL;
}
uint
StringDictionaryHASHRPDAC::locateRank(uint rank)
{
cout << "This dictionary does not provide rank location" << endl;
return 0;
}
IteratorDictString*
StringDictionaryHASHRPDAC::extractPrefix(uchar *str, uint strLen)
{
cout << "This dictionary does not provide prefix extraction" << endl;
return NULL;
}
IteratorDictString*
StringDictionaryHASHRPDAC::extractSubstr(uchar *str, uint strLen)
{
cout << "This dictionary does not provide substring extraction" << endl;
return NULL;
}
uchar *
StringDictionaryHASHRPDAC::extractRank(uint rank, uint *strLen)
{
cout << "This dictionary does not provide rank extraction" << endl;
return NULL;
}
IteratorDictString*
StringDictionaryHASHRPDAC::extractTable()
{
vector<uchar*> tabledec(elements);
uint strLen;
for (uint i=1; i<=elements; i++) tabledec[i-1] = extract(i, &strLen);
return new IteratorDictStringVector(&tabledec, elements);
}
size_t
StringDictionaryHASHRPDAC::getSize()
{
return hash->getSize()+rp->getSize()+sizeof(StringDictionaryHASHRPDAC);
}
void
StringDictionaryHASHRPDAC::save(ofstream &out)
{
saveValue<uint32_t>(out, type);
saveValue<uint64_t>(out, elements);
saveValue<uint32_t>(out, maxlength);
rp->save(out, type);
hash->save(out);
}
StringDictionary*
StringDictionaryHASHRPDAC::load(ifstream &in, uint technique)
{
size_t type = loadValue<uint32_t>(in);
if (type != HASHRPDAC) return NULL;
StringDictionaryHASHRPDAC *dict = new StringDictionaryHASHRPDAC();
dict->type = technique;
dict->elements = loadValue<uint64_t>(in);
dict->maxlength = loadValue<uint32_t>(in);
dict->rp = RePair::load(in);
dict->hash = HashDAC::load(in);
return dict;
}
StringDictionaryHASHRPDAC::~StringDictionaryHASHRPDAC()
{
delete rp;
delete hash;
}