-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDynamicBitSetIterators.hpp
More file actions
400 lines (350 loc) · 15.4 KB
/
DynamicBitSetIterators.hpp
File metadata and controls
400 lines (350 loc) · 15.4 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#pragma once
#include <vector>
#include <algorithm>
#include <iterator>
#include <stdexcept>
#include <utility>
#include <type_traits>
#include "BooleanBitWrapper.hpp"
namespace TwilightDream
{
class AbstractBitIteratorBase
{
public:
AbstractBitIteratorBase(size_t bit_position, size_t max_valid_bits)
:
bit_position(bit_position), max_valid_bits(max_valid_bits)
{};
virtual ~AbstractBitIteratorBase() = default;
//Iterator increments by 1
//迭代器自增
virtual void offset_up() = 0;
//Iterator decrements by 1
//迭代器自减
virtual void offset_down() = 0;
virtual bool is_read_only_end_iterator() const = 0;
virtual bool is_start_bit_position() const = 0;
virtual bool is_end_bit_position() const = 0;
//Iterator increments by n
//迭代器自增n次
virtual void advance( std::ptrdiff_t n ) = 0;
//Iterator decrements by n
//迭代器自减n次
virtual void retreat( std::ptrdiff_t n ) = 0;
protected:
//The bit position currently pointed to (max_bit_position = max_valid_bits - 1)
size_t bit_position = 0;
//Current bit container data, and then determine the boundary size
//Indicates the number of digits counted from the LSB position on the right to the MSB position on the left.
const size_t max_valid_bits = 0;
//This boolean flag, if true, means that the instantiated iterator is a read-only boundary flag, it cannot be moved bit_position, and its dereference data cannot be accessed.
bool is_read_only = false;
};
// 正向迭代器基类
class BitIteratorBaseData : public AbstractBitIteratorBase
{
public:
BitIteratorBaseData(bool is_begin_or_end_iterator, std::vector<BooleanBitWrapper>* container, size_t MVB )
: AbstractBitIteratorBase(is_begin_or_end_iterator ? 0 : MVB - 1, MVB), container_pointer(container)
{
if(container == nullptr)
throw std::logic_error("The pointer to the pointer is an invalid pointer!");
if(container->empty())
throw std::logic_error("There is no data in the container and no iteration is allowed.");
if(MVB == 0)
throw std::logic_error("The maximum valid bit count is not set correctly.");
if (is_begin_or_end_iterator)
{
// 开始位置的迭代器
data_pointer = &( (*container)[0] );
is_read_only = false;
is_after_end = false;
}
else
{
// 结束位置的迭代器
data_pointer = &( (*container)[container->size() - 1] );
is_read_only = true;
is_after_end = true;
}
}
virtual ~BitIteratorBaseData() = default;
void offset_up() override;
void offset_down() override;
bool is_read_only_end_iterator() const override;
bool is_start_bit_position() const override;
bool is_end_bit_position() const override;
void advance( std::ptrdiff_t n ) override;
void retreat( std::ptrdiff_t n ) override;
protected:
BooleanBitWrapper* data_pointer = nullptr;
std::vector<BooleanBitWrapper>* container_pointer = nullptr;
/*
Regardless of whether the instantiated iterator is read-only or not.
This boolean flag is used in the context of the current iterator's characteristics to indicate bit positions that are out of the container's range (e.g., before the start position or after the end position).
But the current bit positions we record are not actually modified.
With this flag, we can perform an extra iteration to ensure that the iterator logic is correct.
*/
bool is_after_end = false;
};
// 反向迭代器基类
class ReverseBitIteratorBaseData : public AbstractBitIteratorBase
{
public:
ReverseBitIteratorBaseData(bool is_begin_or_end_iterator, std::vector<BooleanBitWrapper>* container, size_t MVB )
: AbstractBitIteratorBase(is_begin_or_end_iterator ? MVB - 1 : 0, MVB), container_pointer(container)
{
if(container == nullptr)
throw std::logic_error("The pointer to the pointer is an invalid pointer!");
if(container->empty())
throw std::logic_error("There is no data in the container and no iteration is allowed.");
if(MVB == 0)
throw std::logic_error("The maximum valid bit count is not set correctly.");
if (is_begin_or_end_iterator)
{
// 开始位置的迭代器
data_pointer = &( (*container)[container->size() - 1] );
is_read_only = false;
is_before_begin = false;
}
else
{
// 结束位置的迭代器
data_pointer = &( (*container)[0] );
is_read_only = true;
is_before_begin = true;
}
}
virtual ~ReverseBitIteratorBaseData() = default;
void offset_up() override;
void offset_down() override;
bool is_read_only_end_iterator() const override;
bool is_start_bit_position() const override;
bool is_end_bit_position() const override;
void advance( std::ptrdiff_t n ) override;
void retreat( std::ptrdiff_t n ) override;
protected:
BooleanBitWrapper* data_pointer = nullptr;
std::vector<BooleanBitWrapper>* container_pointer = nullptr;
/*
Regardless of whether the instantiated iterator is read-only or not.
This boolean flag is used in the context of the current iterator's characteristics to indicate bit positions that are out of the container's range (e.g., before the start position or after the end position).
But the current bit positions we record are not actually modified.
With this flag, we can perform an extra iteration to ensure that the iterator logic is correct.
*/
bool is_before_begin = false;
};
// 常量(不可以修改迭代器说指向的数据,但是这个迭代器可以移动自己的位置)正向迭代器基类
class ConstantBitIteratorBaseData : public AbstractBitIteratorBase
{
public:
ConstantBitIteratorBaseData(bool is_begin_or_end_iterator, const std::vector<BooleanBitWrapper>* container, size_t MVB )
: AbstractBitIteratorBase(is_begin_or_end_iterator ? 0 : MVB - 1, MVB), container_pointer(container)
{
if(container == nullptr)
throw std::logic_error("The pointer to the pointer is an invalid pointer!");
if(container->empty())
throw std::logic_error("There is no data in the container and no iteration is allowed.");
if(MVB == 0)
throw std::logic_error("The maximum valid bit count is not set correctly.");
if (is_begin_or_end_iterator)
{
// 开始位置的迭代器
data_pointer = &( (*container)[0] );
is_read_only = false;
is_after_end = false;
}
else
{
// 结束位置的迭代器
data_pointer = &( (*container)[container->size() - 1] );
is_read_only = true;
is_after_end = true;
}
}
virtual ~ConstantBitIteratorBaseData() = default;
void offset_up() override;
void offset_down() override;
bool is_read_only_end_iterator() const override;
bool is_start_bit_position() const override;
bool is_end_bit_position() const override;
void advance( std::ptrdiff_t n ) override;
void retreat( std::ptrdiff_t n ) override;
protected:
const BooleanBitWrapper* data_pointer = nullptr;
const std::vector<BooleanBitWrapper>* container_pointer = nullptr;
/*
Regardless of whether the instantiated iterator is read-only or not.
This boolean flag is used in the context of the current iterator's characteristics to indicate bit positions that are out of the container's range (e.g., before the start position or after the end position).
But the current bit positions we record are not actually modified.
With this flag, we can perform an extra iteration to ensure that the iterator logic is correct.
*/
bool is_after_end = false;
};
// 常量(不可以修改迭代器说指向的数据,但是这个迭代器可以移动自己的位置)反向迭代器基类
class ConstantReverseBitIteratorBaseData : public AbstractBitIteratorBase
{
public:
ConstantReverseBitIteratorBaseData(bool is_begin_or_end_iterator, const std::vector<BooleanBitWrapper>* container, size_t MVB )
: AbstractBitIteratorBase(is_begin_or_end_iterator ? MVB - 1 : 0, MVB), container_pointer(container)
{
if(container == nullptr)
throw std::logic_error("The pointer to the pointer is an invalid pointer!");
if(container->empty())
throw std::logic_error("There is no data in the container and no iteration is allowed.");
if(MVB == 0)
throw std::logic_error("The maximum valid bit count is not set correctly.");
if (is_begin_or_end_iterator)
{
// 开始位置的迭代器
data_pointer = &( (*container)[container->size() - 1] );
is_read_only = false;
is_before_begin = false;
}
else
{
// 结束位置的迭代器
data_pointer = &( (*container)[0] );
is_read_only = true;
is_before_begin = true;
}
}
virtual ~ConstantReverseBitIteratorBaseData() = default;
void offset_up() override;
void offset_down() override;
bool is_read_only_end_iterator() const override;
bool is_start_bit_position() const override;
bool is_end_bit_position() const override;
void advance( std::ptrdiff_t n ) override;
void retreat( std::ptrdiff_t n ) override;
protected:
const BooleanBitWrapper* data_pointer = nullptr;
const std::vector<BooleanBitWrapper>* container_pointer = nullptr;
/*
Regardless of whether the instantiated iterator is read-only or not.
This boolean flag is used in the context of the current iterator's characteristics to indicate bit positions that are out of the container's range (e.g., before the start position or after the end position).
But the current bit positions we record are not actually modified.
With this flag, we can perform an extra iteration to ensure that the iterator logic is correct.
*/
bool is_before_begin = false;
};
struct BitReference
{
uint32_t* data_pointer;
uint32_t bits_mask;
BitReference() noexcept;
BitReference(BooleanBitWrapper* wrapper_pointer, uint32_t bits_mask);
BitReference(const BitReference& other) = default;
operator bool() const noexcept;
BitReference& operator=(bool value) noexcept;
BitReference& operator=(const BitReference& other) noexcept;
BitReference& operator^=(const BitReference& other) noexcept;
BitReference& operator&=(const BitReference& other) noexcept;
BitReference& operator|=(const BitReference& other) noexcept;
bool operator~() noexcept;
bool operator==(const BitReference& other) const;
bool operator<(const BitReference& other) const;
bool operator>(const BitReference& other) const;
bool operator<=(const BitReference& other) const;
bool operator>=(const BitReference& other) const;
void flip() noexcept;
};
struct BitIterator : BitIteratorBaseData
{
using BitIteratorBaseData::BitIteratorBaseData;
using iterator_category = std::random_access_iterator_tag;
using iterator = BitIterator;
using value_type = bool;
using difference_type = std::ptrdiff_t;
using pointer = BitReference*;
using reference = BitReference;
BitReference operator*();
BitReference operator[](std::ptrdiff_t offset);
BitIterator operator+(std::ptrdiff_t offset) const;
BitIterator operator-(std::ptrdiff_t offset) const;
BitIterator& operator+=(std::ptrdiff_t offset);
BitIterator& operator-=(std::ptrdiff_t offset);
BitIterator& operator++();
BitIterator operator++(int);
BitIterator& operator--();
BitIterator operator--(int);
BitIterator& operator=(const BitIterator& other);
bool operator==(const BitIterator& other) const;
bool operator!=(const BitIterator& other) const;
bool operator<(const BitIterator& other) const;
bool operator>(const BitIterator& other) const;
bool operator<=(const BitIterator& other) const;
bool operator>=(const BitIterator& other) const;
friend std::ptrdiff_t operator-(const BitIterator& left, const BitIterator& right);
};
struct ReverseBitIterator : ReverseBitIteratorBaseData
{
using ReverseBitIteratorBaseData::ReverseBitIteratorBaseData;
BitReference operator*();
BitReference operator[](std::ptrdiff_t offset);
ReverseBitIterator operator+(std::ptrdiff_t offset) const;
ReverseBitIterator operator-(std::ptrdiff_t offset) const;
ReverseBitIterator& operator-=(std::ptrdiff_t n);
ReverseBitIterator& operator+=(std::ptrdiff_t n);
ReverseBitIterator& operator++();
ReverseBitIterator operator++(int);
ReverseBitIterator& operator--();
ReverseBitIterator operator--(int);
ReverseBitIterator& operator=(const ReverseBitIterator& other);
bool operator==(const ReverseBitIterator& other) const;
bool operator!=(const ReverseBitIterator& other) const;
bool operator<(const ReverseBitIterator& other) const;
bool operator>(const ReverseBitIterator& other) const;
bool operator<=(const ReverseBitIterator& other) const;
bool operator>=(const ReverseBitIterator& other) const;
friend std::ptrdiff_t operator-(const ReverseBitIterator& left, const ReverseBitIterator& right);
};
struct ConstantBitIterator : ConstantBitIteratorBaseData
{
using ConstantBitIteratorBaseData::ConstantBitIteratorBaseData;
using iterator_category = std::random_access_iterator_tag;
using iterator = ConstantBitIterator;
using value_type = bool;
using difference_type = std::ptrdiff_t;
using pointer = bool*;
using reference = bool;
bool operator*() const;
bool operator[](std::ptrdiff_t offset) const;
ConstantBitIterator operator+(std::ptrdiff_t offset) const;
ConstantBitIterator operator-(std::ptrdiff_t offset) const;
std::ptrdiff_t operator-(const ConstantBitIterator& other) const;
ConstantBitIterator& operator++();
ConstantBitIterator operator++(int);
ConstantBitIterator& operator--();
ConstantBitIterator operator--(int);
ConstantBitIterator& operator=(const ConstantBitIterator& other);
bool operator==(const ConstantBitIterator& other) const;
bool operator!=(const ConstantBitIterator& other) const;
bool operator<(const ConstantBitIterator& other) const;
bool operator>(const ConstantBitIterator& other) const;
bool operator<=(const ConstantBitIterator& other) const;
bool operator>=(const ConstantBitIterator& other) const;
friend std::ptrdiff_t operator-(const ConstantBitIterator& left, const ConstantBitIterator& right);
};
struct ConstantReverseBitIterator : ConstantReverseBitIteratorBaseData
{
using ConstantReverseBitIteratorBaseData::ConstantReverseBitIteratorBaseData;
bool operator*() const;
bool operator[](std::ptrdiff_t offset) const;
ConstantReverseBitIterator operator+(std::ptrdiff_t offset) const;
ConstantReverseBitIterator operator-(std::ptrdiff_t offset) const;
std::ptrdiff_t operator-(const ConstantReverseBitIterator& other) const;
ConstantReverseBitIterator& operator++();
ConstantReverseBitIterator operator++(int);
ConstantReverseBitIterator& operator--();
ConstantReverseBitIterator operator--(int);
ConstantReverseBitIterator& operator=(const ConstantReverseBitIterator& other);
bool operator==(const ConstantReverseBitIterator& other) const;
bool operator!=(const ConstantReverseBitIterator& other) const;
bool operator<(const ConstantReverseBitIterator& other) const;
bool operator>(const ConstantReverseBitIterator& other) const;
bool operator<=(const ConstantReverseBitIterator& other) const;
bool operator>=(const ConstantReverseBitIterator& other) const;
friend std::ptrdiff_t operator-(const ConstantReverseBitIterator& left, const ConstantReverseBitIterator& right);
};
} // namespace TwilightDream