New (faster) representation for tags added; faster construction of parentheses
[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::select(uint symbol, uint pos, uint l, wt_coder * c) {
40         if(symbol!=this->symbol) return (uint)-1;
41         if(pos==0 || pos>count) return (uint)-1;
42         return pos;
43 }
44
45 uint wt_node_leaf::access(uint pos) {
46         return symbol;
47 }
48
49 uint wt_node_leaf::size() {
50         return sizeof(wt_node_leaf);
51 }
52
53 uint wt_node_leaf::save(FILE *fp) {
54   uint wr = WT_NODE_LEAF_HDR;
55   wr = fwrite(&wr,sizeof(uint),1,fp);
56   wr += fwrite(&count,sizeof(uint),1,fp);
57   wr += fwrite(&symbol,sizeof(uint),1,fp);
58   return wr-3;
59 }
60
61 wt_node_leaf * wt_node_leaf::load(FILE *fp) {
62   uint rd;
63   if(fread(&rd,sizeof(uint),1,fp)!=1) return NULL;
64   if(rd!=WT_NODE_LEAF_HDR) return NULL;
65   wt_node_leaf * ret = new wt_node_leaf();
66   rd = fread(&(ret->count),sizeof(uint),1,fp);
67   rd += fread(&(ret->symbol),sizeof(uint),1,fp);
68   if(rd!=2) { delete ret; return NULL; }
69   return ret;
70 }