1 /* wt_node_internal.cpp
2 * Copyright (C) 2008, Francisco Claude, all rights reserved.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <wt_node_internal.h>
24 wt_node_internal::wt_node_internal(uint * symbols, uint n, uint l, wt_coder * c, static_bitsequence_builder * bmb) {
25 uint * ibitmap = new uint[n/W+1];
26 for(uint i=0;i<n/W+1;i++)
29 if(c->is_set(symbols[i],l))
31 bitmap = bmb->build(ibitmap, n);
33 uint count_right = bitmap->rank1(n-1);
34 uint count_left = n-count_right+1;
35 uint * left = new uint[count_left+1];
36 uint * right = new uint[count_right+1];
37 count_right = count_left = 0;
38 bool match_left = true, match_right = true;
39 for(uint i=0;i<n;i++) {
40 if(bitmap->access(i)) {
41 right[count_right++]=symbols[i];
43 if(right[count_right-1]!=right[count_right-2])
47 left[count_left++]=symbols[i];
49 if(left[count_left-1]!=left[count_left-2])
54 if(match_left/* && c->done(left[0],l+1)*/)
55 left_child = new wt_node_leaf(left[0], count_left);
57 left_child = new wt_node_internal(left, count_left, l+1, c, bmb);
62 if(match_right/* && c->done(right[0],l+1)*/)
63 right_child = new wt_node_leaf(right[0], count_right);
65 right_child = new wt_node_internal(right, count_right, l+1, c, bmb);
73 // Deletes symbols array!
74 wt_node_internal::wt_node_internal(uchar * symbols, uint n, uint l, wt_coder * c, static_bitsequence_builder * bmb) {
75 uint * ibitmap = new uint[n/W+1];
76 for(uint i=0;i<n/W+1;i++)
79 if(c->is_set((uint)symbols[i],l))
81 bitmap = bmb->build(ibitmap, n);
83 uint count_right = bitmap->rank1(n-1);
84 uint count_left = n-count_right+1;
85 uchar * left = new uchar[count_left+1];
86 uchar * right = new uchar[count_right+1];
87 count_right = count_left = 0;
88 bool match_left = true, match_right = true;
89 for(uint i=0;i<n;i++) {
90 if(bitmap->access(i)) {
91 right[count_right++]=symbols[i];
93 if(right[count_right-1]!=right[count_right-2])
97 left[count_left++]=symbols[i];
99 if(left[count_left-1]!=left[count_left-2])
108 if(match_left/* && c->done(left[0],l+1)*/)
110 left_child = new wt_node_leaf((uint)left[0], count_left);
116 left_child = new wt_node_internal(left, count_left, l+1, c, bmb);
117 left = 0; // Already deleted
123 if(match_right/* && c->done(right[0],l+1)*/)
125 right_child = new wt_node_leaf((uint)right[0], count_right);
131 right_child = new wt_node_internal(right, count_right, l+1, c, bmb);
132 right = 0; // Already deleted
137 delete [] left; // already deleted if count_left > 0
142 wt_node_internal::wt_node_internal() { }
144 wt_node_internal::~wt_node_internal() {
146 if(right_child!=NULL) delete right_child;
147 if(left_child!=NULL) delete left_child;
150 uint wt_node_internal::rank(uint symbol, uint pos, uint l, wt_coder * c) {
151 bool is_set = c->is_set(symbol,l);
153 if(left_child==NULL) return 0;
154 return left_child->rank(symbol, bitmap->rank0(pos)-1,l+1,c);
157 if(right_child==NULL) return 0;
158 return right_child->rank(symbol, bitmap->rank1(pos)-1,l+1,c);
162 // return value is rank of symbol (less or equal to the given symbol) that has rank > 0,
163 // the parameter symbol is updated accordinly
164 uint wt_node_internal::rankLessThan(uint &symbol, uint pos, uint l, wt_coder * c)
166 bool is_set = c->is_set(symbol,l);
169 // cout << "l = " << l << ", symbol = " << (uchar)symbol << ", rank0 = " << bitmap->rank0(pos) << ", rank1 = " << bitmap->rank1(pos) << endl;
173 if(left_child==NULL) return -1;
174 uint rank = bitmap->rank0(pos);
176 result = left_child->rankLessThan(symbol,rank-1,l+1,c);
180 uint rank = bitmap->rank1(pos);
181 if (rank != 0 && right_child != NULL)
182 result = right_child->rankLessThan(symbol, rank-1,l+1,c);
184 // cout << "recursion to leftchild at l = " << l << ", symbol = " << (uchar)symbol << ", rank0 = " << bitmap->rank0(pos) << ", rank1 = " << bitmap->rank1(pos) << endl;
185 // check left child for symbols <= givenSymbol
186 if (result != -1 || left_child == NULL)
188 return left_child->rankLessThan(symbol, bitmap->rank0(pos)-1);
191 uint wt_node_internal::rankLessThan(uint &symbol, uint pos)
196 // cout << "pos = " << pos << ", symbol = " << (uchar)symbol << endl;
200 if(right_child!=NULL)
201 result = right_child->rankLessThan(symbol, bitmap->rank1(pos)-1);
202 if(result == -1 && left_child!=NULL)
203 return left_child->rankLessThan(symbol, bitmap->rank0(pos)-1);
208 uint wt_node_internal::select(uint symbol, uint pos, uint l, wt_coder * c) {
209 bool is_set = c->is_set(symbol, l);
214 uint new_pos = left_child->select(symbol, pos, l+1,c);
215 if(new_pos+1==0) return (uint)(-1);
216 ret = bitmap->select0(new_pos)+1;
218 if(right_child==NULL)
220 uint new_pos = right_child->select(symbol, pos, l+1,c);
221 if(new_pos+1==0) return (uint)(-1);
222 ret = bitmap->select1(new_pos)+1;
224 if(ret==0) return (uint)-1;
228 uint wt_node_internal::access(uint pos) {
229 bool is_set = bitmap->access(pos);
231 assert(left_child!=NULL);
232 return left_child->access(bitmap->rank0(pos)-1);
234 assert(right_child!=NULL);
235 return right_child->access(bitmap->rank1(pos)-1);
239 // Returns the value at given position and its rank
240 uint wt_node_internal::access(uint pos, uint &rank)
242 // p is the internal node we are pointing our finger at each step
243 wt_node_internal *p = this;
247 bool is_set = p->bitmap->access(pos);
248 // cout << "is_set = " << is_set << ", pos = " << pos << ", rank0 = " << bitmap->rank0(pos) << ", rank1 = " << bitmap->rank1(pos) << endl;
252 pos = p->bitmap->rank0(pos)-1;
253 wt_node_internal *tmp = dynamic_cast<wt_node_internal *>(p->left_child);
258 return p->left_child->access(0);
260 p = tmp; // new internal node
265 pos = p->bitmap->rank1(pos)-1;
266 wt_node_internal *tmp = dynamic_cast<wt_node_internal *>(p->right_child);
271 return p->right_child->access(0);
273 p = tmp; // new internal node
278 uint wt_node_internal::size() {
279 uint s = bitmap->size()+sizeof(wt_node_internal);
281 s += left_child->size();
282 if(right_child!=NULL)
283 s += right_child->size();
287 uint wt_node_internal::save(FILE *fp) {
288 uint wr = WT_NODE_INTERNAL_HDR;
289 wr = fwrite(&wr,sizeof(uint),1,fp);
291 if(bitmap->save(fp)) return 1;
292 if(left_child!=NULL) {
293 if(left_child->save(fp)) return 1;
295 wr = WT_NODE_NULL_HDR;
296 wr = fwrite(&wr,sizeof(uint),1,fp);
299 if(right_child!=NULL) {
300 if(right_child->save(fp)) return 1;
302 wr = WT_NODE_NULL_HDR;
303 wr = fwrite(&wr,sizeof(uint),1,fp);
309 wt_node_internal * wt_node_internal::load(FILE *fp) {
311 if(fread(&rd,sizeof(uint),1,fp)!=1) return NULL;
312 if(rd!=WT_NODE_INTERNAL_HDR) return NULL;
313 wt_node_internal * ret = new wt_node_internal();
314 ret->bitmap = static_bitsequence::load(fp);
315 ret->left_child = wt_node::load(fp);
316 ret->right_child = wt_node::load(fp);