forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitArray.I
More file actions
374 lines (338 loc) · 7.8 KB
/
bitArray.I
File metadata and controls
374 lines (338 loc) · 7.8 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
/**
* PANDA 3D SOFTWARE
* Copyright (c) Carnegie Mellon University. All rights reserved.
*
* All use of this software is subject to the terms of the revised BSD
* license. You should have received a copy of this license along
* with this source code in a file named "LICENSE."
*
* @file bitArray.I
* @author drose
* @date 2006-01-20
*/
/**
*
*/
INLINE BitArray::
BitArray() {
_highest_bits = 0;
}
/**
*
*/
INLINE BitArray::
BitArray(WordType init_value) {
if (init_value != 0) {
_array.push_back(MaskType(init_value));
}
_highest_bits = 0;
}
/**
* Returns a BitArray with an infinite array of bits, all on.
*/
INLINE BitArray BitArray::
all_on() {
BitArray result;
result._highest_bits = 1;
return result;
}
/**
* Returns a BitArray whose bits are all off.
*/
INLINE BitArray BitArray::
all_off() {
return BitArray();
}
/**
* Returns a BitArray whose lower on_bits bits are on.
*/
INLINE BitArray BitArray::
lower_on(int on_bits) {
BitArray result;
result.set_range(0, on_bits);
return result;
}
/**
* Returns a BitArray with only the indicated bit on.
*/
INLINE BitArray BitArray::
bit(int index) {
BitArray result;
result.set_bit(index);
return result;
}
/**
* Returns a BitArray whose size bits, beginning at low_bit, are on.
*/
INLINE BitArray BitArray::
range(int low_bit, int size) {
BitArray result;
result.set_range(low_bit, size);
return result;
}
/**
* Returns the current number of possibly different bits in this array. There
* are actually an infinite number of bits, but every bit higher than this bit
* will have the same value, either 0 or 1 (see get_highest_bits()).
*
* This number may grow and/or shrink automatically as needed.
*/
INLINE size_t BitArray::
get_num_bits() const {
return get_num_words() * (size_t)num_bits_per_word;
}
/**
* Returns true if the nth bit is set, false if it is cleared. It is valid
* for n to increase beyond get_num_bits(), but the return value
* get_num_bits() will always be the same.
*/
INLINE bool BitArray::
get_bit(int index) const {
nassertr(index >= 0, false);
int w = index / num_bits_per_word;
int b = index % num_bits_per_word;
if ((size_t)w >= get_num_words()) {
return get_highest_bits();
} else {
return (_array[w].get_bit(b));
}
}
/**
* Sets the nth bit on. If n >= get_num_bits(), this automatically extends
* the array.
*/
INLINE void BitArray::
set_bit(int index) {
nassertv(index >= 0);
int w = index / num_bits_per_word;
int b = index % num_bits_per_word;
if ((size_t)w >= get_num_words() && _highest_bits) {
// All the highest bits are already on.
return;
}
ensure_has_word(w);
_array[w].set_bit(b);
normalize();
}
/**
* Sets the nth bit off. If n >= get_num_bits(), this automatically extends
* the array.
*/
INLINE void BitArray::
clear_bit(int index) {
nassertv(index >= 0);
int w = index / num_bits_per_word;
int b = index % num_bits_per_word;
if ((size_t)w >= get_num_words() && !_highest_bits) {
// All the highest bits are already off.
return;
}
ensure_has_word(w);
_array[w].clear_bit(b);
normalize();
}
/**
* Sets the nth bit either on or off, according to the indicated bool value.
*/
INLINE void BitArray::
set_bit_to(int index, bool value) {
if (value) {
set_bit(index);
} else {
clear_bit(index);
}
}
/**
* Returns true if the infinite set of bits beyond get_num_bits() are all on,
* or false of they are all off.
*/
INLINE bool BitArray::
get_highest_bits() const {
return (_highest_bits != 0);
}
/**
* Returns a word that represents only the indicated range of bits within this
* BitArray, shifted to the least-significant position. size must be <=
* get_num_bits_per_word().
*/
INLINE BitArray::WordType BitArray::
extract(int low_bit, int size) const {
nassertr(size >= 0 && size <= num_bits_per_word, 0);
int w = low_bit / num_bits_per_word;
int b = low_bit % num_bits_per_word;
if (b + size < num_bits_per_word) {
// The whole thing fits within one word of the array.
return get_word(w).extract(b, size);
} else {
// We have to split it across two words.
int num_lower_bits = num_bits_per_word - b;
int num_higher_bits = size - num_lower_bits;
return get_word(w).extract(b, num_lower_bits) |
(get_word(w + 1).extract(0, num_higher_bits) << num_lower_bits);
}
}
/**
* Stores the indicated word into the indicated range of bits with this
* BitArray.
*/
INLINE void BitArray::
store(WordType value, int low_bit, int size) {
nassertv(size >= 0);
int w = low_bit / num_bits_per_word;
int b = low_bit % num_bits_per_word;
if (b + size < num_bits_per_word) {
// The whole thing fits within one word of the array.
ensure_has_word(w);
_array[w].store(value, b, size);
} else {
// We have to split it across two words.
int num_lower_bits = num_bits_per_word - b;
int num_higher_bits = size - num_lower_bits;
ensure_has_word(w + 1);
_array[w].store(value, b, num_lower_bits);
_array[w + 1].store(value >> num_lower_bits, 0, num_higher_bits);
}
normalize();
}
/**
* Sets the indicated range of bits to either on or off.
*/
INLINE void BitArray::
set_range_to(bool value, int low_bit, int size) {
if (value) {
set_range(low_bit, size);
} else {
clear_range(low_bit, size);
}
}
/**
* Returns the number of possibly-unique words stored in the array.
*/
INLINE size_t BitArray::
get_num_words() const {
return _array.size();
}
/**
* Returns the nth word in the array. It is valid for n to be greater than
* get_num_words(), but the return value beyond get_num_words() will always be
* the same.
*/
INLINE BitArray::MaskType BitArray::
get_word(size_t n) const {
nassertr(n >= 0, MaskType::all_off());
if (n < get_num_words()) {
return _array[n];
}
if (_highest_bits) {
return MaskType::all_on();
} else {
return MaskType::all_off();
}
}
/**
* Replaces the nth word in the array. If n >= get_num_words(), this
* automatically extends the array.
*/
INLINE void BitArray::
set_word(size_t n, WordType value) {
ensure_has_word(n);
_array[n] = value;
normalize();
}
/**
* Sets all the bits in the BitArray off.
*/
void BitArray::
clear() {
_array.clear();
_highest_bits = 0;
}
/**
*
*/
INLINE bool BitArray::
operator == (const BitArray &other) const {
return compare_to(other) == 0;
}
/**
*
*/
INLINE bool BitArray::
operator != (const BitArray &other) const {
return compare_to(other) != 0;
}
/**
* Returns true if the unsigned integer which is represented by this BitArray
* is less than that of the other one, false otherwise.
*/
INLINE bool BitArray::
operator < (const BitArray &other) const {
return compare_to(other) < 0;
}
/**
*
*/
INLINE BitArray BitArray::
operator & (const BitArray &other) const {
BitArray result(*this);
result &= other;
return result;
}
/**
*
*/
INLINE BitArray BitArray::
operator | (const BitArray &other) const {
BitArray result(*this);
result |= other;
return result;
}
/**
*
*/
INLINE BitArray BitArray::
operator ^ (const BitArray &other) const {
BitArray result(*this);
result ^= other;
return result;
}
/**
*
*/
INLINE BitArray BitArray::
operator ~ () const {
BitArray result(*this);
result.invert_in_place();
return result;
}
/**
*
*/
INLINE BitArray BitArray::
operator << (int shift) const {
BitArray result(*this);
result <<= shift;
return result;
}
/**
*
*/
INLINE BitArray BitArray::
operator >> (int shift) const {
BitArray result(*this);
result >>= shift;
return result;
}
/**
* Called internally just before writing to the _array member, this makes a
* new copy of _array if it appears to be shared with any other objects--thus
* achieving copy-on-write.
*/
INLINE void BitArray::
copy_on_write() {
if (_array.get_ref_count() > 1) {
PTA(MaskType) new_array;
new_array.v() = _array.v();
_array = new_array;
}
}