-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbit_array.h
253 lines (230 loc) · 7.04 KB
/
bit_array.h
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#ifndef __BIT_ARRAY__
#define __BIT_ARRAY__
#include <stdint.h>
#include <type_traits>
namespace compact_bit_array {
// bitfield based implementation
template <uint64_t bits, uint64_t size, class Enable = void>
class impl_bitfield;
// 000
template <uint64_t bits, uint64_t size>
class impl_bitfield < bits,
size, typename std::enable_if<!(bits & 7) && bits<64>::type> {
protected:
constexpr static uint64_t _shift = 0;
constexpr static uint64_t _mask = (1 << _shift) - 1;
struct _buf_t {
uint64_t _0 : bits;
} __attribute__((__packed__));
uint8_t _buf[(size * bits + 7) >> 3];
inline _buf_t &_get_buf(uint64_t index) {
return reinterpret_cast<_buf_t *>(_buf)[index >> _shift];
};
public:
inline uint64_t get(uint64_t index) {
return _get_buf(index)._0;
}
inline void set(uint64_t index, uint64_t val) {
_get_buf(index)._0 = val;
}
};
// 100
template <uint64_t bits, uint64_t size>
class impl_bitfield < bits,
size, typename std::enable_if<!(bits & 3) && (bits & 4) && bits<64>::type> {
protected:
constexpr static uint64_t _shift = 1;
constexpr static uint64_t _mask = (1 << _shift) - 1;
struct _buf_t {
uint64_t _0 : bits;
uint64_t _1 : bits;
} __attribute__((__packed__));
uint8_t _buf[(size * bits + 7) >> 3];
inline _buf_t &_get_buf(uint64_t index) {
return reinterpret_cast<_buf_t *>(_buf)[index >> _shift];
};
public:
inline uint64_t get(uint64_t index) {
switch (index & _mask) {
case 0:
return _get_buf(index)._0;
case 1:
return _get_buf(index)._1;
}
return 0;
}
inline void set(uint64_t index, uint64_t val) {
switch (index & _mask) {
case 0:
_get_buf(index)._0 = val;
case 1:
_get_buf(index)._1 = val;
}
}
};
// X10
template <uint64_t bits, uint64_t size>
class impl_bitfield < bits,
size, typename std::enable_if<!(bits & 1) && (bits & 2) && bits<64>::type> {
protected:
constexpr static uint64_t _shift = 2;
constexpr static uint64_t _mask = (1 << _shift) - 1;
struct _buf_t {
uint64_t _0 : bits;
uint64_t _1 : bits;
uint64_t _2 : bits;
uint64_t _3 : bits;
} __attribute__((__packed__));
uint8_t _buf[(size * bits + 7) >> 3];
inline _buf_t &_get_buf(uint64_t index) {
return reinterpret_cast<_buf_t *>(_buf)[index >> _shift];
};
public:
inline uint64_t get(uint64_t index) {
switch (index & _mask) {
case 0:
return _get_buf(index)._0;
case 1:
return _get_buf(index)._1;
case 2:
return _get_buf(index)._2;
case 3:
return _get_buf(index)._3;
}
return 0;
}
inline void set(uint64_t index, uint64_t val) {
switch (index & _mask) {
case 0:
_get_buf(index)._0 = val;
case 1:
_get_buf(index)._1 = val;
case 2:
_get_buf(index)._2 = val;
case 3:
_get_buf(index)._3 = val;
}
}
};
// XX1
template <uint64_t bits, uint64_t size>
class impl_bitfield < bits,
size, typename std::enable_if<(bits & 1) && bits<64>::type> {
protected:
constexpr static uint64_t _shift = 3;
constexpr static uint64_t _mask = (1 << _shift) - 1;
struct _buf_t {
uint64_t _0 : bits;
uint64_t _1 : bits;
uint64_t _2 : bits;
uint64_t _3 : bits;
uint64_t _4 : bits;
uint64_t _5 : bits;
uint64_t _6 : bits;
uint64_t _7 : bits;
} __attribute__((__packed__));
uint8_t _buf[(size * bits + 7) >> 3];
inline _buf_t &_get_buf(uint64_t index) {
return reinterpret_cast<_buf_t *>(_buf)[index >> _shift];
};
public:
inline uint64_t get(uint64_t index) {
switch (index & _mask) {
case 0:
return _get_buf(index)._0;
case 1:
return _get_buf(index)._1;
case 2:
return _get_buf(index)._2;
case 3:
return _get_buf(index)._3;
case 4:
return _get_buf(index)._4;
case 5:
return _get_buf(index)._5;
case 6:
return _get_buf(index)._6;
case 7:
return _get_buf(index)._7;
}
return 0;
}
inline void set(uint64_t index, uint64_t val) {
switch (index & _mask) {
case 0:
_get_buf(index)._0 = val;
case 1:
_get_buf(index)._1 = val;
case 2:
_get_buf(index)._2 = val;
case 3:
_get_buf(index)._3 = val;
case 4:
_get_buf(index)._4 = val;
case 5:
_get_buf(index)._5 = val;
case 6:
_get_buf(index)._6 = val;
case 7:
_get_buf(index)._7 = val;
}
}
};
// bitwise operations based implementation
template <uint64_t bits, uint64_t size, class Enable = void>
class impl_bitwise;
template <uint64_t bits, uint64_t size>
class impl_bitwise<bits,
size, typename std::enable_if<bits <= 57>::type> {
protected:
using T = typename std::conditional<bits <= 25, typename std::conditional<bits <= 9, uint16_t, uint32_t>::type, uint64_t>::type;
constexpr static uint64_t _byte_shift = 3;
constexpr static uint64_t _byte_mask = (1 << _byte_shift) - 1;
constexpr static T _mask = (1 << bits) - 1;
uint8_t _buf[(size * bits + _byte_mask) >> _byte_shift];
inline T &_get_buf(uint64_t index) {
return *reinterpret_cast<T *>(_buf + (index * bits >> _byte_shift));
};
inline T _get_shift(uint64_t index) {
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
return index * bits & _byte_mask;
#else
return (sizeof(T) << _byte_shift) - bits - (index * bits & _byte_mask);
#endif
};
public:
inline uint64_t get(uint64_t index) {
return (_get_buf(index) >> _get_shift(index)) & _mask;
}
inline void set(uint64_t index, uint64_t val) {
T &buf = _get_buf(index);
T shift = _get_shift(index);
buf &= ~(_mask << shift);
buf |= (val & _mask) << shift;
}
};
// bit_array interface
template <uint64_t bits, uint64_t size, typename impl = impl_bitfield<bits, size>>
class bit_array : public impl{
public:
class reference {
bit_array *base;
uint64_t index;
reference();
public:
reference(bit_array &b, uint64_t i) : base(&b), index(i) {}
~reference() {}
reference &operator=(uint64_t x) {
base->set(index, x);
return *this;
}
operator uint64_t() const {
return base->get(index);
}
};
reference operator[](size_t __pos) {
return reference(*this, __pos);
}
};
} // namespace compact_bit_array
#endif