-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathStringDictionaryRPDAC.cpp
308 lines (251 loc) · 6.96 KB
/
StringDictionaryRPDAC.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
/* StringDictionaryRPDAC.cpp
* Copyright (C) 2014, Francisco Claude & Rodrigo Canovas & Miguel A. Martinez-Prieto
* all rights reserved.
*
* This class implements a Compressed String Dictionary combining Re-Pair
* compression and Directly Addresable Codes.
*
* 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 "StringDictionaryRPDAC.h"
StringDictionaryRPDAC::StringDictionaryRPDAC()
{
this->type = RPDAC;
this->elements = 0;
this->maxlength = 0;
}
StringDictionaryRPDAC::StringDictionaryRPDAC(IteratorDictString *it)
{
this->type = RPDAC;
this->elements = 0;
this->maxlength = 0;
uchar *strCurrent=NULL;
uint lenCurrent=0;
size_t processed = 0;
int *dict = new int[it->size()];
while (it->hasNext())
{
strCurrent = it->next(&lenCurrent);
if (lenCurrent >= maxlength) maxlength = lenCurrent+1;
for (uint i=0; i<=lenCurrent; i++) dict[processed+i] = strCurrent[i];
processed += lenCurrent+1;
elements++;
}
delete it;
rp = new RePair(dict, processed, 0);
// 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;
cerr << "### Writing seq [BEGIN]" << endl; cerr.flush();
while (io<processed)
{
if (dict[io] >= 0)
{
if (dict[io] == 0)
{
if (currentseq > maxseq) maxseq = currentseq;
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;
// Building the array for the sequence
rp->Cdac = new DAC_VLS(cdict, ic-2, bits(rp->rules+rp->terminals), maxseq);
delete [] cdict;
}
uint
StringDictionaryRPDAC::locate(uchar *str, uint strLen)
{
// Binary search comparing s with respect to the corresponding
// pivot rule.
size_t left = 1, right = elements, center = 0;
int cmp;
while (left <= right)
{
center = (left+right)/2;
cmp = rp->extractStringAndCompareDAC(center, str, strLen);
// The string precedes the current pivot
if (cmp > 0) right = center-1;
// The string follows the current pivot
else if (cmp < 0) left = center+1;
// The string is the current pivot
else return center;
}
return 0;
}
uchar *
StringDictionaryRPDAC::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*
StringDictionaryRPDAC::locatePrefix(uchar *str, uint strLen)
{
// Binary search comparing s with respect to the corresponding
// pivot rule.
size_t left = 1, right = elements, center = 0;
int cmp = 0;
while (left <= right)
{
center = (left+right)/2;
cmp = rp->extractPrefixAndCompareDAC(center, str, strLen);
// The string precedes the current pivot
if (cmp > 0) right = center-1;
// The string follows the current pivot
else if (cmp < 0) left = center+1;
// The string is the current pivot
else break;
}
// No strings use the required prefix
if (cmp != 0) return new IteratorDictIDContiguous(NORESULT, NORESULT);
if (center > 1)
{
// Looking for the left boundary
uint ll = left, lr = center-1, lc;
while (ll <= lr)
{
lc = (ll+lr)/2;
cmp = rp->extractPrefixAndCompareDAC(lc, str, strLen);
if (cmp == 0) lr = lc-1;
else ll = lc+1;
}
if (lr > NORESULT) left = lr+1;
else left = 1;
}
else left = center;
if (center < elements)
{
// Looking for the right boundary
uint rl = center, rr = right+1, rc;
while (rl < (rr-1))
{
rc = (rl+rr)/2;
cmp = rp->extractPrefixAndCompareDAC(rc, str, strLen);
if (cmp == 0) rl = rc;
else rr = rc;
}
right = rl;
}
else right = center;
return new IteratorDictIDContiguous(left, right);
}
IteratorDictID*
StringDictionaryRPDAC::locateSubstr(uchar *str, uint strLen)
{
cout << "This dictionary does not provide substring location" << endl;
return NULL;
}
uint
StringDictionaryRPDAC::locateRank(uint rank)
{
return rank;
}
IteratorDictString*
StringDictionaryRPDAC::extractPrefix(uchar *str, uint strLen)
{
IteratorDictIDContiguous *it = (IteratorDictIDContiguous*)locatePrefix(str, strLen);
size_t offset = it->getLeftLimit()-1;
size_t scanneable = it->getRightLimit();
delete it;
return new IteratorDictStringRPDAC(rp->G, rp->terminals, rp->Cdac, offset, scanneable, maxlength);
}
IteratorDictString*
StringDictionaryRPDAC::extractSubstr(uchar *str, uint strLen)
{
cout << "This dictionary does not provide substring extraction" << endl;
return 0;
}
uchar *
StringDictionaryRPDAC::extractRank(uint rank, uint *strLen)
{
return extract(rank, strLen);
}
IteratorDictString*
StringDictionaryRPDAC::extractTable()
{
return new IteratorDictStringRPDAC(rp->G, rp->terminals, rp->Cdac, 0, elements, maxlength);
}
size_t
StringDictionaryRPDAC::getSize()
{
return rp->getSize()+sizeof(StringDictionaryRPDAC);
}
void
StringDictionaryRPDAC::save(ofstream &out)
{
saveValue<uint32_t>(out, type);
saveValue<uint64_t>(out, elements);
saveValue<uint32_t>(out, maxlength);
rp->save(out, RPDAC);
}
StringDictionary*
StringDictionaryRPDAC::load(ifstream &in)
{
size_t type = loadValue<uint32_t>(in);
if(type != RPDAC) return NULL;
StringDictionaryRPDAC *dict = new StringDictionaryRPDAC();
dict->type = RPDAC;
dict->elements = loadValue<uint64_t>(in);
dict->maxlength = loadValue<uint32_t>(in);
dict->rp = RePair::load(in);
return dict;
}
StringDictionaryRPDAC::~StringDictionaryRPDAC()
{
delete rp;
}