399f3c24294660063fb20963c66e3e251fc76697
[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     // symbols is an array of elements of "width" bits.
44     static_sequence_wvtree_noptrs(uint * symbols, uint n, unsigned width, static_bitsequence_builder * bmb, alphabet_mapper * am, bool deleteSymbols = false);
45
46     /** Destroys the Wavelet Tree */
47     virtual ~static_sequence_wvtree_noptrs();
48
49     virtual uint rank(uint symbol, uint pos);
50     virtual uint select(uint symbol, uint i);
51     virtual uint access(uint pos);
52     virtual uint size();
53
54     virtual vector<int> access(uint i, uint j, uint min, uint max);
55     virtual vector<int> accessAll(uint i, uint j);
56     virtual uint count(uint i, uint j, uint min, uint max);
57     
58     virtual uint save(FILE *fp);
59     static static_sequence_wvtree_noptrs * load(FILE *fp);
60
61   protected:
62     void access(vector<int> &result, uint i, uint j, uint min, uint max, uint l, uint pivot, uint start, uint end);
63     void accessAll(vector<int> &result, uint i, uint j, uint l, uint pivot, uint start, uint end);
64     uint count(uint i, uint j, uint min, uint max, uint l, uint pivot, uint start, uint end);
65
66     static_sequence_wvtree_noptrs();
67     
68     alphabet_mapper * am;
69     /** Only one bit-string for the Wavelet Tree. */
70     static_bitsequence **bitstring, *occ;
71
72     /** Length of the string. */
73     uint n;
74
75     /** Height of the Wavelet Tree. */
76     uint height,max_v;
77
78     /** Obtains the maximum value from the string
79      * symbols of length n */
80     uint max_value(uint * symbols, uint n);
81     uint max_value(uint * symbols, unsigned width, uint n);
82
83     /** How many bits are needed to represent val */
84     uint bits(uint val);
85
86     /** Returns true if val has its ind-th bit set
87      * to one. */
88     bool is_set(uint val, uint ind);
89
90     /** Sets the ind-th bit in val */
91     uint set(uint val, uint ind);
92
93     /** Recursive function for building the Wavelet Tree. */
94     void build_level(uint **bm, uint *symbols, uint level, uint length, uint offset);
95     void build_level(uint **bm, uint *symbols, unsigned width, uint level, uint length, uint offset);
96 };
97 #endif