00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 INLINE BitArray::
00022 BitArray() {
00023 _highest_bits = 0;
00024 }
00025
00026
00027
00028
00029
00030
00031 INLINE BitArray::
00032 BitArray(WordType init_value) {
00033 if (init_value != 0) {
00034 _array.push_back(MaskType(init_value));
00035 }
00036 _highest_bits = 0;
00037 }
00038
00039
00040
00041
00042
00043
00044 INLINE BitArray::
00045 BitArray(const BitArray ©) :
00046 _array(copy._array),
00047 _highest_bits(copy._highest_bits)
00048 {
00049 }
00050
00051
00052
00053
00054
00055
00056 INLINE BitArray &BitArray::
00057 operator = (const BitArray ©) {
00058 _array = copy._array;
00059 _highest_bits = copy._highest_bits;
00060 return *this;
00061 }
00062
00063
00064
00065
00066
00067
00068
00069 INLINE BitArray BitArray::
00070 all_on() {
00071 BitArray result;
00072 result._highest_bits = 1;
00073 return result;
00074 }
00075
00076
00077
00078
00079
00080
00081 INLINE BitArray BitArray::
00082 all_off() {
00083 return BitArray();
00084 }
00085
00086
00087
00088
00089
00090
00091 INLINE BitArray BitArray::
00092 lower_on(int on_bits) {
00093 BitArray result;
00094 result.set_range(0, on_bits);
00095 return result;
00096 }
00097
00098
00099
00100
00101
00102
00103 INLINE BitArray BitArray::
00104 bit(int index) {
00105 BitArray result;
00106 result.set_bit(index);
00107 return result;
00108 }
00109
00110
00111
00112
00113
00114
00115
00116 INLINE BitArray BitArray::
00117 range(int low_bit, int size) {
00118 BitArray result;
00119 result.set_range(low_bit, size);
00120 return result;
00121 }
00122
00123
00124
00125
00126
00127
00128 INLINE BitArray::
00129 ~BitArray() {
00130 }
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 INLINE bool BitArray::
00146 has_max_num_bits() {
00147 return false;
00148 }
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163 INLINE int BitArray::
00164 get_max_num_bits() {
00165 nassertr(false, 0);
00166 return 0;
00167 }
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177 INLINE int BitArray::
00178 get_num_bits_per_word() {
00179 return num_bits_per_word;
00180 }
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194 INLINE int BitArray::
00195 get_num_bits() const {
00196 return get_num_words() * num_bits_per_word;
00197 }
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207 INLINE bool BitArray::
00208 get_bit(int index) const {
00209 nassertr(index >= 0, false);
00210 int w = index / num_bits_per_word;
00211 int b = index % num_bits_per_word;
00212 if (w >= get_num_words()) {
00213 return get_highest_bits();
00214 } else {
00215 return (_array[w].get_bit(b));
00216 }
00217 }
00218
00219
00220
00221
00222
00223
00224
00225 INLINE void BitArray::
00226 set_bit(int index) {
00227 nassertv(index >= 0);
00228 int w = index / num_bits_per_word;
00229 int b = index % num_bits_per_word;
00230 if (w >= get_num_words() && _highest_bits) {
00231
00232 return;
00233 }
00234 ensure_has_word(w);
00235 _array[w].set_bit(b);
00236 normalize();
00237 }
00238
00239
00240
00241
00242
00243
00244
00245 INLINE void BitArray::
00246 clear_bit(int index) {
00247 nassertv(index >= 0);
00248 int w = index / num_bits_per_word;
00249 int b = index % num_bits_per_word;
00250 if (w >= get_num_words() && !_highest_bits) {
00251
00252 return;
00253 }
00254 ensure_has_word(w);
00255 _array[w].clear_bit(b);
00256 normalize();
00257 }
00258
00259
00260
00261
00262
00263
00264
00265 INLINE void BitArray::
00266 set_bit_to(int index, bool value) {
00267 if (value) {
00268 set_bit(index);
00269 } else {
00270 clear_bit(index);
00271 }
00272 }
00273
00274
00275
00276
00277
00278
00279
00280
00281 INLINE bool BitArray::
00282 get_highest_bits() const {
00283 return (_highest_bits != 0);
00284 }
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294 INLINE BitArray::WordType BitArray::
00295 extract(int low_bit, int size) const {
00296 nassertr(size >= 0 && size <= num_bits_per_word, 0);
00297 int w = low_bit / num_bits_per_word;
00298 int b = low_bit % num_bits_per_word;
00299
00300 if (b + size < num_bits_per_word) {
00301
00302 return get_word(w).extract(b, size);
00303
00304 } else {
00305
00306 int num_lower_bits = num_bits_per_word - b;
00307 int num_higher_bits = size - num_lower_bits;
00308
00309 return get_word(w).extract(b, num_lower_bits) |
00310 (get_word(w + 1).extract(0, num_higher_bits) << num_lower_bits);
00311 }
00312 }
00313
00314
00315
00316
00317
00318
00319
00320 INLINE void BitArray::
00321 store(WordType value, int low_bit, int size) {
00322 nassertv(size >= 0);
00323 int w = low_bit / num_bits_per_word;
00324 int b = low_bit % num_bits_per_word;
00325
00326 if (b + size < num_bits_per_word) {
00327
00328 ensure_has_word(w);
00329 _array[w].store(value, b, size);
00330
00331 } else {
00332
00333 int num_lower_bits = num_bits_per_word - b;
00334 int num_higher_bits = size - num_lower_bits;
00335
00336 ensure_has_word(w + 1);
00337 _array[w].store(value, b, num_lower_bits);
00338 _array[w + 1].store(value >> num_lower_bits, 0, num_higher_bits);
00339 }
00340 normalize();
00341 }
00342
00343
00344
00345
00346
00347
00348 INLINE void BitArray::
00349 set_range_to(bool value, int low_bit, int size) {
00350 if (value) {
00351 set_range(low_bit, size);
00352 } else {
00353 clear_range(low_bit, size);
00354 }
00355 }
00356
00357
00358
00359
00360
00361
00362
00363 INLINE int BitArray::
00364 get_num_words() const {
00365 return _array.size();
00366 }
00367
00368
00369
00370
00371
00372
00373
00374
00375 INLINE BitArray::MaskType BitArray::
00376 get_word(int n) const {
00377 nassertr(n >= 0, MaskType::all_off());
00378 if (n < get_num_words()) {
00379 return _array[n];
00380 }
00381 if (_highest_bits) {
00382 return MaskType::all_on();
00383 } else {
00384 return MaskType::all_off();
00385 }
00386 }
00387
00388
00389
00390
00391
00392
00393
00394
00395 INLINE void BitArray::
00396 set_word(int n, MaskType value) {
00397 nassertv(n >= 0);
00398 ensure_has_word(n);
00399 _array[n] = value;
00400 normalize();
00401 }
00402
00403
00404
00405
00406
00407
00408 void BitArray::
00409 clear() {
00410 _array.clear();
00411 _highest_bits = 0;
00412 }
00413
00414
00415
00416
00417
00418
00419 INLINE bool BitArray::
00420 operator == (const BitArray &other) const {
00421 return compare_to(other) == 0;
00422 }
00423
00424
00425
00426
00427
00428
00429 INLINE bool BitArray::
00430 operator != (const BitArray &other) const {
00431 return compare_to(other) != 0;
00432 }
00433
00434
00435
00436
00437
00438
00439
00440
00441 INLINE bool BitArray::
00442 operator < (const BitArray &other) const {
00443 return compare_to(other) < 0;
00444 }
00445
00446
00447
00448
00449
00450
00451 INLINE BitArray BitArray::
00452 operator & (const BitArray &other) const {
00453 BitArray result(*this);
00454 result &= other;
00455 return result;
00456 }
00457
00458
00459
00460
00461
00462
00463 INLINE BitArray BitArray::
00464 operator | (const BitArray &other) const {
00465 BitArray result(*this);
00466 result |= other;
00467 return result;
00468 }
00469
00470
00471
00472
00473
00474
00475 INLINE BitArray BitArray::
00476 operator ^ (const BitArray &other) const {
00477 BitArray result(*this);
00478 result ^= other;
00479 return result;
00480 }
00481
00482
00483
00484
00485
00486
00487 INLINE BitArray BitArray::
00488 operator ~ () const {
00489 BitArray result(*this);
00490 result.invert_in_place();
00491 return result;
00492 }
00493
00494
00495
00496
00497
00498
00499 INLINE BitArray BitArray::
00500 operator << (int shift) const {
00501 BitArray result(*this);
00502 result <<= shift;
00503 return result;
00504 }
00505
00506
00507
00508
00509
00510
00511 INLINE BitArray BitArray::
00512 operator >> (int shift) const {
00513 BitArray result(*this);
00514 result >>= shift;
00515 return result;
00516 }
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526 INLINE void BitArray::
00527 copy_on_write() {
00528 if (_array.get_ref_count() > 1) {
00529 PTA(MaskType) new_array;
00530 new_array.v() = _array.v();
00531 _array = new_array;
00532 }
00533 }