ca935948030ec3e3deb5f8c103f5e9de9915484b
[SXSI/XMLTree.git] / libcds / src / static_sequence / static_sequence_wvtree.cpp
1 /* static_sequence_wvtree.cpp
2  * Copyright (C) 2008, Francisco Claude, all rights reserved.
3  *
4  * static_sequence_wvtree definition
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21  
22 #include <static_sequence_wvtree.h>
23
24 static_sequence_wvtree::static_sequence_wvtree(uint * symbols, uint n, wt_coder * c, static_bitsequence_builder * bmb, alphabet_mapper * am) {
25     this->n = n;
26   for(uint i=0;i<n;i++) 
27     symbols[i] = am->map(symbols[i]);
28   this->am = am;
29         am->use();
30   this->c=c;
31         c->use();
32         root = new wt_node_internal(symbols, n, 0, c, bmb);
33   for(uint i=0;i<n;i++) 
34     symbols[i] = am->unmap(symbols[i]);  
35 }
36
37 static_sequence_wvtree::static_sequence_wvtree(uchar * symbols, uint n, wt_coder * c, static_bitsequence_builder * bmb, alphabet_mapper * am) {
38     this->n = n;
39   for(uint i=0;i<n;i++) 
40     symbols[i] = (uchar)am->map((uint)symbols[i]);
41   this->am = am;
42         am->use();
43   this->c=c;
44         c->use();
45         uint *done = new uint[n/W+1];
46         for (uint i = 0; i < n/W+1; i++)
47             done[i] = 0;
48         root = new wt_node_internal(symbols, n, 0, c, bmb, 0, done);
49         delete [] done;
50         delete [] symbols;
51         symbols = 0; // Already deleted!
52 //  for(uint i=0;i<n;i++) 
53 //    symbols[i] = (uchar)am->unmap((uint)symbols[i]);  
54 }
55
56 static_sequence_wvtree::static_sequence_wvtree() {}
57
58 static_sequence_wvtree::~static_sequence_wvtree() {
59         delete root;
60         am->unuse();
61   c->unuse(); 
62 }
63
64 uint static_sequence_wvtree::rank(uint symbol, uint pos) {
65         return root->rank(am->map(symbol), pos, 0, c);
66 }
67
68 uint static_sequence_wvtree::rankLessThan(uint &symbol, uint pos) {
69     uint s = am->map(symbol);
70 //    std::cout << "lessthan..." << std::endl;
71     uint r = root->rankLessThan(s, pos);
72     symbol = am->unmap(s);
73     return r;
74 }
75
76
77 uint static_sequence_wvtree::count(uint s) {
78   return root->rank(am->map(s), len-1, 0, c);
79 }
80
81 uint static_sequence_wvtree::select(uint symbol, uint pos) {
82         uint ret = root->select(am->map(symbol), pos, 0, c);
83         if(ret==((uint)-1)) return (uint)-1;
84         return ret-1;
85 }
86
87 uint static_sequence_wvtree::access(uint pos) {
88         return am->unmap(root->access(pos));
89 }
90
91 vector<int> static_sequence_wvtree::access(uint i, uint j, uint min, uint max)
92 {
93     vector<int> resultSet;
94     root->access(resultSet, i, j, am->map(min), am->map(max), c->depth()-1, 0);
95     for (vector<int>::iterator it = resultSet.begin(); it != resultSet.end(); ++it)
96         *it = am->unmap(*it);
97     return resultSet;
98 }
99
100 vector<int> static_sequence_wvtree::accessAll(uint i, uint j)
101 {
102     vector<int> resultSet;
103     if (j < i)
104         return resultSet;
105
106     // resultSet.reserve(j-i+1); // avoid reallocation
107     root->access(resultSet, i, j);
108     for (vector<int>::iterator it = resultSet.begin(); it != resultSet.end(); ++it)
109         *it = am->unmap(*it);
110     return resultSet;
111 }
112
113 uint static_sequence_wvtree::count(uint i, uint j, uint min, uint max)
114 {
115     return root->access(i, j, am->map(min), am->map(max), c->depth()-1, 0);
116 }
117
118
119 uint static_sequence_wvtree::size() {
120         /*cout << "WT: " << root->size() << endl;
121         cout << "Coder: " << c->size() << endl;
122         cout << "AM: " << am->size() << endl;*/
123         return sizeof(static_sequence_wvtree)+sizeof(uint)+root->size()+am->size()+c->size();
124 }
125
126 uint static_sequence_wvtree::save(FILE * fp) { 
127   uint wr = WVTREE_HDR;
128   wr = fwrite(&wr,sizeof(uint),1,fp);
129   if(wr!=1) return 1;
130   wr = fwrite(&n,sizeof(uint),1,fp);
131   if(wr!=1) return 1;
132   if(c->save(fp)) return 1;
133   if(am->save(fp)) return 1;
134   if(root->save(fp)) return 1;
135   return 0;
136 }
137
138 static_sequence_wvtree * static_sequence_wvtree::load(FILE *fp) {
139   uint rd;
140   if(fread(&rd,sizeof(uint),1,fp)!=1) return NULL;
141   if(rd!=WVTREE_HDR) return NULL;
142   static_sequence_wvtree * ret = new static_sequence_wvtree();
143   if(fread(&ret->n,sizeof(uint),1,fp)!=1) return NULL;
144   ret->c = wt_coder::load(fp);
145         ret->c->use();
146   ret->am = alphabet_mapper::load(fp);
147         ret->am->use();
148   ret->root = wt_node::load(fp);
149   return ret;
150 }