HSDS
 All Data Structures Namespaces Files Functions Variables Typedefs Friends Macros
bit-vector.hpp
Go to the documentation of this file.
1 
6 #if !defined(HSDS_BIT_VECTOR_H_)
7 #define HSDS_BIT_VECTOR_H_
8 
9 #include <vector>
10 #include <iostream>
11 #include <algorithm>
12 #include <stdint.h>
13 #include "hsds/vector.hpp"
14 #include "hsds/rank-index.hpp"
15 
16 #if defined(_MSC_VER)
17 
18 #define FORCE_INLINE __forceinline
19 
20 #else
21 
22 #define FORCE_INLINE inline __attribute__((always_inline))
23 
24 #endif
25 
30 namespace hsds {
31 
32 namespace {
33 const uint32_t S_BLOCK_SIZE = 64;
34 const uint32_t L_BLOCK_SIZE = 512;
35 const uint32_t BLOCK_RATE = 8;
36 
37 const char* const E_OUT_OF_RANGE = "Out of range access";
38 const char* const E_SAVE_FILE = "Failed to save the bit vector.";
39 const char* const E_LOAD_FILE = "Failed to read file. File format is invalid.";
40 }
41 
42 const uint64_t NOT_FOUND = 0xFFFFFFFFFFFFFFFF;
43 
44 // forward declaration
45 class Exception;
46 class PopCount;
47 class RankIndex;
48 
53 class BitVector {
54 public:
55 
59  BitVector();
60 
66  BitVector(uint64_t size);
67 
71  virtual ~BitVector();
72 
76  void clear() {
77  BitVector().swap(*this);
78  }
79 
87  bool operator[](uint64_t i) const;
88 
95  void set(uint64_t i, bool b = true);
96 
102  void push_back(bool b);
103 
110  void push_back_bits(uint64_t x, uint64_t len);
111 
120  uint64_t get_bits(uint64_t pos, uint64_t len) const;
121 
129  void build(bool enable_faster_select1 = false, bool enable_faster_select0 = false);
130 
136  FORCE_INLINE uint64_t size() const {
137  return size_;
138  }
139 
147  FORCE_INLINE uint64_t size(bool b) const {
148  return b ? (num_of_1s_) : (size_ - num_of_1s_);
149  }
150 
157  FORCE_INLINE bool empty() const {
158  return size_ == 0;
159  }
160 
169  FORCE_INLINE uint64_t rank(uint64_t i, bool b = true) const {
170  return b ? rank1(i) : rank0(i);
171  }
172 
180  uint64_t rank0(uint64_t i) const;
181 
189  uint64_t rank1(uint64_t i) const;
190 
199  FORCE_INLINE uint64_t select(uint64_t x, bool b = true) const {
200  return b ? select1(x) : select0(x);
201  }
202 
210  uint64_t select0(uint64_t x) const;
211 
219  uint64_t select1(uint64_t x) const;
220 
228  void save(std::ostream &os) const throw (hsds::Exception);
229 
237  void load(std::istream &is) throw (hsds::Exception);
238 
249  uint64_t map(void* ptr, uint64_t size) throw (hsds::Exception);
250 
256  void swap(BitVector &x);
257 
258 private:
259  typedef uint64_t block_type;
260  typedef hsds::Vector<block_type> blocks_type;
261  typedef hsds::Vector<RankIndex> rank_dict_type;
262  typedef hsds::Vector<uint32_t> select_dict_type;
263 
264  blocks_type blocks_;
265  rank_dict_type rank_table_;
266  select_dict_type select0_table_;
267  select_dict_type select1_table_;
268  uint64_t size_;
269  uint64_t num_of_1s_;
270 
271  // Disable assingment operator
272  BitVector &operator=(const BitVector &);
273 };
274 
275 }
276 
277 #endif /* !defined(HSDS_BIT_VECTOR_H_) */