New (faster) representation for tags added; faster construction of parentheses
[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   for(uint i=0;i<n;i++) 
26     symbols[i] = am->map(symbols[i]);
27   this->am = am;
28         am->use();
29   this->c=c;
30         c->use();
31         root = new wt_node_internal(symbols, n, 0, c, bmb);
32   for(uint i=0;i<n;i++) 
33     symbols[i] = am->unmap(symbols[i]);  
34 }
35
36 static_sequence_wvtree::static_sequence_wvtree() {}
37
38 static_sequence_wvtree::~static_sequence_wvtree() {
39         delete root;
40         am->unuse();
41   c->unuse(); 
42 }
43
44 uint static_sequence_wvtree::rank(uint symbol, uint pos) {
45         return root->rank(am->map(symbol), pos, 0, c);
46 }
47
48 uint static_sequence_wvtree::count(uint s) {
49   return root->rank(am->map(s), len-1, 0, c);
50 }
51
52 uint static_sequence_wvtree::select(uint symbol, uint pos) {
53         return root->select(am->map(symbol), pos, 0, c)-1;
54 }
55
56 uint static_sequence_wvtree::access(uint pos) {
57         return am->unmap(root->access(pos));
58 }
59
60 uint static_sequence_wvtree::size() {
61         /*cout << "WT: " << root->size() << endl;
62         cout << "Coder: " << c->size() << endl;
63         cout << "AM: " << am->size() << endl;*/
64         return sizeof(static_sequence_wvtree)+sizeof(uint)+root->size()+am->size()+c->size();
65 }
66
67 uint static_sequence_wvtree::save(FILE * fp) { 
68   uint wr = WVTREE_HDR;
69   wr = fwrite(&wr,sizeof(uint),1,fp);
70   if(wr!=1) return 1;
71   wr = fwrite(&n,sizeof(uint),1,fp);
72   if(wr!=1) return 1;
73   if(c->save(fp)) return 1;
74   if(am->save(fp)) return 1;
75   if(root->save(fp)) return 1;
76   return 0;
77 }
78
79 static_sequence_wvtree * static_sequence_wvtree::load(FILE *fp) {
80   uint rd;
81   if(fread(&rd,sizeof(uint),1,fp)!=1) return NULL;
82   if(rd!=WVTREE_HDR) return NULL;
83   static_sequence_wvtree * ret = new static_sequence_wvtree();
84   if(fread(&ret->n,sizeof(uint),1,fp)!=1) return NULL;
85   ret->c = wt_coder::load(fp);
86         ret->c->use();
87   ret->am = alphabet_mapper::load(fp);
88         ret->am->use();
89   ret->root = wt_node::load(fp);
90   return ret;
91 }