+// return value is rank of symbol (less or equal to the given symbol) that has rank > 0,
+// the parameter symbol is updated accordinly
+uint wt_node_internal::rankLessThan(uint &symbol, uint pos, uint l, wt_coder * c)
+{
+ bool is_set = c->is_set(symbol,l);
+ using std::cout;
+ using std::endl;
+// cout << "l = " << l << ", symbol = " << (uchar)symbol << ", rank0 = " << bitmap->rank0(pos) << ", rank1 = " << bitmap->rank1(pos) << endl;
+
+ uint result = -1;
+ if(!is_set) {
+ if(left_child==NULL) return -1;
+ uint rank = bitmap->rank0(pos);
+ if(rank != 0)
+ result = left_child->rankLessThan(symbol,rank-1,l+1,c);
+ return result;
+ }
+
+ uint rank = bitmap->rank1(pos);
+ if (rank != 0 && right_child != NULL)
+ result = right_child->rankLessThan(symbol, rank-1,l+1,c);
+
+// cout << "recursion to leftchild at l = " << l << ", symbol = " << (uchar)symbol << ", rank0 = " << bitmap->rank0(pos) << ", rank1 = " << bitmap->rank1(pos) << endl;
+ // check left child for symbols <= givenSymbol
+ if (result != -1 || left_child == NULL)
+ return result;
+ return left_child->rankLessThan(symbol, bitmap->rank0(pos)-1);
+}
+
+uint wt_node_internal::rankLessThan(uint &symbol, uint pos)
+{
+ uint result = -1;
+ using std::cout;
+ using std::endl;
+// cout << "pos = " << pos << ", symbol = " << (uchar)symbol << endl;
+
+ if (pos == -1)
+ return -1;
+ if(right_child!=NULL)
+ result = right_child->rankLessThan(symbol, bitmap->rank1(pos)-1);
+ if(result == -1 && left_child!=NULL)
+ return left_child->rankLessThan(symbol, bitmap->rank0(pos)-1);
+ return result;
+}
+
+