Initial import of XMLTree
[SXSI/XMLTree.git] / libcds / src / static_bitsequence / static_bitsequence.h
1 /* static_bitsequence.h
2  * Copyright (C) 2008, Francisco Claude, all rights reserved.
3  *
4  * static_bitsequence 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_BITSEQUENCE_H
23 #define _STATIC_BITSEQUENCE_H
24
25 #define RRR02_HDR 2
26 #define BRW32_HDR 3
27
28 #include <basics.h>
29 #include <iostream>
30
31
32 using namespace std;
33
34 /** Base class for static bitsequences, contains many abstract functions, so this can't
35  *  be instantiated. It includes base implementations for rank0, select0 and select1 based
36  *  on rank0.
37  * 
38  *  @author Francisco Claude
39  */
40 class static_bitsequence {
41   
42 public:
43   virtual ~static_bitsequence() {};
44
45         /** Returns the number of zeros until position i */
46   virtual uint rank0(uint i);
47
48         /** Returns the position of the i-th zero 
49          * @return (uint)-1 if i=0, len if i>num_zeros or the position */
50   virtual uint select0(uint i);
51
52         /** Returns the number of ones until position i */
53   virtual uint rank1(uint i);
54
55         /** Returns the position of the i-th one 
56          * @return (uint)-1 if i=0, len if i>num_ones or the position */
57   virtual uint select1(uint i);
58
59         /** Returns the i-th bit */
60   virtual bool access(uint i);
61
62         /** Returns the length in bits of the bitmap */
63   virtual uint length();
64
65         /** Returns how many ones are in the bitstring */
66   virtual uint count_one();
67
68         /** Returns how many zeros are in the bitstring */
69   virtual uint count_zero();
70
71         /** Returns the size of the structure in bytes */
72   virtual uint size()=0;
73
74   /** Stores the bitmap given a file pointer, return 0 in case of success */
75         virtual int save(FILE * fp)=0;
76   
77   /** Reads a bitmap determining the type */
78   static static_bitsequence * load(FILE * fp);
79   
80 protected:
81         /** Length of the bitstring */
82   uint len;
83         /** Number of ones in the bitstring */
84         uint ones;
85   
86 };
87
88 #include "static_bitsequence_rrr02.h"
89 #include "static_bitsequence_naive.h"
90 #include "static_bitsequence_brw32.h"
91
92 #endif  /* _STATIC_BITSEQUENCE_H */
93