-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitmap.cpp
More file actions
269 lines (227 loc) · 8.62 KB
/
Bitmap.cpp
File metadata and controls
269 lines (227 loc) · 8.62 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
#include "Bitmap.h"
#include <algorithm>
#include <mutex>
#include <stdexcept>
#include <cstdio>
#include <cstring>
namespace detail {
BitmapMutex::BitmapMutex() = default;
void BitmapMutex::lock() noexcept {
mutex_.lock();
}
void BitmapMutex::unlock() noexcept {
mutex_.unlock();
}
bool BitmapMutex::try_lock() noexcept {
return mutex_.try_lock();
}
}
#define BITS_PER_BYTE 8
using namespace std;
Bitmap::Bitmap(size_t number_of_bits, unsigned char initial_bit_value)
{
if (number_of_bits > MAX_BITS) {
throw std::invalid_argument("number_of_bits exceeds MAX_BITS");
}
number_of_bits_ = number_of_bits;
number_of_words_ = (number_of_bits / (sizeof(size_t) * BITS_PER_BYTE)) +
((number_of_bits % (sizeof(size_t) * BITS_PER_BYTE)) == 0 ? 0 : 1);
bitmap_.fill(0);
// If initial_bit_value == 1, then all bits are set initially.
size_t initial_word_value = (initial_bit_value ? ~0ULL : 0ULL);
fill(bitmap_.begin(), bitmap_.begin() + number_of_words_, initial_word_value);
// Set the running tally.
// If all bits are set initially, then count_of_ones_ = number_of_bits; otherwise 0.
if (initial_bit_value == 1) {
count_of_ones_.store(static_cast<int>(number_of_bits));
}
else {
count_of_ones_.store(0);
}
}
unsigned char Bitmap::bitValue(size_t bit_number) const {
if (bit_number >= number_of_bits_) {
throw std::out_of_range("bit_number out of range in Bitmap::bitValue");
}
size_t word_num = bit_number / (sizeof(size_t) * BITS_PER_BYTE);
size_t word_bit_number = bit_number % (sizeof(size_t) * BITS_PER_BYTE);
size_t check_bit = (static_cast<size_t>(1)) << word_bit_number;
return (bitmap_[word_num] & check_bit) ? 1 : 0;
}
bool Bitmap::isSet(size_t bit_number) const {
return (bitValue(bit_number) == 1);
}
void Bitmap::setBitTo(size_t bit_number, unsigned char new_bit_value) {
std::lock_guard<detail::BitmapMutex> guard(in_use_);
if (bit_number >= number_of_bits_) {
throw std::out_of_range("bit_number out of range in Bitmap::setBitTo");
}
// read old bit
unsigned char old_val = bitValue(bit_number);
if (old_val == new_bit_value) {
// no change, so do nothing
return;
}
// calculate location in the array
size_t word_num = bit_number / (sizeof(size_t) * BITS_PER_BYTE);
size_t word_bit_number = bit_number % (sizeof(size_t) * BITS_PER_BYTE);
size_t bit_mask = (static_cast<size_t>(1)) << word_bit_number;
// update the bit
if (new_bit_value) {
bitmap_[word_num] |= bit_mask;
count_of_ones_.fetch_add(1, std::memory_order_relaxed);
}
else {
bitmap_[word_num] &= ~bit_mask;
count_of_ones_.fetch_sub(1, std::memory_order_relaxed);
}
}
int Bitmap::getAndOptionallyClearFirstOne(bool do_clear) const {
std::lock_guard<detail::BitmapMutex> guard(in_use_);
size_t result = INVALID_BIT_NUMBER;
const size_t bits_per_word = sizeof(size_t) * BITS_PER_BYTE;
for (size_t word_num = 0; word_num < number_of_words_; ++word_num) {
size_t check_word = bitmap_[word_num];
if (check_word != 0) {
size_t base_offset = word_num * bits_per_word;
if (base_offset >= number_of_bits_) {
break;
}
size_t bits_remaining = number_of_bits_ - base_offset;
size_t bits_to_check = (std::min)(bits_per_word, bits_remaining);
for (size_t bit_num = 0; bit_num < bits_to_check; ++bit_num) {
size_t check_bit = (static_cast<size_t>(1)) << bit_num;
if (check_bit & check_word) {
result = base_offset + bit_num;
if (do_clear) {
// old_val was 1, now clearing it
const_cast<Bitmap*>(this)->bitmap_[word_num] &= (~check_bit);
const_cast<Bitmap*>(this)->count_of_ones_.fetch_sub(1, std::memory_order_relaxed);
}
break;
}
}
}
if (result != INVALID_BIT_NUMBER) {
break;
}
}
return (result == INVALID_BIT_NUMBER ? -1 : static_cast<int>(result));
}
int Bitmap::getFirstOne() const {
return getAndOptionallyClearFirstOne(false);
}
int Bitmap::getAndClearFirstOne() {
return const_cast<Bitmap*>(this)->getAndOptionallyClearFirstOne(true);
}
int Bitmap::getAndOptionallySetFirstZero(bool do_set) {
std::lock_guard<detail::BitmapMutex> guard(in_use_);
size_t result = INVALID_BIT_NUMBER;
const size_t bits_per_word = sizeof(size_t) * BITS_PER_BYTE;
for (size_t word_num = 0; word_num < number_of_words_; ++word_num) {
size_t check_word = bitmap_[word_num];
if (check_word != (size_t)~0) {
size_t base_offset = word_num * bits_per_word;
if (base_offset >= number_of_bits_) {
break;
}
size_t bits_remaining = number_of_bits_ - base_offset;
size_t bits_to_check = (std::min)(bits_per_word, bits_remaining);
for (size_t bit_num = 0; bit_num < bits_to_check; ++bit_num) {
size_t check_bit = (static_cast<size_t>(1)) << bit_num;
// check if bit is 0
if ((check_bit & check_word) == 0) {
result = base_offset + bit_num;
if (do_set) {
// old_val was 0, now setting it
bitmap_[word_num] |= check_bit;
count_of_ones_.fetch_add(1, std::memory_order_relaxed);
}
break;
}
}
}
if (result != INVALID_BIT_NUMBER) {
break;
}
}
return (result == INVALID_BIT_NUMBER ? -1 : static_cast<int>(result));
}
int Bitmap::getFirstZero() const {
return const_cast<Bitmap*>(this)->getAndOptionallySetFirstZero(false);
}
int Bitmap::getAndSetFirstZero() {
return getAndOptionallySetFirstZero(true);
}
unsigned char Bitmap::testAndSet(size_t bit_number) {
std::lock_guard<detail::BitmapMutex> guard(in_use_);
unsigned char old_val = bitValue(bit_number);
if (old_val == 0) {
setBitTo(bit_number, 1);
}
return old_val;
}
unsigned char Bitmap::testAndClear(size_t bit_number) {
std::lock_guard<detail::BitmapMutex> guard(in_use_);
unsigned char old_val = bitValue(bit_number);
if (old_val == 1) {
setBitTo(bit_number, 0);
}
return old_val;
}
int Bitmap::countOnes() const {
return count_of_ones_.load(std::memory_order_relaxed);
}
int Bitmap::countZeroes() const {
return static_cast<int>(number_of_bits_) - countOnes();
}
std::string Bitmap::asHexString() const {
std::lock_guard<detail::BitmapMutex> guard(in_use_);
std::string result;
#if defined(_WIN64) || defined(__x86_64__) || defined(__ppc64__)
// 64-bit: 16 hex digits.
const char* fmt = "%016zx";
#else
// 32-bit: 8 hex digits.
const char* fmt = "%08zx";
#endif
for (size_t i = 0; i < number_of_words_; ++i) {
char buf[32];
// Use sprintf_s with the fixed format.
sprintf_s(buf, sizeof(buf), fmt, bitmap_[i]);
result.insert(0, buf);
}
return result;
}
std::string Bitmap::asBinaryString() const {
std::lock_guard<detail::BitmapMutex> guard(in_use_);
if (number_of_bits_ > 1000) {
return "(too many bits for binary string)";
}
// pre-allocate the string to avoid repeated reallocations
std::string result;
result.reserve(number_of_bits_);
const size_t bits_per_word = sizeof(size_t) * 8;
// we iterate from the highest word down to the lowest word
// so that the leftmost character in 'result' corresponds to the highest bit index.
for (size_t w = number_of_words_; w > 0; ) {
--w; // move from last word to first
size_t base_offset = w * bits_per_word;
// if this word goes beyond the total bits, skip the unused portion
if (base_offset >= number_of_bits_) {
continue;
}
// determine how many bits in this word are actually in range
size_t bits_remaining = number_of_bits_ - base_offset;
size_t bits_to_check = (std::min)(bits_per_word, bits_remaining);
size_t word_val = bitmap_[w];
// within this word, iterate from the highest bit down to 0
// so bit (bits_to_check - 1) is placed first.
for (size_t bit_num = bits_to_check; bit_num > 0; ) {
--bit_num;
bool bit_is_set = ((word_val >> bit_num) & 1ULL) != 0ULL;
result.push_back(bit_is_set ? '1' : '0');
}
}
return result;
}