-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbit_vector.hpp
105 lines (84 loc) · 2.66 KB
/
bit_vector.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#ifndef BITVECTOR
#define BITVECTOR
#include "avl.hpp"
#include <vector>
#include <bitset>
// encapsualte the members that are needed for the bitvector tree structure
template <size_t S>
struct BV_Node : Node<BV_Node<S>> {
#ifdef ADS_DEBUG
uint32_t id;
#endif
uint32_t nums;
uint32_t ones;
std::bitset<S> *data;
BV_Node() {
#ifdef ADS_DEBUG
id = rand() % 99;
#endif
nums = 0;
ones = 0;
data = new std::bitset<S>;
}
~BV_Node() {
delete data;
}
};
// represents a dynamic bitvector that allows for inserts and deletes everywhere
// as well as rank and select queries
template <size_t S = 512>
class BitVector : public AVL<BV_Node<S>> {
private:
size_t BLOCK_SIZE;
size_t TARGET_SIZE;
size_t SPLIT_BOUND;
size_t LOWER_BOUND;
std::bitset<S> FULL_MASK;
std::bitset<S> MSB_MASK;
std::bitset<S> LSB_MASK;
BV_Node<S> *insert(BV_Node<S> *, uint32_t, bool);
BV_Node<S> *del(BV_Node<S> *, uint32_t);
void flip(BV_Node<S> *, uint32_t);
void set(BV_Node<S> *, uint32_t);
void unset(BV_Node<S> *, uint32_t);
uint32_t rank(BV_Node<S> *, uint32_t, bool);
uint32_t select(BV_Node<S> *, uint32_t, bool);
bool access(BV_Node<S> *, uint32_t);
void complement(BV_Node<S> *);
uint32_t size(BV_Node<S> *);
BV_Node<S> *find_block(BV_Node<S> *, uint32_t*);
#ifdef ADS_DEBUG
void show(BV_Node<S> *);
bool validate(BV_Node<S> *);
#endif
void propagate_update(BV_Node<S> *, BV_Node<S> *, int32_t, int32_t);
void split_block_update(BV_Node<S> *, BV_Node<S> *, BV_Node<S> *);
void steal_left(BV_Node<S> *, BV_Node<S> *);
void steal_right(BV_Node<S> *, BV_Node<S> *);
void merge_left_pre_update(BV_Node<S> *, BV_Node<S> *);
void merge_right_pre_update(BV_Node<S> *, BV_Node<S> *);
void merge_post_update(BV_Node<S> *);
void rotate_left_update(BV_Node<S> *);
void rotate_right_update(BV_Node<S> *);
public:
void insert(uint32_t, bool);
void del(uint32_t);
void flip(uint32_t);
void set(uint32_t);
void unset(uint32_t);
uint32_t rank(uint32_t, bool);
uint32_t select(uint32_t, bool);
bool access(uint32_t);
void complement();
uint32_t size();
std::vector<bool> extract();
#ifdef ADS_DEBUG
void show();
bool validate();
#endif
uint32_t operator[](uint32_t);
void operator~();
BitVector();
BitVector(std::vector<bool>);
};
#endif