1 /* static_sequence_wvtree.cpp
2 * Copyright (C) 2008, Francisco Claude, all rights reserved.
4 * static_sequence_wvtree definition
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 <static_sequence_wvtree.h>
24 static_sequence_wvtree::static_sequence_wvtree(uint * symbols, uint n, wt_coder * c, static_bitsequence_builder * bmb, alphabet_mapper * am) {
26 symbols[i] = am->map(symbols[i]);
31 root = new wt_node_internal(symbols, n, 0, c, bmb);
33 symbols[i] = am->unmap(symbols[i]);
36 static_sequence_wvtree::static_sequence_wvtree(uchar * symbols, uint n, wt_coder * c, static_bitsequence_builder * bmb, alphabet_mapper * am) {
38 symbols[i] = (uchar)am->map((uint)symbols[i]);
43 root = new wt_node_internal(symbols, n, 0, c, bmb);
44 symbols = 0; // Already deleted!
45 // for(uint i=0;i<n;i++)
46 // symbols[i] = (uchar)am->unmap((uint)symbols[i]);
49 static_sequence_wvtree::static_sequence_wvtree() {}
51 static_sequence_wvtree::~static_sequence_wvtree() {
57 uint static_sequence_wvtree::rank(uint symbol, uint pos) {
58 return root->rank(am->map(symbol), pos, 0, c);
61 uint static_sequence_wvtree::rankLessThan(uint &symbol, uint pos) {
62 uint s = am->map(symbol);
63 std::cout << "lessthan..." << std::endl;
64 uint r = root->rankLessThan(s, pos, 0, c);
65 symbol = am->unmap(s);
70 uint static_sequence_wvtree::count(uint s) {
71 return root->rank(am->map(s), len-1, 0, c);
74 uint static_sequence_wvtree::select(uint symbol, uint pos) {
75 uint ret = root->select(am->map(symbol), pos, 0, c);
76 if(ret==((uint)-1)) return (uint)-1;
80 uint static_sequence_wvtree::access(uint pos) {
81 return am->unmap(root->access(pos));
84 vector<int> static_sequence_wvtree::access(uint i, uint j, uint min, uint max)
86 vector<int> resultSet;
87 root->access(resultSet, i, j, am->map(min), am->map(max), c->depth()-1, 0);
88 for (vector<int>::iterator it = resultSet.begin(); it != resultSet.end(); ++it)
93 vector<int> static_sequence_wvtree::accessAll(uint i, uint j)
95 vector<int> resultSet;
99 resultSet.reserve(j-i+1);
100 root->access(resultSet, i, j);
101 for (vector<int>::iterator it = resultSet.begin(); it != resultSet.end(); ++it)
102 *it = am->unmap(*it);
106 uint static_sequence_wvtree::count(uint i, uint j, uint min, uint max)
108 return root->access(i, j, am->map(min), am->map(max), c->depth()-1, 0);
112 uint static_sequence_wvtree::size() {
113 /*cout << "WT: " << root->size() << endl;
114 cout << "Coder: " << c->size() << endl;
115 cout << "AM: " << am->size() << endl;*/
116 return sizeof(static_sequence_wvtree)+sizeof(uint)+root->size()+am->size()+c->size();
119 uint static_sequence_wvtree::save(FILE * fp) {
120 uint wr = WVTREE_HDR;
121 wr = fwrite(&wr,sizeof(uint),1,fp);
123 wr = fwrite(&n,sizeof(uint),1,fp);
125 if(c->save(fp)) return 1;
126 if(am->save(fp)) return 1;
127 if(root->save(fp)) return 1;
131 static_sequence_wvtree * static_sequence_wvtree::load(FILE *fp) {
133 if(fread(&rd,sizeof(uint),1,fp)!=1) return NULL;
134 if(rd!=WVTREE_HDR) return NULL;
135 static_sequence_wvtree * ret = new static_sequence_wvtree();
136 if(fread(&ret->n,sizeof(uint),1,fp)!=1) return NULL;
137 ret->c = wt_coder::load(fp);
139 ret->am = alphabet_mapper::load(fp);
141 ret->root = wt_node::load(fp);