forked from chen3feng/toft
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitmap.cpp
More file actions
264 lines (220 loc) · 7.02 KB
/
Copy pathbitmap.cpp
File metadata and controls
264 lines (220 loc) · 7.02 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
// Copyright (c) 2011, The Toft Authors.
// All rights reserved.
//
// Author: CHEN Feng <[email protected]>
#include "toft/container/bitmap.h"
#include <algorithm>
namespace toft {
namespace details {
void BitmapBase::DoAndWith(WordType* words, const WordType* words2, size_t word_size)
{
for (size_t i = 0; i < word_size; ++i)
words[i] &= words2[i];
}
void BitmapBase::DoOrWith(WordType* words, const WordType* words2, size_t word_size)
{
for (size_t i = 0; i < word_size; ++i)
words[i] |= words2[i];
}
void BitmapBase::DoXorWith(WordType* words, const WordType* words2, size_t word_size)
{
for (size_t i = 0; i < word_size; ++i)
words[i] ^= words2[i];
}
void BitmapBase::DoSetAll(WordType* words, uint64_t size)
{
size_t word_size = WordSizeOfBits(size);
memset(words, 0xFF, word_size * sizeof(WordType));
MaskOffTailBits(words, size);
}
bool BitmapBase::DoAllAreSet(const WordType* words, uint64_t num_bits)
{
const size_t last_word_index = num_bits / kBitsPerWord;
const size_t tail_bits = num_bits % kBitsPerWord;
for (size_t i = 0; i < last_word_index; ++i)
{
if (words[i] != kAllSetMask)
return false;
}
if (tail_bits > 0)
{
WordType tail_word = words[last_word_index];
WordType mask = (1ULL << tail_bits) - 1;
if ((tail_word & mask) != mask)
return false;
}
return true;
}
bool BitmapBase::DoAllAreClear(const WordType* words, uint64_t num_bits)
{
const size_t last_word_index = num_bits / kBitsPerWord;
const size_t tail_bits = num_bits % kBitsPerWord;
for (size_t i = 0; i < last_word_index; ++i)
{
if (words[i] != 0)
return false;
}
if (tail_bits > 0)
{
WordType tail_word = words[last_word_index];
if ((tail_word >> tail_bits << tail_bits) != tail_word)
return false;
}
return true;
}
bool BitmapBase::DoAllAreClearInRange(const WordType* words, size_t start, size_t end)
{
size_t start_word_index = start / kBitsPerWord;
size_t start_tail_bits = start % kBitsPerWord;
size_t end_word_index = end / kBitsPerWord;
size_t end_tail_bits = end % kBitsPerWord;
// span different words
if (start_word_index != end_word_index)
{
// check header bits
if (start_tail_bits > 0)
{
if ((words[start_word_index] >> start_tail_bits) != 0)
return false;
// skip these tested bits
++start_word_index;
}
// check middle part, can checked be word by word because all the bits of
// these words are in the range
for (size_t i = start_word_index; i < end_word_index; ++i)
{
if (words[i] != 0)
return false;
}
// check tail bits
if (end_tail_bits > 0)
{
WordType mask = ~WordType((1ULL << end_tail_bits) - 1);
if ((words[end_word_index] | mask) != mask)
return false;
}
}
else // in same words
{
WordType word = words[start_word_index];
return ((word >> start_tail_bits) << (kBitsPerWord - end_tail_bits + start_tail_bits)) == 0;
}
return true;
}
bool BitmapBase::DoIsSubsetOf(
const WordType* words,
const WordType* fullset_words,
uint64_t num_bits)
{
const size_t last_word_index = num_bits / kBitsPerWord;
const size_t tail_bits = num_bits % kBitsPerWord;
for (size_t i = 0; i < last_word_index; ++i)
{
if ((words[i] | fullset_words[i]) != fullset_words[i])
return false;
}
if (tail_bits > 0)
{
WordType mask = (1ULL << tail_bits) - 1;
WordType tail_word = words[last_word_index] & mask;
WordType fullset_tail_word = fullset_words[last_word_index] & mask;
if ((tail_word | fullset_tail_word) != fullset_tail_word)
return false;
}
return true;
}
void BitmapBase::DoLeftShift(WordType* words, uint64_t num_bits, size_t shift)
{
const size_t last_word_index = shift / kBitsPerWord;
const size_t tail_bits = shift % kBitsPerWord;
const size_t word_size = WordSizeOfBits(num_bits);
if (tail_bits == 0)
{
for (size_t n = word_size - 1; n >= last_word_index; --n)
words[n] = words[n - last_word_index];
}
else
{
const size_t sub_offset = kBitsPerWord - tail_bits;
for (size_t n = word_size - 1; n > last_word_index; --n)
words[n] = ((words[n - last_word_index] << tail_bits)
| (words[n - last_word_index - 1] >> sub_offset));
words[last_word_index] = words[0] << tail_bits;
}
std::fill(words, words + last_word_index, static_cast<WordType>(0));
MaskOffTailBits(words, num_bits);
}
void BitmapBase::DoRightShift(WordType* words, uint64_t word_size, size_t shift)
{
const size_t last_word_index = shift / kBitsPerWord;
const size_t tail_bits = shift % kBitsPerWord;
const size_t limit = word_size - last_word_index - 1;
if (tail_bits == 0)
{
for (size_t n = 0; n <= limit; ++n)
words[n] = words[n + last_word_index];
}
else
{
const size_t sub_offset = kBitsPerWord - tail_bits;
for (size_t n = 0; n < limit; ++n)
words[n] = ((words[n + last_word_index] >> tail_bits)
| (words[n + last_word_index + 1] << sub_offset));
words[limit] = words[word_size-1] >> tail_bits;
}
std::fill(words + limit + 1, words + word_size, static_cast<WordType>(0));
}
bool BitmapBase::DoFindFirst(WordType* words, size_t word_size, size_t* result)
{
for (size_t i = 0; i < word_size; i++)
{
WordType thisword = words[i];
if (thisword != static_cast<WordType>(0))
{
*result = (i * kBitsPerWord
+ __builtin_ctzl(thisword));
return true;
}
}
return false;
}
bool BitmapBase::DoFindNext(WordType* words, size_t word_size, size_t prev, size_t* result)
{
// make bound inclusive
++prev;
// check out of bounds
if (prev >= word_size * kBitsPerWord)
return false;
// search first word
size_t i = WordIndexOfBit(prev);
WordType thisword = words[i];
// mask off bits below bound
thisword &= kAllSetMask << ShiftOfBit(prev);
if (thisword != static_cast<WordType>(0))
{
*result = (i * kBitsPerWord
+ __builtin_ctzl(thisword));
return true;
}
// check subsequent words
i++;
for (; i < word_size; i++)
{
thisword = words[i];
if (thisword != static_cast<WordType>(0))
{
*result = (i * kBitsPerWord
+ __builtin_ctzl(thisword));
return true;
}
}
return false;
}
void BitmapBase::DoAppendToString(const WordType* words, uint64_t num_bits, std::string* out)
{
out->reserve(out->size() + num_bits);
for (uint64_t i = num_bits; i > 0; --i)
out->push_back(DoGetAt(words, i - 1) ? '1' : '0');
}
} // namespace details
} // namespace toft