Small updates, code clean up
[SXSI/XMLTree.git] / libcds / src / static_sequence / static_sequence_wvtree.cpp
index d5713ce..ca93594 100644 (file)
@@ -22,6 +22,7 @@
 #include <static_sequence_wvtree.h>
 
 static_sequence_wvtree::static_sequence_wvtree(uint * symbols, uint n, wt_coder * c, static_bitsequence_builder * bmb, alphabet_mapper * am) {
+    this->n = n;
   for(uint i=0;i<n;i++) 
     symbols[i] = am->map(symbols[i]);
   this->am = am;
@@ -34,15 +35,22 @@ static_sequence_wvtree::static_sequence_wvtree(uint * symbols, uint n, wt_coder
 }
 
 static_sequence_wvtree::static_sequence_wvtree(uchar * symbols, uint n, wt_coder * c, static_bitsequence_builder * bmb, alphabet_mapper * am) {
+    this->n = n;
   for(uint i=0;i<n;i++) 
     symbols[i] = (uchar)am->map((uint)symbols[i]);
   this->am = am;
        am->use();
   this->c=c;
        c->use();
-       root = new wt_node_internal(symbols, n, 0, c, bmb);
-  for(uint i=0;i<n;i++) 
-    symbols[i] = (uchar)am->unmap((uint)symbols[i]);  
+        uint *done = new uint[n/W+1];
+        for (uint i = 0; i < n/W+1; i++)
+            done[i] = 0;
+       root = new wt_node_internal(symbols, n, 0, c, bmb, 0, done);
+        delete [] done;
+        delete [] symbols;
+        symbols = 0; // Already deleted!
+//  for(uint i=0;i<n;i++) 
+//    symbols[i] = (uchar)am->unmap((uint)symbols[i]);  
 }
 
 static_sequence_wvtree::static_sequence_wvtree() {}
@@ -57,6 +65,15 @@ uint static_sequence_wvtree::rank(uint symbol, uint pos) {
        return root->rank(am->map(symbol), pos, 0, c);
 }
 
+uint static_sequence_wvtree::rankLessThan(uint &symbol, uint pos) {
+    uint s = am->map(symbol);
+//    std::cout << "lessthan..." << std::endl;
+    uint r = root->rankLessThan(s, pos);
+    symbol = am->unmap(s);
+    return r;
+}
+
+
 uint static_sequence_wvtree::count(uint s) {
   return root->rank(am->map(s), len-1, 0, c);
 }
@@ -71,6 +88,34 @@ uint static_sequence_wvtree::access(uint pos) {
        return am->unmap(root->access(pos));
 }
 
+vector<int> static_sequence_wvtree::access(uint i, uint j, uint min, uint max)
+{
+    vector<int> resultSet;
+    root->access(resultSet, i, j, am->map(min), am->map(max), c->depth()-1, 0);
+    for (vector<int>::iterator it = resultSet.begin(); it != resultSet.end(); ++it)
+        *it = am->unmap(*it);
+    return resultSet;
+}
+
+vector<int> static_sequence_wvtree::accessAll(uint i, uint j)
+{
+    vector<int> resultSet;
+    if (j < i)
+        return resultSet;
+
+    // resultSet.reserve(j-i+1); // avoid reallocation
+    root->access(resultSet, i, j);
+    for (vector<int>::iterator it = resultSet.begin(); it != resultSet.end(); ++it)
+        *it = am->unmap(*it);
+    return resultSet;
+}
+
+uint static_sequence_wvtree::count(uint i, uint j, uint min, uint max)
+{
+    return root->access(i, j, am->map(min), am->map(max), c->depth()-1, 0);
+}
+
+
 uint static_sequence_wvtree::size() {
        /*cout << "WT: " << root->size() << endl;
        cout << "Coder: " << c->size() << endl;