forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitArray.cxx
More file actions
931 lines (824 loc) · 23 KB
/
bitArray.cxx
File metadata and controls
931 lines (824 loc) · 23 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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
/**
* 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.cxx
* @author drose
* @date 2006-01-20
*/
#include "bitArray.h"
#include "sparseArray.h"
#include "datagram.h"
#include "datagramIterator.h"
using std::max;
using std::min;
using std::ostream;
TypeHandle BitArray::_type_handle;
/**
*
*/
BitArray::
BitArray(const SparseArray &from) {
_highest_bits = 0;
int num_subranges = from.get_num_subranges();
for (int i = 0; i < num_subranges; ++i) {
int begin = from.get_subrange_begin(i);
int end = from.get_subrange_end(i);
set_range(begin, end - begin);
}
if (from.is_inverse()) {
invert_in_place();
}
}
/**
* Returns true if the entire bitmask is zero, false otherwise.
*/
bool BitArray::
is_zero() const {
if (_highest_bits) {
// If all the infinite highest bits are set, certainly the bitmask is
// nonzero.
return false;
}
// Start from the high end, since that's more likely to be nonzero.
Array::reverse_iterator ai;
for (ai = _array.rbegin(); ai != _array.rend(); ++ai) {
if (!(*ai).is_zero()) {
return false;
}
}
return true;
}
/**
* Returns true if the entire bitmask is one, false otherwise.
*/
bool BitArray::
is_all_on() const {
if (!_highest_bits) {
// If all the infinite highest bits are not set, certainly the bitmask is
// not all on.
return false;
}
Array::reverse_iterator ai;
for (ai = _array.rbegin(); ai != _array.rend(); ++ai) {
if (!(*ai).is_all_on()) {
return false;
}
}
return true;
}
/**
* Returns true if any bit in the indicated range is set, false otherwise.
*/
bool BitArray::
has_any_of(int low_bit, int size) const {
if ((size_t)(low_bit + size) > get_num_bits()) {
// This range touches the highest bits.
if (_highest_bits) {
return true;
}
}
int w = low_bit / num_bits_per_word;
int b = low_bit % num_bits_per_word;
if (w >= (int)get_num_words()) {
// This range is entirely among the highest bits.
return (_highest_bits != 0);
}
if (b + size <= num_bits_per_word) {
// The whole thing fits within one word of the array.
return get_word(w).has_any_of(b, size);
}
int num_high_bits = num_bits_per_word - b;
if (_array[w].has_any_of(b, num_high_bits)) {
return true;
}
size -= num_high_bits;
++w;
while (size > 0) {
if ((size_t)w >= get_num_words()) {
// Now we're up to the highest bits.
return (_highest_bits != 0);
}
if (size <= num_bits_per_word) {
// The remainder fits within one word of the array.
return _array[w].has_any_of(0, size);
}
// Keep going.
if (!_array[w].is_zero()) {
return true;
}
size -= num_bits_per_word;
++w;
}
return false;
}
/**
* Returns true if all bits in the indicated range are set, false otherwise.
*/
bool BitArray::
has_all_of(int low_bit, int size) const {
if ((size_t)(low_bit + size) > get_num_bits()) {
// This range touches the highest bits.
if (!_highest_bits) {
return false;
}
}
int w = low_bit / num_bits_per_word;
int b = low_bit % num_bits_per_word;
if (w >= (int)get_num_words()) {
// This range is entirely among the highest bits.
return (_highest_bits != 0);
}
if (b + size <= num_bits_per_word) {
// The whole thing fits within one word of the array.
return get_word(w).has_all_of(b, size);
}
int num_high_bits = num_bits_per_word - b;
if (!_array[w].has_all_of(b, num_high_bits)) {
return false;
}
size -= num_high_bits;
++w;
while (size > 0) {
if (size <= num_bits_per_word) {
// The remainder fits within one word of the array.
return _array[w].has_all_of(0, size);
}
// Keep going.
if (!_array[w].is_all_on()) {
return false;
}
size -= num_bits_per_word;
++w;
if (w >= (int)get_num_words()) {
// Now we're up to the highest bits.
return (_highest_bits != 0);
}
}
return true;
}
/**
* Sets the indicated range of bits on.
*/
void BitArray::
set_range(int low_bit, int size) {
int w = low_bit / num_bits_per_word;
int b = low_bit % num_bits_per_word;
if (w >= (int)get_num_words() && _highest_bits) {
// All the highest bits are already on.
return;
}
if (b + size <= num_bits_per_word) {
// The whole thing fits within one word of the array.
ensure_has_word(w);
_array[w].set_range(b, size);
normalize();
return;
}
ensure_has_word(w);
int num_high_bits = num_bits_per_word - b;
_array[w].set_range(b, num_high_bits);
size -= num_high_bits;
++w;
while (size > 0) {
if (size <= num_bits_per_word) {
// The remainder fits within one word of the array.
ensure_has_word(w);
_array[w].set_range(0, size);
normalize();
return;
}
// Keep going.
ensure_has_word(w);
_array[w] = MaskType::all_on();
size -= num_bits_per_word;
++w;
if (w >= (int)get_num_words() && _highest_bits) {
// All the highest bits are already on.
normalize();
return;
}
}
normalize();
}
/**
* Sets the indicated range of bits off.
*/
void BitArray::
clear_range(int low_bit, int size) {
int w = low_bit / num_bits_per_word;
int b = low_bit % num_bits_per_word;
if (w >= (int)get_num_words() && !_highest_bits) {
// All the highest bits are already off.
return;
}
if (b + size <= num_bits_per_word) {
// The whole thing fits within one word of the array.
ensure_has_word(w);
_array[w].clear_range(b, size);
normalize();
return;
}
ensure_has_word(w);
int num_high_bits = num_bits_per_word - b;
_array[w].clear_range(b, num_high_bits);
size -= num_high_bits;
++w;
while (size > 0) {
if (size <= num_bits_per_word) {
// The remainder fits within one word of the array.
ensure_has_word(w);
_array[w].clear_range(0, size);
normalize();
return;
}
// Keep going.
ensure_has_word(w);
_array[w] = MaskType::all_off();
size -= num_bits_per_word;
++w;
if (w >= (int)get_num_words() && !_highest_bits) {
// All the highest bits are already off.
normalize();
return;
}
}
normalize();
}
/**
* Returns the number of bits that are set to 1 in the array. Returns -1 if
* there are an infinite number of 1 bits.
*/
int BitArray::
get_num_on_bits() const {
if (_highest_bits) {
return -1;
}
int result = 0;
Array::const_iterator ai;
for (ai = _array.begin(); ai != _array.end(); ++ai) {
result += (*ai).get_num_on_bits();
}
return result;
}
/**
* Returns the number of bits that are set to 0 in the array. Returns -1 if
* there are an infinite number of 0 bits.
*/
int BitArray::
get_num_off_bits() const {
if (!_highest_bits) {
return -1;
}
int result = 0;
Array::const_iterator ai;
for (ai = _array.begin(); ai != _array.end(); ++ai) {
result += (*ai).get_num_off_bits();
}
return result;
}
/**
* Returns the index of the lowest 1 bit in the array. Returns -1 if there
* are no 1 bits.
*/
int BitArray::
get_lowest_on_bit() const {
int num_words = get_num_words();
for (int w = 0; w < num_words; ++w) {
int b = _array[w].get_lowest_on_bit();
if (b != -1) {
return w * num_bits_per_word + b;
}
}
if (_highest_bits) {
return num_words * num_bits_per_word;
} else {
return -1;
}
}
/**
* Returns the index of the lowest 0 bit in the array. Returns -1 if there
* are no 0 bits.
*/
int BitArray::
get_lowest_off_bit() const {
int num_words = get_num_words();
for (int w = 0; w < num_words; ++w) {
int b = _array[w].get_lowest_off_bit();
if (b != -1) {
return w * num_bits_per_word + b;
}
}
if (!_highest_bits) {
return num_words * num_bits_per_word;
} else {
return -1;
}
}
/**
* Returns the index of the highest 1 bit in the array. Returns -1 if there
* are no 1 bits or if there an infinite number of 1 bits.
*/
int BitArray::
get_highest_on_bit() const {
if (_highest_bits) {
return -1;
}
int num_words = get_num_words();
for (int w = num_words - 1; w >= 0; --w) {
int b = _array[w].get_highest_on_bit();
if (b != -1) {
return w * num_bits_per_word + b;
}
}
return -1;
}
/**
* Returns the index of the highest 0 bit in the array. Returns -1 if there
* are no 0 bits or if there an infinite number of 1 bits.
*/
int BitArray::
get_highest_off_bit() const {
if (!_highest_bits) {
return -1;
}
int num_words = get_num_words();
for (int w = num_words - 1; w >= 0; --w) {
int b = _array[w].get_highest_off_bit();
if (b != -1) {
return w * num_bits_per_word + b;
}
}
return -1;
}
/**
* Returns the index of the next bit in the array, above low_bit, whose value
* is different that the value of low_bit. Returns low_bit again if all bits
* higher than low_bit have the same value.
*
* This can be used to quickly iterate through all of the bits in the array.
*/
int BitArray::
get_next_higher_different_bit(int low_bit) const {
int w = low_bit / num_bits_per_word;
int b = low_bit % num_bits_per_word;
int num_words = get_num_words();
if (w >= num_words) {
return low_bit;
}
int b2 = _array[w].get_next_higher_different_bit(b);
if (b2 != b && b2 < num_bits_per_word) {
// The next higher bit is within the same word.
return w * num_bits_per_word + b2;
}
// Look for the next word with anything interesting.
MaskType skip_next = (_array[w].get_bit(b)) ? MaskType::all_on() : MaskType::all_off();
int w2 = w;
++w2;
while (w2 < num_words && _array[w2] == skip_next) {
++w2;
}
if (w2 >= num_words) {
// All bits higher are the same value.
int is_on = _array[w].get_bit(b);
return is_on ? (num_words * num_bits_per_word) : low_bit;
}
if (_array[w2].get_bit(0) != _array[w].get_bit(b)) {
// The first bit of word w2 is different.
return w2 * num_bits_per_word;
}
b2 = _array[w2].get_next_higher_different_bit(0);
return w2 * num_bits_per_word + b2;
}
/**
* Inverts all the bits in the BitArray. This is equivalent to array =
* ~array.
*/
void BitArray::
invert_in_place() {
_highest_bits = !_highest_bits;
copy_on_write();
Array::iterator ai;
for (ai = _array.begin(); ai != _array.end(); ++ai) {
(*ai) = ~(*ai);
}
}
/**
* Returns true if this BitArray has any "one" bits in common with the other
* one, false otherwise.
*
* This is equivalent to (array & other) != 0, but may be faster.
*/
bool BitArray::
has_bits_in_common(const BitArray &other) const {
if (_highest_bits && other._highest_bits) {
// Yup, in fact we have an infinite number of bits in common.
return true;
}
size_t num_common_words = min(_array.size(), other._array.size());
// Consider the words that are on top of either array.
if (other._array.size() < _array.size() && other._highest_bits) {
// The other array has fewer actual words, and the top n words of the
// other array are all ones. We have bits in common if any of our top n
// words are nonzero.
Array::const_iterator ai;
for (ai = _array.begin() + other._array.size();
ai != _array.end();
++ai) {
if (!(*ai).is_zero()) {
return true;
}
}
} else if (_array.size() < other._array.size() && _highest_bits) {
// This array has fewer actual words, and the top n words of this array
// are all ones. We have bits in common if any of the the other's top n
// words are nonzero.
Array::const_iterator ai;
for (ai = other._array.begin() + _array.size();
ai != other._array.end();
++ai) {
if (!(*ai).is_zero()) {
return true;
}
}
}
// Consider the words that both arrays have in common.
for (size_t i = 0; i < num_common_words; ++i) {
if (!(_array[i] & other._array[i]).is_zero()) {
return true;
}
}
// Nope, nothing.
return false;
}
/**
* Writes the BitArray out as a hex number. For a BitArray, this is always
* the same as output_hex(); it's too confusing for the output format to
* change back and forth at runtime.
*/
void BitArray::
output(ostream &out) const {
output_hex(out);
}
/**
* Writes the BitArray out as a binary number, with spaces every four bits.
*/
void BitArray::
output_binary(ostream &out, int spaces_every) const {
if (_highest_bits) {
out << "...1 ";
}
int num_bits = max((int)get_num_bits(), spaces_every);
for (int i = num_bits - 1; i >= 0; i--) {
if (spaces_every != 0 && ((i % spaces_every) == spaces_every - 1)) {
out << ' ';
}
out << (get_bit(i) ? '1' : '0');
}
}
/**
* Writes the BitArray out as a hexadecimal number, with spaces every four
* digits.
*/
void BitArray::
output_hex(ostream &out, int spaces_every) const {
int num_bits = get_num_bits();
int num_digits = max((num_bits + 3) / 4, spaces_every);
if (_highest_bits) {
out << "...f ";
}
for (int i = num_digits - 1; i >= 0; i--) {
WordType digit = extract(i * 4, 4);
if (spaces_every != 0 && ((i % spaces_every) == spaces_every - 1)) {
out << ' ';
}
if (digit > 9) {
out << (char)(digit - 10 + 'a');
} else {
out << (char)(digit + '0');
}
}
}
/**
* Writes the BitArray out as a binary or a hex number, according to the
* number of bits.
*/
void BitArray::
write(ostream &out, int indent_level) const {
indent(out, indent_level) << *this << "\n";
}
/**
* Returns a number less than zero if this BitArray sorts before the indicated
* other BitArray, greater than zero if it sorts after, or 0 if they are
* equivalent. This is based on the same ordering defined by operator <.
*/
int BitArray::
compare_to(const BitArray &other) const {
if (_highest_bits != other._highest_bits) {
return _highest_bits ? 1 : -1;
}
int num_words = max(get_num_words(), other.get_num_words());
// Compare from highest-order to lowest-order word.
for (int i = num_words - 1; i >= 0; --i) {
int compare = get_word(i).compare_to(other.get_word(i));
if (compare != 0) {
return compare;
}
}
return 0;
}
/**
*
*/
void BitArray::
operator &= (const BitArray &other) {
size_t num_common_words = min(_array.size(), other._array.size());
copy_on_write();
// Consider the words that are on top of either array.
if (other._array.size() < _array.size() && !other._highest_bits) {
// The other array has fewer actual words, and the top n words of the
// other array are all zeroes. "mask off" the top n words of this array.
_array.erase(_array.begin() + other._array.size(), _array.end());
} else if (_array.size() < other._array.size() && _highest_bits) {
// This array has fewer actual words, and the top n words of this array
// are all ones. "mask on" the top n words of the other array.
Array::const_iterator ai;
for (ai = other._array.begin() + _array.size();
ai != other._array.end();
++ai) {
_array.push_back(*ai);
}
}
// Consider the words that both arrays have in common.
for (size_t i = 0; i < num_common_words; ++i) {
_array[i] &= other._array[i];
}
_highest_bits &= other._highest_bits;
normalize();
}
/**
*
*/
void BitArray::
operator |= (const BitArray &other) {
size_t num_common_words = min(_array.size(), other._array.size());
copy_on_write();
// Consider the words that are on top of either array.
if (other._array.size() < _array.size() && other._highest_bits) {
// The other array has fewer actual words, and the top n words of the
// other array are all ones. The top n words of this array become ones
// too (which means we can drop them out).
_array.erase(_array.begin() + other._array.size(), _array.end());
} else if (_array.size() < other._array.size() && !_highest_bits) {
// This array has fewer actual words, and the top n words of this array
// are all zeros. Copy in the top n words of the other array.
Array::const_iterator ai;
for (ai = other._array.begin() + _array.size();
ai != other._array.end();
++ai) {
_array.push_back(*ai);
}
}
// Consider the words that both arrays have in common.
for (size_t i = 0; i < num_common_words; ++i) {
_array[i] |= other._array[i];
}
_highest_bits |= other._highest_bits;
normalize();
}
/**
*
*/
void BitArray::
operator ^= (const BitArray &other) {
size_t num_common_words = min(_array.size(), other._array.size());
copy_on_write();
// Consider the words that are on top of either array.
if (other._array.size() < _array.size() && other._highest_bits) {
// The other array has fewer actual words, and the top n words of the
// other array are all ones. The top n words of this array get inverted.
Array::iterator ai;
for (ai = _array.begin() + other._array.size();
ai != _array.end();
++ai) {
(*ai).invert_in_place();
}
} else if (_array.size() < other._array.size()) {
if (!_highest_bits) {
// This array has fewer actual words, and the top n words of this array
// are all zeros. Copy in the top n words of the other array.
Array::const_iterator ai;
for (ai = other._array.begin() + _array.size();
ai != other._array.end();
++ai) {
_array.push_back(*ai);
}
} else {
// This array has fewer actual words, and the top n words of this array
// are all ones. Copy in the top n words of the other array, inverted.
Array::const_iterator ai;
for (ai = other._array.begin() + _array.size();
ai != other._array.end();
++ai) {
_array.push_back(~(*ai));
}
}
}
// Consider the words that both arrays have in common.
for (size_t i = 0; i < num_common_words; ++i) {
_array[i] ^= other._array[i];
}
_highest_bits ^= other._highest_bits;
normalize();
}
/**
* Logical left shift. The rightmost bits are filled in with zeroes. Since
* this is an infinite bit array, none of the bits on the left are lost.
*/
void BitArray::
operator <<= (int shift) {
if (shift == 0 || _array.empty()) {
return;
}
if (shift < 0) {
operator >>= (-shift);
return;
}
int w = shift / num_bits_per_word;
int b = shift % num_bits_per_word;
if (b == 0) {
// Easy case--word-at-a-time.
Array new_array;
new_array.reserve(_array.size() + w);
for (int i = 0; i < w; ++i) {
new_array.push_back(MaskType::all_off());
}
Array::const_iterator ai;
for (ai = _array.begin(); ai != _array.end(); ++ai) {
new_array.push_back(*ai);
}
_array = new_array;
} else {
// Harder case--we have to shuffle bits between words.
Array new_array;
new_array.reserve(_array.size() + w + 1);
for (int i = 0; i < w; ++i) {
new_array.push_back(MaskType::all_off());
}
int downshift_count = num_bits_per_word - b;
MaskType lower_mask = MaskType::lower_on(downshift_count);
MaskType upper_mask = ~lower_mask;
Array::const_iterator ai = _array.begin();
nassertv(ai != _array.end());
MaskType next_bits = ((*ai) & upper_mask) >> downshift_count;
new_array.push_back(((*ai) & lower_mask) << b);
++ai;
while (ai != _array.end()) {
new_array.push_back((((*ai) & lower_mask) << b) | next_bits);
next_bits = ((*ai) & upper_mask) >> downshift_count;
++ai;
}
// Finally, the top n bits.
if (_highest_bits) {
next_bits |= ~MaskType::lower_on(b);
}
new_array.push_back(next_bits);
_array = new_array;
}
normalize();
}
/**
* Logical right shift. The rightmost bits are lost. Since this is an
* infinite bit array, there is no question of sign extension; there is no
* need to synthesize bits on the left.
*/
void BitArray::
operator >>= (int shift) {
if (shift == 0 || _array.empty()) {
return;
}
if (shift < 0) {
operator <<= (-shift);
return;
}
int w = shift / num_bits_per_word;
int b = shift % num_bits_per_word;
if (w >= (int)_array.size()) {
// Trivial case--shift to nothing.
_array.clear();
return;
}
if (b == 0) {
// Easy case--word-at-a-time.
Array new_array;
new_array.reserve(_array.size() - w);
Array::const_iterator ai;
for (ai = _array.begin() + w; ai != _array.end(); ++ai) {
new_array.push_back(*ai);
}
_array = new_array;
} else {
// Harder case--we have to shuffle bits between words.
Array new_array;
new_array.reserve(_array.size() - w);
int upshift_count = num_bits_per_word - b;
MaskType lower_mask = MaskType::lower_on(b);
MaskType upper_mask = ~lower_mask;
Array::const_iterator ai = _array.begin() + w;
nassertv(ai < _array.end());
MaskType next_bits = ((*ai) & upper_mask) >> b;
++ai;
while (ai != _array.end()) {
new_array.push_back((((*ai) & lower_mask) << upshift_count) | next_bits);
next_bits = ((*ai) & upper_mask) >> b;
++ai;
}
// Finally, the top n bits.
if (_highest_bits) {
next_bits |= ~MaskType::lower_on(upshift_count);
}
new_array.push_back(next_bits);
_array = new_array;
}
normalize();
}
/**
* Adds the bitmask to the indicated hash generator.
*/
void BitArray::
generate_hash(ChecksumHashGenerator &hashgen) const {
hashgen.add_int(_highest_bits);
Array::const_iterator ai;
for (ai = _array.begin(); ai != _array.end(); ++ai) {
hashgen.add_int((*ai).get_word());
}
}
/**
* Ensures that at least word n has been allocated into the array.
*/
void BitArray::
ensure_has_word(int n) {
copy_on_write();
if (_highest_bits) {
while ((size_t)n >= _array.size()) {
_array.push_back(MaskType::all_on());
}
} else {
while ((size_t)n >= _array.size()) {
_array.push_back(MaskType::all_off());
}
}
}
/**
* Ensures that the array is the smallest array that represents this same
* value, by removing the topmost words that are all bits off (or on).
*/
void BitArray::
normalize() {
if (_highest_bits) {
if (!_array.empty() && _array.back() == MaskType::all_on()) {
copy_on_write();
_array.pop_back();
while (!_array.empty() && _array.back() == MaskType::all_on()) {
_array.pop_back();
}
}
} else {
if (!_array.empty() && _array.back().is_zero()) {
copy_on_write();
_array.pop_back();
while (!_array.empty() && _array.back().is_zero()) {
_array.pop_back();
}
}
}
}
/**
* Writes the contents of this object to the datagram for shipping out to a
* Bam file.
*/
void BitArray::
write_datagram(BamWriter *manager, Datagram &dg) const {
dg.add_uint32(_array.size());
Array::const_iterator ai;
for (ai = _array.begin(); ai != _array.end(); ++ai) {
dg.add_uint32((*ai).get_word());
}
dg.add_uint8(_highest_bits);
}
/**
* Reads the object that was previously written to a Bam file.
*/
void BitArray::
read_datagram(DatagramIterator &scan, BamReader *manager) {
size_t num_words = scan.get_uint32();
_array = Array::empty_array(num_words);
for (size_t i = 0; i < num_words; ++i) {
_array[i] = WordType(scan.get_uint32());
}
_highest_bits = scan.get_uint8();
}