-
+/* wt_node_internal.cpp
+ * Copyright (C) 2008, Francisco Claude, all rights reserved.
+ *
+ * wt_node_internal
+ *
+ * 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
+ *
+ */
+
#include <wt_node_internal.h>
-
wt_node_internal::wt_node_internal(uint * symbols, uint n, uint l, wt_coder * c, static_bitsequence_builder * bmb) {
uint * ibitmap = new uint[n/W+1];
for(uint i=0;i<n/W+1;i++)
}
}
if(count_left>0) {
- if(match_left)
+ if(match_left/* && c->done(left[0],l+1)*/)
left_child = new wt_node_leaf(left[0], count_left);
else
left_child = new wt_node_internal(left, count_left, l+1, c, bmb);
left_child = NULL;
}
if(count_right>0) {
- if(match_right)
+ if(match_right/* && c->done(right[0],l+1)*/)
right_child = new wt_node_leaf(right[0], count_right);
else
right_child = new wt_node_internal(right, count_right, l+1, c, bmb);
delete [] right;
}
+// Deletes symbols array!
+wt_node_internal::wt_node_internal(uchar * symbols, uint n, uint l, wt_coder * c, static_bitsequence_builder * bmb) {
+ uint * ibitmap = new uint[n/W+1];
+ for(uint i=0;i<n/W+1;i++)
+ ibitmap[i]=0;
+ for(uint i=0;i<n;i++)
+ if(c->is_set((uint)symbols[i],l))
+ bitset(ibitmap,i);
+ bitmap = bmb->build(ibitmap, n);
+ delete [] ibitmap;
+ uint count_right = bitmap->rank1(n-1);
+ uint count_left = n-count_right+1;
+ uchar * left = new uchar[count_left+1];
+ uchar * right = new uchar[count_right+1];
+ count_right = count_left = 0;
+ bool match_left = true, match_right = true;
+ for(uint i=0;i<n;i++) {
+ if(bitmap->access(i)) {
+ right[count_right++]=symbols[i];
+ if(count_right>1)
+ if(right[count_right-1]!=right[count_right-2])
+ match_right = false;
+ }
+ else {
+ left[count_left++]=symbols[i];
+ if(count_left>1)
+ if(left[count_left-1]!=left[count_left-2])
+ match_left = false;
+ }
+ }
+
+ delete [] symbols;
+ symbols = 0;
+
+ if(count_left>0) {
+ if(match_left/* && c->done(left[0],l+1)*/)
+ {
+ left_child = new wt_node_leaf((uint)left[0], count_left);
+ delete [] left;
+ left = 0;
+ }
+ else
+ {
+ left_child = new wt_node_internal(left, count_left, l+1, c, bmb);
+ left = 0; // Already deleted
+ }
+ } else {
+ left_child = NULL;
+ }
+ if(count_right>0) {
+ if(match_right/* && c->done(right[0],l+1)*/)
+ {
+ right_child = new wt_node_leaf((uint)right[0], count_right);
+ delete [] right;
+ right = 0;
+ }
+ else
+ {
+ right_child = new wt_node_internal(right, count_right, l+1, c, bmb);
+ right = 0; // Already deleted
+ }
+ } else {
+ right_child = NULL;
+ }
+// delete [] left; // already deleted
+// delete [] right;
+}
+
+
wt_node_internal::wt_node_internal() { }
wt_node_internal::~wt_node_internal() {
uint wt_node_internal::select(uint symbol, uint pos, uint l, wt_coder * c) {
bool is_set = c->is_set(symbol, l);
+ uint ret = 0;
if(!is_set) {
if(left_child==NULL)
return (uint)(-1);
uint new_pos = left_child->select(symbol, pos, l+1,c);
if(new_pos+1==0) return (uint)(-1);
- return bitmap->select0(new_pos)+1;
+ ret = bitmap->select0(new_pos)+1;
} else {
if(right_child==NULL)
return (uint)(-1);
uint new_pos = right_child->select(symbol, pos, l+1,c);
if(new_pos+1==0) return (uint)(-1);
- return bitmap->select1(new_pos)+1;
+ ret = bitmap->select1(new_pos)+1;
}
+ if(ret==0) return (uint)-1;
+ return ret;
}
uint wt_node_internal::access(uint pos) {