forked from maxrossi91/ShapedSlp
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPlainSlp.hpp
More file actions
370 lines (305 loc) · 9.47 KB
/
PlainSlp.hpp
File metadata and controls
370 lines (305 loc) · 9.47 KB
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
#ifndef INCLUDE_GUARD_PlainSlp
#define INCLUDE_GUARD_PlainSlp
#include <stdint.h> // include uint64_t etc.
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <stack>
#include "Common.hpp"
#include "NaiveSlp.hpp"
#include "fbtree.h"
template
<
typename tparam_var_t,
class VarVecT,
class LenVecT
>
class PlainSlp
{
public:
//// Public constant, alias etc.
using var_t = tparam_var_t;
using nodeT = std::tuple<uint64_t, var_t, var_t>; // (expansion_length, node_id, child_rank): stack of nodes indicates a path
private:
size_t len_;
size_t numRules_;
var_t startVar_;
std::vector<char> alph_;
VarVecT left_;
VarVecT right_;
LenVecT expLen_; // expansion lengths
public:
PlainSlp () : len_(0),
numRules_(0),
startVar_(0)
{}
~PlainSlp()
{}
size_t getLen() const {
return len_;
}
size_t getLenSeq() const {
return 1;
}
size_t getNumRules() const {
return numRules_;
}
size_t getAlphSize() const {
return alph_.size();
}
char getChar(uint64_t i) const {
return alph_[i];
}
var_t getLeft
(
const uint64_t ruleId
) const {
return left_[ruleId];
}
var_t getRight
(
const uint64_t ruleId
) const {
return right_[ruleId];
}
var_t getSeq(uint64_t i) const {
return startVar_;
}
size_t getNumRulesOfSlp() const {
return numRules_;
}
char charAt
(
const uint64_t pos //!< 0-based position
) const {
return charAt(pos, startVar_);
}
char charAt
(
const uint64_t pos, //!< relative position in a variable
const uint64_t varId //!< varId
) const {
// std::cout << "pos = " << pos << ", len = " << len << ", stgOffset = " << stgOffset << ", slpOffset = " << slpOffset << std::endl;
if (varId < getAlphSize()) {
return getChar(varId);
}
const uint64_t leftVarId = getLeft(varId - getAlphSize());
const uint64_t leftLen = (leftVarId < getAlphSize()) ? 1 : expLen_[leftVarId - getAlphSize()];
if (pos < leftLen) {
return charAt(pos, leftVarId);
} else {
return charAt(pos - leftLen, getRight(varId - getAlphSize()));
}
}
void expandSubstr
(
const uint64_t pos, //!< beginning position
const uint64_t lenExpand, //!< length to expand
char * str //!< [out] must have length at least 'len'
) const {
assert(pos < getLen());
assert(lenExpand > 0);
assert(lenExpand <= getLen() - pos);
expandSubstr(pos, lenExpand, str, startVar_);
}
void expandSubstr
(
const uint64_t pos, //!< relative position in a variable
const uint64_t lenExpand, //!< length to expand
char * str, //!< [out] must have length at least 'len'
const uint64_t varId //!< varId
) const {
// std::cout << "pos = " << pos << ", len = " << len << ", varLen = " << varLen << ", stgOffset = " << stgOffset << ", slpOffset = " << slpOffset << std::endl;
assert(lenExpand > 0);
if (varId < getAlphSize()) {
*str = getChar(varId);
return;
}
const uint64_t leftVarId = getLeft(varId - getAlphSize());
const uint64_t leftLen = (leftVarId < getAlphSize()) ? 1 : expLen_[leftVarId - getAlphSize()];
if (pos < leftLen) {
expandSubstr(pos, lenExpand, str, leftVarId);
if (leftLen - pos < lenExpand) {
expandPref(lenExpand - (leftLen - pos), str + (leftLen - pos), getRight(varId - getAlphSize()));
}
} else {
expandSubstr(pos - leftLen, lenExpand, str, getRight(varId - getAlphSize()));
}
}
void expandPref
(
const uint64_t lenExpand, //!< length to expand
char * str, //!< [out] must have length at least 'len'
const uint64_t varId //!< varId
) const {
// std::cout << "len = " << len << ", varLen = " << varLen << ", stgOffset = " << stgOffset << ", slpOffset = " << slpOffset << std::endl;
assert(lenExpand > 0);
if (varId < getAlphSize()) {
*str = getChar(varId);
return;
}
const uint64_t leftVarId = getLeft(varId - getAlphSize());
const uint64_t leftLen = (leftVarId < getAlphSize()) ? 1 : expLen_[leftVarId - getAlphSize()];
expandPref(lenExpand, str, leftVarId);
if (lenExpand > leftLen) {
expandPref(lenExpand - leftLen, str + leftLen, getRight(varId - getAlphSize()));
}
}
nodeT getRootNode() const {
return std::forward_as_tuple(getLen(), 0, 0);
}
nodeT getChildNode_Root
(
const uint64_t idx
) const {
return std::forward_as_tuple(getLen(), startVar_, idx);
}
nodeT getChildNode
(
const nodeT & node,
const uint64_t idx
) const {
assert(std::get<0>(node) > 1); // len > 1
const uint64_t len = std::get<0>(node);
const uint64_t varId = std::get<1>(node);
const uint64_t newVarId = (idx == 0) ? getLeft(varId - getAlphSize()) : getRight(varId - getAlphSize());
const uint64_t newLen = (newVarId < getAlphSize()) ? 1 : expLen_[newVarId - getAlphSize()];
return std::forward_as_tuple(newLen, newVarId, idx);
}
nodeT getChildNodeForPos_Root
(
uint64_t & pos //! [in, out]
) const {
return std::forward_as_tuple(getLen(), startVar_, 0);
}
nodeT getChildNodeForPos
(
const nodeT & node,
uint64_t & pos //! [in, out]
) const {
assert(std::get<0>(node) > 1); // len > 1
const uint64_t len = std::get<0>(node);
const uint64_t varId = std::get<1>(node);
const uint64_t leftVarId = getLeft(varId - getAlphSize());
const uint64_t leftLen = (leftVarId < getAlphSize()) ? 1 : expLen_[leftVarId - getAlphSize()];
const uint64_t idx = (pos < leftLen) ? 0 : 1;
const uint64_t newVarId = (idx == 0) ? leftVarId : getRight(varId - getAlphSize());
const uint64_t newLen = (idx == 0) ? leftLen : len - leftLen;
pos -= leftLen * idx;
return std::forward_as_tuple(newLen, newVarId, idx);
}
void printStatus
(
const bool verbose = false
) const {
std::cout << "PlainSlp object (" << this << ") " << __func__ << "(" << verbose << ") BEGIN" << std::endl;
std::cout << "alphSize = " << getAlphSize() << ", len = " << getLen() << ", lenSeq = " << getLenSeq()
<< ", numRulesOfSlp = " << getNumRulesOfSlp()
<< std::endl;
const size_t bytesAlph = sizeof(std::vector<char>) + (sizeof(char) * alph_.size());
const size_t bytesLeft = left_.calcMemBytes();
const size_t bytesRight = right_.calcMemBytes();
const size_t bytesExpLen = expLen_.calcMemBytes();
std::cout << "DS sizes (bytes)" << std::endl;
std::cout << bytesAlph + bytesLeft + bytesRight + bytesExpLen << std::endl;
std::cout << "| alph = " << bytesAlph << std::endl;
std::cout << "| left = " << bytesLeft << std::endl;
std::cout << "| right = " << bytesRight << std::endl;
std::cout << "| expLen = " << bytesExpLen << std::endl;
if (verbose) {
std::cout << "rules: " << std::endl;
for (uint64_t i = 0; i < getNumRulesOfSlp(); ++i) {
const auto l = getLeft(i);
const auto r = getRight(i);
std::cout << i + getAlphSize() << "(";
if (l < getAlphSize()) {
std::cout << "'" << getChar(l) << "'";
} else {
std::cout << l;
}
std::cout << ", ";
if (r < getAlphSize()) {
std::cout << "'" << getChar(r) << "'";
} else {
std::cout << r;
}
std::cout << ") ";
}
std::cout << std::endl;
std::cout << "expLen: " << std::endl;
for (uint64_t i = 0; i < getNumRulesOfSlp(); ++i) {
std::cout << expLen_[i] << " ";
}
std::cout << std::endl;
}
std::cout << "PlainSlp object (" << this << ") " << __func__ << "(" << verbose << ") END" << std::endl;
}
size_t calcMemBytes() const {
size_t ret = 0;
ret += sizeof(std::vector<char>) + (sizeof(char) * alph_.size());
ret += left_.calcMemBytes();
ret += right_.calcMemBytes();
ret += expLen_.calcMemBytes();
return ret;
}
void load
(
std::istream & in
) {
in.read((char*) & len_, sizeof(len_));
in.read((char*) & numRules_, sizeof(numRules_));
in.read((char*) & startVar_, sizeof(startVar_));
uint64_t alphSize = 0;
in.read((char*) & alphSize, sizeof(alphSize));
alph_.resize(alphSize);
in.read((char*) alph_.data(), alphSize * sizeof(alph_[0]));
left_.load(in);
right_.load(in);
expLen_.load(in);
}
void serialize
(
std::ostream & out
) const {
out.write((char*) & len_, sizeof(len_));
out.write((char*) & numRules_, sizeof(numRules_));
out.write((char*) & startVar_, sizeof(startVar_));
uint64_t alphSize = getAlphSize();
out.write((char*) & alphSize, sizeof(alphSize));
out.write((char*) alph_.data(), alphSize * sizeof(alph_[0]));
left_.serialize(out);
right_.serialize(out);
expLen_.serialize(out);
}
void init
(
const NaiveSlp<var_t> & slp
) {
assert(slp.getLenSeq() == 1);
const uint64_t alphSize = slp.getAlphSize();
alph_.resize(alphSize);
for (uint64_t i = 0; i < alphSize; ++i) {
alph_[i] = slp.getChar(i);
}
numRules_ = slp.getNumRules();
startVar_ = numRules_ - 1 + getAlphSize();
{
std::vector<uint64_t> left(slp.getNumRules());
std::vector<uint64_t> right(slp.getNumRules());
for (uint64_t i = 0; i < slp.getNumRules(); ++i) {
left[i] = slp.getLeft(i);
right[i] = slp.getRight(i);
}
left_.init(left);
right_.init(right);
}
{
std::vector<uint64_t> expLen(slp.getNumRules());
slp.makeLenVec(expLen);
len_ = expLen[slp.getNumRules() - 1];
expLen_.init(expLen);
}
}
};
#endif