X-Git-Url: http://git.nguyen.vg/gitweb/?a=blobdiff_plain;ds=sidebyside;f=libcds%2Fsrc%2Fstatic_sequence%2Fstatic_sequence_wvtree.cpp;h=ca935948030ec3e3deb5f8c103f5e9de9915484b;hb=e38bc834442d5369a523ba47d74865e48995ace4;hp=b1353d422837585605a1dfc992a8c0513900c9df;hpb=450ba3c9c74665094fb8f6821d6cc92d2bf23011;p=SXSI%2FXMLTree.git diff --git a/libcds/src/static_sequence/static_sequence_wvtree.cpp b/libcds/src/static_sequence/static_sequence_wvtree.cpp index b1353d4..ca93594 100644 --- a/libcds/src/static_sequence/static_sequence_wvtree.cpp +++ b/libcds/src/static_sequence/static_sequence_wvtree.cpp @@ -1,4 +1,4 @@ -/* static_sequence_wvtree.h +/* static_sequence_wvtree.cpp * Copyright (C) 2008, Francisco Claude, all rights reserved. * * static_sequence_wvtree definition @@ -22,39 +22,100 @@ #include 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;imap(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;iunmap(symbols[i]); } +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;imap((uint)symbols[i]); + this->am = am; + am->use(); + this->c=c; + c->use(); + 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;iunmap((uint)symbols[i]); +} + static_sequence_wvtree::static_sequence_wvtree() {} static_sequence_wvtree::~static_sequence_wvtree() { delete root; - delete am; - delete c; + am->unuse(); + c->unuse(); } 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); } uint static_sequence_wvtree::select(uint symbol, uint pos) { - return root->select(am->map(symbol), pos, 0, c)-1; + uint ret = root->select(am->map(symbol), pos, 0, c); + if(ret==((uint)-1)) return (uint)-1; + return ret-1; } uint static_sequence_wvtree::access(uint pos) { return am->unmap(root->access(pos)); } +vector static_sequence_wvtree::access(uint i, uint j, uint min, uint max) +{ + vector resultSet; + root->access(resultSet, i, j, am->map(min), am->map(max), c->depth()-1, 0); + for (vector::iterator it = resultSet.begin(); it != resultSet.end(); ++it) + *it = am->unmap(*it); + return resultSet; +} + +vector static_sequence_wvtree::accessAll(uint i, uint j) +{ + vector resultSet; + if (j < i) + return resultSet; + + // resultSet.reserve(j-i+1); // avoid reallocation + root->access(resultSet, i, j); + for (vector::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; @@ -81,7 +142,9 @@ static_sequence_wvtree * static_sequence_wvtree::load(FILE *fp) { static_sequence_wvtree * ret = new static_sequence_wvtree(); if(fread(&ret->n,sizeof(uint),1,fp)!=1) return NULL; ret->c = wt_coder::load(fp); + ret->c->use(); ret->am = alphabet_mapper::load(fp); + ret->am->use(); ret->root = wt_node::load(fp); return ret; }