Create branch library-split
[SXSI/XMLTree.git] / libcds / src / static_bitsequence / static_bitsequence_rrr02.h
1 /* static_bitsequence_rrr02.h
2  * Copyright (C) 2008, Francisco Claude, all rights reserved.
3  *
4  * RRR02 Bitsequence - 
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
23 #ifndef _STATIC_BITSEQUENCE_RRR02_H
24 #define _STATIC_BITSEQUENCE_RRR02_H
25
26 #define BLOCK_SIZE 15
27 #define DEFAULT_SAMPLING 32
28
29 #include <static_bitsequence.h>
30 #include <table_offset.h>
31 #include <cassert>
32 #include <iostream>
33
34 //using namespace std;
35
36 /** Implementation of Raman, Raman and Rao's [1] proposal for rank/select capable
37  *  data structures, it achieves space nH_0, O(sample_rate) time for rank and O(log len) 
38  *  for select. The practial implementation is based on [2]
39  * 
40  *  [1] R. Raman, V. Raman and S. Rao. Succinct indexable dictionaries with applications 
41  *     to encoding $k$-ary trees and multisets. SODA02.
42  *  [2] F. Claude and G. Navarro. Practical Rank/Select over Arbitrary Sequences. SPIRE08.
43  *
44  *  @author Francisco Claude
45  */
46 class static_bitsequence_rrr02: public static_bitsequence {
47 public:
48   static_bitsequence_rrr02(uint * bitseq, uint len, uint sample_rate=DEFAULT_SAMPLING);
49   virtual ~static_bitsequence_rrr02();
50   
51   /** Returns the number of zeros until position i */
52   virtual uint rank0(uint i);
53   
54   /** Returns the number of ones until position i */
55   virtual uint rank1(uint i);
56   
57   /** Returns the position of the i-th zero 
58    * @return (uint)-1 if i=0, len if i>num_zeros or the position */
59   virtual uint select0(uint i);
60   
61   /** Returns the position of the i-th one 
62    * @return (uint)-1 if i=0, len if i>num_ones or the position */
63   virtual uint select1(uint i);
64   
65   /** Returns the i-th bit */
66   virtual bool access(uint i);
67   
68   /** Returns the size of the structure in bytes */
69   virtual uint size();
70   
71   /** Stores the bitmap given a file pointer, return 0 in case of success */
72         virtual int save(FILE * fp);
73   
74   /** Reads the bitmap from a file pointer, returns NULL in case of error */
75         static static_bitsequence_rrr02 * load(FILE * fp);
76   
77   /** Creates a new sampling for the queries */
78         void create_sampling(uint sampling_rate);
79   
80   /** Frees the space required by the table E, which is static and global
81    *  to all instances.
82    */
83   static void delete_E() {
84     delete E;
85   }
86
87   
88 protected:
89   static_bitsequence_rrr02();
90         /** Classes and offsets */
91   uint *C, *O;
92         /** Length of C and O (in uints) */
93         uint C_len, O_len;
94         /** Bits required per field for C and in total for O */
95         uint C_field_bits, O_bits_len;
96         /** C and O samplings */
97         uint *C_sampling, *O_pos;
98         /** Length of the samplings */
99         uint C_sampling_len,O_pos_len;
100         /** Lenght in bits per field */
101         uint C_sampling_field_bits,O_pos_field_bits;
102         /** Sample rate */
103         uint sample_rate;
104
105         static table_offset * E;
106 };
107
108 #endif  /* _STATIC_BITSEQUENCE_RRR02_H */