This repository was archived by the owner on Oct 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxbw.cpp
More file actions
195 lines (171 loc) · 5.01 KB
/
Copy pathxbw.cpp
File metadata and controls
195 lines (171 loc) · 5.01 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
#include "xbw.h"
XBW::XBW(const string & filename, const string & params) {
string filename2 = filename+".xbw";
ifstream input(filename2.c_str(),ios::binary);
assert(input.good());
// Read the number of nodes
input.read((char*)&nodesCount,sizeof(uint));
//cout << "nodesCount=" << nodesCount << endl;
// Read the alpha sequence
uint * alphaInt = new uint[nodesCount];
input.read((char*)alphaInt,nodesCount*sizeof(uint));
// Read bitmap last
uint * lastInt = new uint[nodesCount/W+1];
input.read((char*)lastInt,(nodesCount/W+1)*sizeof(uint));
// Read bitmap leaf
//uint * leafInt = new uint[nodesCount/W+1];
//input.read((char*)leafInt,(nodesCount/W+1)*sizeof(uint));
// Initialize and compute the max label
maxLabel = 0;
for(uint i=0;i<nodesCount;i++) {
maxLabel = max(alphaInt[i],maxLabel);
}
uint * AInt = new uint[nodesCount/W+2];
input.read((char*)AInt,(nodesCount/W+2)*sizeof(uint));
// Create the data structures
alphabet_mapper * am = new alphabet_mapper_none();
//wt_coder * wcc = new wt_coder_huff(alphaInt, nodesCount, am);
static_bitsequence_builder * sbb = new static_bitsequence_builder_brw32(20);
static_bitsequence_builder * sbb2 = new static_bitsequence_builder_sdarray();
//static_sequence_builder * ssb = new static_sequence_builder_wvtree(wcc, sbb, am);
alpha = new static_sequence_bs(alphaInt, nodesCount, am, sbb2);
//alpha = ssb->build(alphaInt, nodesCount);
last = sbb->build(lastInt, nodesCount);
//leaf = sbb->build(leafInt, nodesCount);
A = sbb->build(AInt, nodesCount+1);
/*for(uint i=0;i<A->length();i++)
cout << A->access(i);
cout << endl;*/
//alphaTmp = alphaInt;
balpha = bits(maxLabel);
alphaSeq = new uint[uint_len(nodesCount,balpha)];
for(uint i=0;i<uint_len(nodesCount,balpha);i++)
alphaSeq[i] = 0;
for(uint i=0;i<nodesCount;i++)
set_field(alphaSeq,balpha,i,alphaInt[i]);
input.close();
delete sbb;
delete sbb2;
// Free the temporary arrays
delete [] alphaInt;
delete [] lastInt;
//delete [] leafInt;
delete [] AInt;
}
XBW::~XBW() {
delete alpha;
delete last;
//delete leaf;
delete A;
delete [] alphaSeq;
}
uint XBW::size() const {
uint s = sizeof(XBW)+alpha->size()+last->size();
//s += sizeof(uint)*nodesCount;
s += uint_len(nodesCount,balpha)*sizeof(uint);
return s;
}
void XBW::getChildren(const uint n, uint * ini, uint * fin) const {
if(isLeaf(n)) {
*fin=0;
*ini=1;
return;
}
//uint c = alphaTmp[n];
//uint c = alpha->access(n);
//uint c = get_field(alphaSeq,balpha,n);
uint c = getLabel(n);
uint r = alpha->rank(c, n);
uint y = A->select1(c);
uint z = last->rank1(y-1);
*ini = last->select1(z+r-1)+1;
*fin = last->select1(z+r);
}
uint XBW::getRankedChild(const uint n, const uint k) const {
uint ini, fin;
getChildren(n, &ini, &fin);
if(k>fin-ini+1)
return (uint)-1;
return ini+k-1;
}
uint XBW::getCharRankedChild(const uint n, const uint l, const uint k) const {
uint ini,fin;
getChildren(n, &ini, &fin);
if(ini>fin)
return (uint)-1;
uint y1 = alpha->rank(l, ini-1);
uint y2 = alpha->rank(l, fin);
if(k > y2-y1)
return (uint)-1;
return alpha->select(l,y1+k);
}
uint XBW::getDegree(const uint n) const {
uint ini,fin;
getChildren(n, &ini, &fin);
return fin-ini+1;
}
uint XBW::getCharDegree(const uint n, const uint l) const {
uint ini,fin;
getChildren(n, &ini, &fin);
if(ini>fin) return 0;
return alpha->rank(l, fin) - alpha->rank(l, ini-1);
}
uint XBW::getParent(const uint n) const {
if(n==0)
return (uint)-1;
uint c = A->rank1(n);
uint y = A->select1(c);
uint k = last->rank1(n-1)-last->rank1(y-1);
uint p = alpha->select(c,k+1);
return p;
}
bool XBW::isLeaf(const uint n) const {
return maxLabel==getLabel(n);
}
void XBW::subPathSearch(const uint * qry, const uint ql, uint * ini, uint * fin) const {
if(ql<=1) {
*ini = 0;
*fin = nodesCount-1;
return;
}
uint first = A->select1(qry[0]);
uint last = A->select1(qry[0]+1)-1;
//cout << "first=" << first << " last=" << last << endl;
for(uint i=1;i<ql-1;i++) {
uint k1 = alpha->rank(qry[i],first-1);
uint z1 = alpha->select(qry[i], k1+1);
uint k2 = alpha->rank(qry[i],last);
uint z2 = alpha->select(qry[i], k2);
if(k1==k2) { // No occurrence
*ini = 1;
*fin = 0;
return;
}
first = getRankedChild(z1,1);
last = getRankedChild(z2,getDegree(z2));
//cout << "first=" << first << " last=" << last << endl;
}
*ini = first;
*fin = last;
}
uint * XBW::subPathSearch(const uint * qry, const uint ql, uint * len) const {
uint ini, fin;
subPathSearch(qry,ql,&ini,&fin);
if(ini>fin) {
len = 0;
return NULL;
}
uint elem = qry[ql-1];
uint k1 = (ini==0?0:alpha->rank(elem, ini-1));
uint k2 = alpha->rank(elem, fin);
*len = k2-k1;
uint * ret = new uint[*len];
for(uint p=k1+1; p<=k2; p++)
ret[p-k1-1] = alpha->select(elem, p);
return ret;
}
uint XBW::getLabel(const uint n) const {
//return alphaTmp[n];
//return alpha->access(n);
return get_field(alphaSeq,balpha,n);
}