Added support for char arrays
[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, uint l, wt_coder * c) {
40 //    std::cout <<"this-symbol: " << (uchar)this->symbol << ", symbol = " << (uchar)symbol << ", pos = " << pos << std::endl;
41         if(symbol > this->symbol) return -1;
42         symbol = this->symbol;
43         pos++;
44         return pos;
45 }
46
47 uint wt_node_leaf::rankLessThan(uint &symbol, uint pos) {
48 //    std::cout <<"this-symbol: " << (uchar)this->symbol << ", symbol = " << (uchar)symbol << ", pos = " << pos << std::endl;
49     if (pos == -1)
50         return -1;
51     symbol = this->symbol;
52     pos++;
53     return pos;
54 }
55
56 uint wt_node_leaf::select(uint symbol, uint pos, uint l, wt_coder * c) {
57         if(symbol!=this->symbol) return (uint)-1;
58         if(pos==0 || pos>count) return (uint)-1;
59         return pos;
60 }
61
62 uint wt_node_leaf::access(uint pos) {
63 //   std::cout <<"this-symbol: " << (uchar)this->symbol << ", pos = " << pos << std::endl;
64
65         return symbol;
66 }
67
68 uint wt_node_leaf::size() {
69         return sizeof(wt_node_leaf);
70 }
71
72 uint wt_node_leaf::save(FILE *fp) {
73   uint wr = WT_NODE_LEAF_HDR;
74   wr = fwrite(&wr,sizeof(uint),1,fp);
75   wr += fwrite(&count,sizeof(uint),1,fp);
76   wr += fwrite(&symbol,sizeof(uint),1,fp);
77   return wr-3;
78 }
79
80 wt_node_leaf * wt_node_leaf::load(FILE *fp) {
81   uint rd;
82   if(fread(&rd,sizeof(uint),1,fp)!=1) return NULL;
83   if(rd!=WT_NODE_LEAF_HDR) return NULL;
84   wt_node_leaf * ret = new wt_node_leaf();
85   rd = fread(&(ret->count),sizeof(uint),1,fp);
86   rd += fread(&(ret->symbol),sizeof(uint),1,fp);
87   if(rd!=2) { delete ret; return NULL; }
88   return ret;
89 }