6c036fd212b045c78a52dcf6616b8df69bc4a7c3
[SXSI/XMLTree.git] / libcds / src / static_sequence / wt_node_leaf.cpp
1 /* wt_node_leaf.cpp
2  * Copyright (C) 2008, Francisco Claude, all rights reserved.
3  *
4  * wt_node_leaf
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 <wt_node_leaf.h>
23
24 wt_node_leaf::wt_node_leaf(uint symbol, uint count) {
25         this->symbol = symbol;
26         this->count = count;
27 }
28
29 wt_node_leaf::wt_node_leaf() {}
30
31 wt_node_leaf::~wt_node_leaf() {}
32
33 uint wt_node_leaf::rank(uint symbol, uint pos, uint l, wt_coder * c) {
34         if(symbol!=this->symbol) return 0;
35         pos++;
36         return pos;
37 }
38
39 uint wt_node_leaf::rankLessThan(uint &symbol, uint pos) {
40 //    std::cout <<"this-symbol: " << (uchar)this->symbol << ", symbol = " << (uchar)symbol << ", pos = " << pos << std::endl;
41     if (pos == (uint)-1 || symbol < this->symbol)
42         return -1;
43     symbol = this->symbol;
44     pos++;
45     return pos;
46 }
47
48 uint wt_node_leaf::select(uint symbol, uint pos, uint l, wt_coder * c) {
49         if(symbol!=this->symbol) return (uint)-1;
50         if(pos==0 || pos>count) return (uint)-1;
51         return pos;
52 }
53
54 uint wt_node_leaf::access(uint pos) {
55 //   std::cout <<"this-symbol: " << (uchar)this->symbol << ", pos = " << pos << std::endl;
56
57         return symbol;
58 }
59
60 void wt_node_leaf::access(vector<int> &result, uint i, uint j, uint min, uint max, uint l, uint pivot)
61 {
62 //    std::cout << "At l = " << l << ", [" << i << ", " << j  << "], [" << min << ", " << max << "], symbol = " << symbol << std::endl;
63     
64     if (i <= j && symbol >= min && symbol <= max)
65         result.push_back((int)symbol);
66 }
67
68 void wt_node_leaf::access(vector<int> &result, uint i, uint j)
69 {
70 //    std::cout << "At l = " << l << ", [" << i << ", " << j  << "], [" << min << ", " << max << "], symbol = " << symbol << std::endl;
71     
72     if (i <= j)
73         result.push_back((int)symbol);
74 }
75
76 uint wt_node_leaf::access(uint i, uint j, uint min, uint max, uint l, uint pivot)
77 {
78 //    std::cout << "At l = " << l << ", [" << i << ", " << j  << "], [" << min << ", " << max << "], symbol = " << symbol << std::endl;
79     
80     if (i <= j && symbol >= min && symbol <= max)
81         return 1;
82     return 0;
83 }
84
85 uint wt_node_leaf::size() {
86         return sizeof(wt_node_leaf);
87 }
88
89 uint wt_node_leaf::save(FILE *fp) {
90   uint wr = WT_NODE_LEAF_HDR;
91   wr = fwrite(&wr,sizeof(uint),1,fp);
92   wr += fwrite(&count,sizeof(uint),1,fp);
93   wr += fwrite(&symbol,sizeof(uint),1,fp);
94   return wr-3;
95 }
96
97 wt_node_leaf * wt_node_leaf::load(FILE *fp) {
98   uint rd;
99   if(fread(&rd,sizeof(uint),1,fp)!=1) return NULL;
100   if(rd!=WT_NODE_LEAF_HDR) return NULL;
101   wt_node_leaf * ret = new wt_node_leaf();
102   rd = fread(&(ret->count),sizeof(uint),1,fp);
103   rd += fread(&(ret->symbol),sizeof(uint),1,fp);
104   if(rd!=2) { delete ret; return NULL; }
105   return ret;
106 }