3f7a977f44f7cdbe05d0c904c416c87569158b05
[SXSI/XMLTree.git] / libcds / src / static_sequence / static_sequence_wvtree_noptrs.h
1 /* static_sequence_wvtree_noptrs.h
2  * Copyright (C) 2008, Francisco Claude, all rights reserved.
3  *
4  * static_sequence_wvtree_noptrs 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 #ifndef _STATIC_SEQUENCE_WVTREE_NOPTRS_H
23 #define _STATIC_SEQUENCE_WVTREE_NOPTRS_H
24
25 #include <iostream>
26 #include <cassert>
27 #include <basics.h>
28 #include <static_bitsequence.h>
29 #include <static_bitsequence_builder.h>
30 #include <static_sequence.h>
31 #include <alphabet_mapper.h>
32
33 using namespace std;
34
35 class static_sequence_wvtree_noptrs : public static_sequence {
36   public:
37
38     /** Builds a Wavelet Tree for the string
39      * pointed by symbols assuming its length
40      * equals n and uses bmb to build the bitsequence */
41     static_sequence_wvtree_noptrs(uint * symbols, uint n, static_bitsequence_builder * bmb, alphabet_mapper * am, bool deleteSymbols = false);
42
43     /** Destroys the Wavelet Tree */
44     virtual ~static_sequence_wvtree_noptrs();
45
46     virtual uint rank(uint symbol, uint pos);
47     virtual uint select(uint symbol, uint i);
48     virtual uint access(uint pos);
49     virtual uint size();
50
51     virtual vector<int> access(uint i, uint j, uint min, uint max);
52     virtual vector<int> accessAll(uint i, uint j);
53     virtual uint count(uint i, uint j, uint min, uint max);
54     
55     virtual uint save(FILE *fp);
56     static static_sequence_wvtree_noptrs * load(FILE *fp);
57
58   protected:
59     void access(vector<int> &result, uint i, uint j, uint min, uint max, uint l, uint pivot, uint start, uint end);
60     void accessAll(vector<int> &result, uint i, uint j, uint l, uint pivot, uint start, uint end);
61     uint count(uint i, uint j, uint min, uint max, uint l, uint pivot, uint start, uint end);
62
63     static_sequence_wvtree_noptrs();
64     
65     alphabet_mapper * am;
66     /** Only one bit-string for the Wavelet Tree. */
67     static_bitsequence **bitstring, *occ;
68
69     /** Length of the string. */
70     uint n;
71
72     /** Height of the Wavelet Tree. */
73     uint height,max_v;
74
75     /** Obtains the maximum value from the string
76      * symbols of length n */
77     uint max_value(uint * symbols, uint n);
78
79     /** How many bits are needed to represent val */
80     uint bits(uint val);
81
82     /** Returns true if val has its ind-th bit set
83      * to one. */
84     bool is_set(uint val, uint ind);
85
86     /** Sets the ind-th bit in val */
87     uint set(uint val, uint ind);
88
89     /** Recursive function for building the Wavelet Tree. */
90     void build_level(uint **bm, uint *symbols, uint level, uint length, uint offset);
91 };
92 #endif