-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathbitvectr.h
1690 lines (1366 loc) · 53 KB
/
bitvectr.h
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
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright IBM Corp. and others 1996
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution
* and is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License, v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception [1] and GNU General Public
* License, version 2 with the OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*******************************************************************************/
/***************************************************************************/
/* */
/* File name: bitvectr.h */
/* Purpose: Definition of the BitVector abstract base class. */
/* */
/***************************************************************************/
#ifndef CS2_BITVECTR_H
#define CS2_BITVECTR_H
#include "cs2/cs2.h"
#include "cs2/bitmanip.h"
#include "cs2/allocator.h"
#ifdef CS2_ALLOCINFO
#define allocate(x) allocate(x, __FILE__, __LINE__)
#define deallocate(x,y) deallocate(x, y, __FILE__, __LINE__)
#define reallocate(x,y,z) reallocate(x, y, z, __FILE__, __LINE__)
#endif
namespace CS2 {
// BitVector
//
// This class acts just like an array of bits. You can
// use the [] operator to address the bits in the array.
//
// The bit vector is initially zero.
typedef uint32_t BitIndex;
typedef uint32_t ShortWord;
#ifdef BITVECTOR_64BIT
typedef uint64_t BitWord;
const uint64_t kHighBit = 0x8000000000000000ull;
const uint64_t kFullMask = 0xFFFFFFFFFFFFFFFFull;
const uint64_t kZeroBits = 0x0000000000000000ull;
#else /* ! BITVECTOR_64BIT */
typedef uint32_t BitWord;
const uint32_t kHighBit = 0x80000000ul;
const uint32_t kFullMask = 0xFFFFFFFFul;
const uint32_t kZeroBits = 0x00000000ul;
#endif /* BITVECTOR_64BIT */
const uint32_t kBitWordSize = 8 * sizeof(BitWord);
const uint32_t kShortWordSize = 8 * sizeof(ShortWord);
template <class Allocator>
class ABitVector : private Allocator {
private:
class BitRef;
public:
explicit ABitVector(const Allocator &a = Allocator()) :
Allocator(a), fNumBits(0), fBitWords(NULL) {
}
~ABitVector() {
ClearToNull();
}
// Copy constructor and assignment operator
// These just copy the bits, no memory allocation is done
ABitVector (const ABitVector &adoptVector);
ABitVector (const ABitVector &adoptVector, const Allocator &a);
ABitVector & operator= (const ABitVector &adoptVector);
Allocator& allocator() { return *this;}
// Return the bit (0 or 1) at the given index
const BitRef operator[] (BitIndex) const;
BitRef operator[] (BitIndex bitIndex) {
return BitRef(*this, bitIndex);
}
static bool hasFastRandomLookup() {
return true;
}
// Return the value of the bit (0 or 1) at the given index
bool ValueAt (BitIndex) const;
BitWord WordValueAt (BitIndex, BitIndex) const;
void GrowTo (BitIndex index, bool geometric = true, bool forceGeometric = false);
void GrowTo (const ABitVector &vector);
// Check if bit vectors are equal
bool operator== (const ABitVector &) const;
bool operator!= (const ABitVector &) const;
//Bitwise operations
ABitVector & operator|= (const ABitVector &);
ABitVector & operator&= (const ABitVector &);
ABitVector & operator-= (const ABitVector &);
//Bitwise operations
template <class B2>
ABitVector & operator|= (const B2 &);
template <class B2>
ABitVector & operator&= (const B2 &);
template <class B2>
ABitVector & operator-= (const B2 &);
// Clear all bits in the vector (ie. clear and discard memory), and make empty.
void Clear ();
// Clear all bits in the vector (ie. clear and discard memory), and make null.
void ClearToNull ();
// deprecated. use Clear.
void Truncate() { Clear();}
// Set first n bits in the vector
void SetAll (BitIndex numBits);
// Return the index of the first zero bit in the vector
BitIndex LowestZero() const;
// Return the index of the first one bit in the vector
BitIndex FirstOne() const;
BitIndex LastOne() const;
int32_t FirstOneWordIndex() const;
int32_t LastOneWordIndex() const;
// if there is a bit in the specified range, clear the highest one and set
// foundone to true. otherwise, set foundone to false
BitIndex ClearLastOneIfThereIsOneInRange(BitIndex low, BitIndex high, bool& foundone);
// Determine if the vector is cleared, ie is the empty set.
bool IsZero() const;
// Determine if the vector is cleared to null, ie is the zero measure set "null set" which is also the empty set.
bool IsNull() const;
// Determine if the intersection of this vector and the input vector
// is non-zero
bool Intersects (const ABitVector &inputVector) const;
template <class B2>
bool Intersects (const B2 &inputVector) const;
// Determine if this vector is a subset of the inputVector
bool operator <= (const ABitVector &inputVector) const;
// Return the size in words (as defined by the type BitWord) of a vector
// of the given bit length.
uint32_t SizeInWords () const;
static uint32_t SizeInWords (BitIndex numBits);
// Return the size in 4-byte words (as defined by the type
// ShortWord) of a vector of the given bit length. Only to be used
// by the CopyToMemory() CopyFromMemory routines to maintain bitwise
// compatibility between 32 and 64-bit implementations
static uint32_t SizeInShortWords (BitIndex numBits);
// Return the size in bytes of a vector of the given bit length.
static uint32_t SizeInBytes (BitIndex numBits);
// Return the number of bytes of memory used by this bit vector
unsigned long MemoryUsage() const;
// Return the number of '1' bits in the vector. Optionally specify the
// number of leading bits to examine.
uint32_t PopulationCount (uint32_t numBits = 0xEFFFFFFFul) const;
uint32_t PopulationCount (const ABitVector &mask) const;
// Copy the vector to the given memory area.
// If numBits if greater than the size of the vector, then pad with
// zeroes.
void CopyToMemory (void *ptr, BitIndex numBits = 0) const;
// Copy the vector from the given memory area.
// If numBits is greater than the size of the vector, then copy only
// up to the size of the vector.
void CopyFromMemory (void *ptr, BitIndex numBits = 0);
// Bitwise binary logical operations (two operand and three operand formats)
// Return value indicates if the target vector was changed.
bool And (const ABitVector &inputVector);
bool And (const ABitVector &inputVector,
ABitVector &outputVector) const;
bool Andc (const ABitVector &inputVector);
bool Andc (const ABitVector &inputVector,
ABitVector &outputVector) const;
bool Or (const ABitVector &inputVector);
bool Or (const ABitVector &inputVector,
ABitVector &outputVector) const;
bool Xor (const ABitVector &inputVector);
bool Xor (const ABitVector &inputVector,
ABitVector &outputVector) const;
template <class B2> void Or(const B2 &v);
template <class B2> void Andc(const B2 &v);
template <class B2> void And(const B2 &v);
template <class B2> ABitVector &operator= (const B2 &v);
template <class B2>
void Or(B2** vs, uint32_t nvs);
static inline uint32_t Minimum(uint32_t v1, uint32_t v2){
if (v1<v2) return v1; return v2;
}
static inline uint32_t Maximum(uint32_t v1, uint32_t v2){
if (v1>v2) return v1; return v2;
}
class Cursor;
friend class ABitVector::Cursor;
class Cursor {
public:
Cursor (const ABitVector &adoptVector) : fVector(adoptVector),
fWord(0),
fIndex(0),
fWordCount(fVector.SizeInWords())
{}
Cursor (const Cursor &adoptCursor) : fVector(adoptCursor.fVector),
fWord(adoptCursor.fVector),
fIndex(adoptCursor.fIndex),
fWordCount(adoptCursor.fWordCount)
{}
bool SetToFirstOne() {
return SetToNextOneAfter(0);
}
bool SetToNextOne() {
fWord<<=1;
fIndex+=1;
BitWord word = fWord;
if (fWord==0) {
BitWord wordIndex;
for (wordIndex=(fIndex+kBitWordSize-1)/kBitWordSize;
wordIndex < fWordCount;
wordIndex+=1) {
word = fVector.WordAt(static_cast<uint32_t>(wordIndex));
if (word) {
fIndex = static_cast<BitIndex>(wordIndex * kBitWordSize);
goto find_bit;
}
}
fIndex = static_cast<BitIndex>(wordIndex * kBitWordSize);
return false;
}
find_bit:
uint32_t count = BitManipulator::LeadingZeroes(word);
fWord = word << count;
fIndex += count;
return true;
}
bool Valid() const {
return fIndex<fWordCount*kBitWordSize;
}
bool SetToNextOneAfter(uint32_t v) {
fWordCount = fVector.SizeInWords();
fIndex=v;
if (!Valid()) {
fIndex=fWordCount * kBitWordSize;
return false;
}
fWord=fVector.WordAt(v/kBitWordSize);
fWord <<= (v%kBitWordSize);
if (fWord&kHighBit) return true;
return SetToNextOne();
}
operator BitIndex() const {
return fIndex;
}
bool SetToFirstZero() {
CS2Assert (false, ("NOT IMPLEMENTED"));
return false;
}
bool SetToNextZero() {
CS2Assert (false, ("NOT IMPLEMENTED"));
return false;
}
protected:
const ABitVector &fVector;
BitWord fWord;
BitIndex fIndex;
uint32_t fWordCount;
Cursor &operator= (const Cursor &adoptCursor);
};
// STL style iteration - bare-bones, input iteration at this point
// TODO: reverse iterator?
// TODO: const iterator?
//
class iterator : private Cursor {
public:
iterator(const ABitVector &adoptVector, BitIndex index = 0) :
Cursor(adoptVector) {
Cursor::SetToNextOneAfter(index);
}
// Cast to index operator - returns the current index being pointed to
operator BitIndex() const { return Cursor::fIndex; }
// Dereference operator - identical to cast to index operator - note it returns the index, not a reference to it.
BitIndex operator*() const { return Cursor::fIndex; }
// Member access operator - not applicable to bit vectors, there is no element only an index
// BitIndex *operator->() const { return & (this->operator*()); }
// pre-increment
iterator& operator++() { Cursor::SetToNextOne(); return *this; }
// TODO: post-increment
// comparison
bool operator==(const iterator &other) const { return this == &other ||
(Cursor::fVector == other.fVector &&
Cursor::fIndex == other.fIndex); }
bool operator!=(const iterator &other) const { return ! operator==(other); }
};
// returns the iterator to the first element
iterator begin() { return iterator(*this); }
// returns the iterator to the one-past-the-last element
iterator end() { return iterator(*this, LastOne()+1); }
template <class ostr>
friend ostr &operator<< (ostr &out, const ABitVector &vector) {
typename ABitVector<Allocator>::Cursor vectorCursor (vector);
uint32_t i;
out << "( ";
for (vectorCursor.SetToFirstOne(), i = 0;
vectorCursor.Valid();
vectorCursor.SetToNextOne(), ++i) {
out << (int) vectorCursor << " ";
}
out << ")";
return out;
}
private:
// Return the size in bits of the vector
uint32_t SizeInBits() const;
BitWord & WordAt (uint32_t wordIndex) const;
uint32_t fNumBits;
BitWord *fBitWords;
class BitRef {
public:
BitRef (const ABitVector &vector, BitIndex index):
fIndex(index), fVector(vector) {}
BitRef (ABitVector &vector, BitIndex index):
fIndex(index), fVector(vector) {}
BitRef (const BitRef &b) :
fIndex(b.fIndex), fVector(b.fVector)
{ }
operator bool() const {
BitIndex wordIndex;
BitIndex bitIndex;
bool bitValue;
// Bits beyond the allocated size are assumed to be zero.
if (fIndex >= fVector.SizeInBits()) return false;
wordIndex = fIndex / kBitWordSize;
bitIndex = fIndex % kBitWordSize;
bitValue = ((fVector.WordAt(wordIndex) << bitIndex) >> (kBitWordSize - 1)) != 0;
return bitValue;
}
BitRef& operator= (bool bitValue) {
uint32_t wordIndex, bitIndex, shiftAmount;
BitWord bitMask, resultWord;
CS2Assert (bitValue == 0 || bitValue == 1, ("Incorrect bool value"));
// Do not grow to set a 0
if (bitValue == 0 && fVector.SizeInBits() < fIndex)
return *this;
fVector.GrowTo(fIndex+1);
CS2Assert (fIndex < fVector.SizeInBits(), ("Bit vector index out of range"));
wordIndex = fIndex / kBitWordSize;
bitIndex = fIndex % kBitWordSize;
shiftAmount = kBitWordSize - bitIndex - 1;
bitMask = ((BitWord)bitValue) << shiftAmount;
resultWord = fVector.WordAt(wordIndex);
resultWord &= ~(((BitWord)1) << shiftAmount);
resultWord |= bitMask;
fVector.WordAt(wordIndex) = resultWord;
return *this;
}
BitRef& operator= (const BitRef& value) {
return *this = (bool) value;
}
private:
BitIndex fIndex;
ABitVector &fVector;
};
template <class A2>
friend
inline void Swap(ABitVector<A2> &vectorA, ABitVector<A2> &vectorB);
};
template <class Allocator>
inline uint32_t ABitVector<Allocator>::SizeInBits() const {
return fNumBits;
}
template <class Allocator>
inline BitWord & ABitVector<Allocator>::WordAt (uint32_t wordIndex) const {
CS2Assert (wordIndex < SizeInWords(fNumBits),
("Accessing ABitVector word %d, over current size %d\n",
wordIndex, SizeInWords(fNumBits)));
return fBitWords[wordIndex];
}
template <class Allocator>
inline ABitVector<Allocator>::ABitVector (const ABitVector<Allocator> &adoptVector) : Allocator(adoptVector), fNumBits(0), fBitWords(NULL) {
*this = adoptVector;
}
template <class Allocator>
inline ABitVector<Allocator>::ABitVector (const ABitVector<Allocator> &adoptVector, const Allocator &a) : Allocator(a), fNumBits(0), fBitWords(NULL) {
*this = adoptVector;
}
template <class Allocator>
inline bool ABitVector<Allocator>::ValueAt (BitIndex bitIndex) const {
BitIndex wordIndex;
if (bitIndex>= SizeInBits()) return false;
wordIndex = bitIndex / kBitWordSize;
bitIndex = bitIndex % kBitWordSize;
return ((WordAt(wordIndex) << bitIndex) >> (kBitWordSize - 1)) != 0;
}
template <class Allocator>
inline BitWord ABitVector<Allocator>::WordValueAt (BitIndex bitIndex, BitIndex wordIndex) const {
if (bitIndex>= SizeInBits()) return BitWord(0);
return WordAt(wordIndex);
}
// Grow the vector to at least 'newBitSize' bits. By default, grow the
// vector additional extents to guarantee geometric growth.
// Force geometric growth in special cases
// This routine also makes the vector non-null.
template <class Allocator>
inline
void ABitVector<Allocator>::GrowTo (BitIndex newBitSize, bool geometric, bool forceGeometric) {
uint32_t newWordSize, oldBitSize;
if (newBitSize <= fNumBits) {
if (fNumBits == 0) {
// make non-null
Clear();
}
return;
}
if (geometric &&
(forceGeometric || newBitSize<1024)) {
uint32_t power2Size=1;
while (power2Size<newBitSize) power2Size<<=1;
newBitSize = power2Size;
} else {
// bump newBitSize to next multiple of chunk
const uint32_t chunk = 128*8;
newBitSize = (newBitSize + chunk) - (newBitSize % chunk);
}
oldBitSize = fNumBits;
newWordSize = SizeInWords(newBitSize);
uint32_t numBits = newWordSize * kBitWordSize;
if (oldBitSize == 0) {
fBitWords = (BitWord *) Allocator::allocate(SizeInBytes(numBits));
memset(fBitWords,0, SizeInBytes(numBits));
} else {
fBitWords = (BitWord *) Allocator::reallocate(SizeInBytes(numBits),
fBitWords,
SizeInBytes(oldBitSize));
memset(((char *)fBitWords)+SizeInBytes(oldBitSize),0,
SizeInBytes(numBits) -
SizeInBytes(oldBitSize));
}
fNumBits = numBits;
}
template <class Allocator>
inline
void ABitVector<Allocator>::GrowTo (const ABitVector &vector) {
GrowTo(vector.fNumBits, false, false);
}
template <class Allocator>
inline bool ABitVector<Allocator>::operator!= (const ABitVector &v) const {
return !operator==(v);
}
template <class Allocator>
inline uint32_t ABitVector<Allocator>::SizeInWords (BitIndex numBits) {
#ifdef DEV_VERSION
CS2Assert (numBits < 0xFFFFFFFFul - kBitWordSize, ("Illegal vector length"));
#endif
return (numBits + kBitWordSize - 1) / kBitWordSize;
}
template <class Allocator>
inline uint32_t ABitVector<Allocator>::SizeInWords () const {
return SizeInWords(fNumBits);
}
template <class Allocator>
inline uint32_t ABitVector<Allocator>::SizeInShortWords (BitIndex numBits) {
#ifdef DEV_VERSION
CS2Assert (numBits < 0xFFFFFFFFul - kBitWordSize, ("Illegal vector length"));
#endif
return (numBits + kShortWordSize - 1) / kShortWordSize;
}
template <class Allocator>
inline uint32_t ABitVector<Allocator>::SizeInBytes (BitIndex numBits) {
return ABitVector<Allocator>::SizeInWords(numBits) * sizeof(BitWord);
}
// ABitVector<Allocator>::And (const ABitVector &)
//
// Compute the bitwise AND of this vector and the input vector.
// Place the result in this vector.
template <class Allocator>
inline bool ABitVector<Allocator>::And (const ABitVector &inputVector) {
return And (inputVector, *this);
}
// ABitVector<Allocator>::Andc (const ABitVector &)
//
// Compute the bitwise AND with COMPLEMENT of this vector and the input vector.
// Place the result in this vector.
template <class Allocator>
inline bool ABitVector<Allocator>::Andc (const ABitVector &inputVector) {
return Andc (inputVector, *this);
}
// ABitVector<Allocator>::Or (const ABitVector &)
//
// Compute the bitwise OR of this vector and the input vector.
// Place the result in this vector.
template <class Allocator>
inline bool ABitVector<Allocator>::Or (const ABitVector &inputVector) {
GrowTo(inputVector);
return Or (inputVector, *this);
}
template <class Allocator>
template <class B2>
inline void ABitVector<Allocator>::Or (const B2 &v) {
if (v.IsZero()) return;
GrowTo(v.LastOne()+1, false, false);
if (!v.hasBitWordRepresentation() || (v.wordSize() != kBitWordSize)) {
// have to use Cursor to do the OR
typename B2::Cursor vc(v);
vc.SetToFirstOne();
uint32_t wordIndex = vc / kBitWordSize;
BitWord word = WordAt(wordIndex);
uint32_t bitIndex = vc % kBitWordSize;
word |= (kHighBit >> bitIndex);
for (vc.SetToNextOne();vc.Valid();vc.SetToNextOne()) {
if (vc/kBitWordSize != wordIndex) {
WordAt(wordIndex) = word;
wordIndex = vc/kBitWordSize;
word = WordAt(wordIndex);
}
bitIndex = vc % kBitWordSize;
word |= (kHighBit >> bitIndex);
}
WordAt(wordIndex) = word;
return;
} else /* (hasBitWordRepresentation() && (v.wordSize() == kBitWordSize)) */ {
// can use more efficient implementation by ORed corresponding words from the two bit vectors together
// as their underlying bit representations are equivalent
for (int32_t i = v.FirstOneWordIndex(); i <= v.LastOneWordIndex(); i++)
WordAt(i) |= v.WordAt(i);
return;
}
}
template <class Allocator>
template <class B2>
inline void ABitVector<Allocator>::And(const B2 &v) {
if (IsZero()) {return;}
if (v.IsZero()) {Clear(); return;}
if (!v.hasBitWordRepresentation() || (v.wordSize() != kBitWordSize)) {
uint32_t lastWordIndex = SizeInWords();
typename B2::Cursor vc(v);
vc.SetToFirstOne();
uint32_t wordIndex = vc / kBitWordSize;
uint32_t i, lastWord=0;
BitWord word = 0;
uint32_t bitIndex = vc % kBitWordSize;
word |= (kHighBit >> bitIndex);
for (vc.SetToNextOne();vc.Valid();vc.SetToNextOne()) {
if (vc/kBitWordSize != wordIndex) {
if (wordIndex < lastWordIndex) {
for (i=lastWord; i<wordIndex; i++)
WordAt(i)=0;
WordAt(wordIndex) &= word;
lastWord = wordIndex + 1;
wordIndex = vc/kBitWordSize;
word = 0;
} else break;
}
bitIndex = vc % kBitWordSize;
word |= (kHighBit >> bitIndex);
}
if (wordIndex < lastWordIndex) {
for (i=lastWord; i<wordIndex; i++) {
WordAt(i)=0;
}
WordAt(wordIndex) &= word;
lastWord = wordIndex+1;
}
for (i=lastWord; i<lastWordIndex; i++)
WordAt(i)=0;
} else /* (hasBitWordRepresentation() && (v.wordSize() == kBitWordSize)) */ {
// can use more efficient implementation by ANDed corresponding words from the two bit vectors together
// as their underlying bit representations are equivalent
int32_t low = v.FirstOneWordIndex();
int32_t high = v.LastOneWordIndex();
int32_t thisLow = FirstOneWordIndex();
int32_t thisHigh = LastOneWordIndex();
if (high < thisLow || low > thisHigh) {
// No intersection
Clear();
return;
}
int32_t i;
// Clear all the words before and after those set in the other vector
if (low < thisLow)
low = thisLow;
else {
for (i = thisLow; i < low; i++)
WordAt(i) = kZeroBits;
}
if (high > thisHigh) {
high = thisHigh;
} else {
for (i = thisHigh; i > high; i--)
WordAt(i) = kZeroBits;
}
// AND in all of the words from the 2nd vector
for (i = low; i <= high; i++)
WordAt(i) &= v.WordAt(i);
// compress representation
if (IsZero()) { Clear(); }
}
}
template <class Allocator>
template <class B2>
inline void ABitVector<Allocator>::Andc(const B2 &v) {
if (v.IsZero()) return;
if (!v.hasBitWordRepresentation() || (v.wordSize() != kBitWordSize)) {
uint32_t lastWordIndex = SizeInWords(fNumBits);
typename B2::Cursor vc(v);
vc.SetToFirstOne();
uint32_t wordIndex = vc / kBitWordSize;
if (lastWordIndex <= wordIndex) return;
BitWord word = 0;
uint32_t bitIndex = vc % kBitWordSize;
word |= (kHighBit >> bitIndex);
for (vc.SetToNextOne();vc.Valid();vc.SetToNextOne()) {
if (vc/kBitWordSize != wordIndex) {
WordAt(wordIndex) &= ~word;
wordIndex = vc/kBitWordSize;
if (lastWordIndex <= wordIndex) return;
word = 0;
}
bitIndex = vc % kBitWordSize;
word |= (kHighBit >> bitIndex);
}
WordAt(wordIndex) &= ~word;
} else /* (hasBitWordRepresentation() && (v.wordSize() == kBitWordSize)) */ {
// can use more efficient implementation by ANDC corresponding words from the two bit vectors together
// as their underlying bit representations are equivalent
int32_t low = v.FirstOneWordIndex();
int32_t high = v.LastOneWordIndex();
int32_t thisLow = FirstOneWordIndex();
int32_t thisHigh = LastOneWordIndex();
if (high < thisLow || low > thisHigh)
return; // No intersection
// AND in the logical NOT of all of the words from the 2nd vector
if (low < thisLow)
low = thisLow;
if (high > thisHigh)
high = thisHigh;
for (int32_t wordIndex = low; wordIndex <= high; wordIndex++)
WordAt(wordIndex) &= ~v.WordAt(wordIndex);
// compress representation
if (IsZero()) { Clear(); }
}
}
template <class Allocator>
template <class B2>
inline ABitVector<Allocator> &ABitVector<Allocator>::operator= (const B2 &v) {
Clear();
Or(v);
return *this;
}
template <class Allocator>
template <class B2>
inline void ABitVector<Allocator>::Or (B2 **vs, uint32_t nvs) {
uint32_t i;
for (i=0; i<nvs; i++){
Or(*(vs[i]));
}
}
// ABitVector<Allocator>::Xor (const ABitVector &)
//
// Compute the bitwise XOR of this vector and the input vector.
// Place the result in this vector.
template <class Allocator>
inline bool ABitVector<Allocator>::Xor (const ABitVector &inputVector) {
GrowTo(inputVector);
return Xor (inputVector, *this);
}
//
// Swap one sparse bit vector with another.
template <class Allocator>
inline void Swap(ABitVector<Allocator> &vectorA, ABitVector<Allocator> &vectorB) {
uint32_t numBits = vectorA.fNumBits;
BitWord* bitWords = vectorA.fBitWords;
vectorA.fNumBits = vectorB.fNumBits;
vectorA.fBitWords = vectorB.fBitWords;
vectorB.fNumBits = numBits;
vectorB.fBitWords = bitWords;
}
template <class Allocator>
inline
ABitVector<Allocator> & ABitVector<Allocator>::operator= (const ABitVector<Allocator> &adoptVector) {
if (adoptVector.IsNull()) {
ClearToNull();
return *this;
}
GrowTo(adoptVector);
uint32_t bitsToCopy, remainder;
bitsToCopy = adoptVector.fNumBits;
if (bitsToCopy > fNumBits) {
bitsToCopy = fNumBits;
remainder = 0;
} else {
remainder = SizeInBytes(fNumBits) - SizeInBytes(bitsToCopy);
}
memcpy (fBitWords, adoptVector.fBitWords, SizeInBytes(bitsToCopy));
if (remainder > 0) {
memset ((char *) fBitWords + SizeInBytes(bitsToCopy), 0, remainder);
}
return *this;
}
template <class Allocator>
inline
bool ABitVector<Allocator>::operator== (const ABitVector<Allocator> &v) const {
uint32_t bx = 0, wx = 0,
minLen = Minimum(fNumBits,v.fNumBits);
while (bx < minLen) {
if (fBitWords[wx] != v.fBitWords[wx]) return false;
++wx;
bx += kBitWordSize;
}
if (bx < fNumBits) {
while (bx < fNumBits) {
if (fBitWords[wx] != kZeroBits) return false;
++wx;
bx += kBitWordSize;
}
} else if (bx < v.fNumBits) {
while (bx < v.fNumBits) {
if (v.fBitWords[wx] != kZeroBits) return false;
++wx;
bx += kBitWordSize;
}
}
return true;
}
template <class Allocator>
inline
unsigned long ABitVector<Allocator>::MemoryUsage() const {
unsigned long usageBytes;
usageBytes = sizeof(ABitVector);
usageBytes += SizeInBytes(fNumBits);
return usageBytes;
}
// ABitVector<Allocator>::Clear ()
//
// Clear all bits in the vector (ie. clear and discard memory)
template <class Allocator>
inline void ABitVector<Allocator>::Clear () {
if (fNumBits) {
Allocator::deallocate(fBitWords, SizeInBytes(fNumBits));
fNumBits=0;
}
// unallocated, but non-null
fBitWords = (BitWord *) 1;
}
// ABitVector<Allocator>::ClearToNull ()
//
// Clear all bits in the vector (ie. clear and discard memory), clear to null set.
template <class Allocator>
inline void ABitVector<Allocator>::ClearToNull () {
Clear();
// unallocated, and null
fBitWords=NULL;
}
// ABitVector<Allocator>::Set ()
//
// Set first n bits in the vector
template <class Allocator>
inline void ABitVector<Allocator>::SetAll (BitIndex numBits) {
if (numBits == 0)
return;
GrowTo(numBits);
uint32_t lastWordIndex = SizeInWords(numBits) - 1;
for (uint32_t i = 0; i < lastWordIndex; i++)
WordAt(i) = (BitWord)-1;
uint32_t lastIndex = lastWordIndex * kBitWordSize;
for (uint32_t i = lastIndex; i < numBits; i++)
(*this)[i] = true;
}
// ABitVector<Allocator>::LowestZero ()
//
// Return the index of the first zero bit in the vector
template <class Allocator>
inline BitIndex ABitVector<Allocator>::LowestZero () const {
uint32_t wordIndex, vectorWordSize;
vectorWordSize = SizeInWords(fNumBits);
// Scan to the first word which has a zero bit
for (wordIndex = 0;
(wordIndex < vectorWordSize) && (WordAt(wordIndex) == kFullMask);
++wordIndex);
// If we found no zero bits return the index just beyond the vector
if (wordIndex >= vectorWordSize) return fNumBits + 1;
return wordIndex * kBitWordSize +
BitManipulator::LeadingOnes (WordAt(wordIndex));
}
// ABitVector<Allocator>::LowestOne ()
//
// Return the index of the first one bit in the vector
template <class Allocator>
inline BitIndex ABitVector<Allocator>::FirstOne () const {
uint32_t wordIndex, vectorWordSize;
vectorWordSize = SizeInWords(fNumBits);
// Scan to the first word which has a one bit
for (wordIndex=0;
(wordIndex < vectorWordSize) && (WordAt(wordIndex) == kZeroBits);
++wordIndex);
// If we found no bits return 0;
if (wordIndex >= vectorWordSize) return 0;
return wordIndex * kBitWordSize +
BitManipulator::LeadingZeroes (WordAt(wordIndex));
}
template <class Allocator>
inline BitIndex ABitVector<Allocator>::LastOne () const {
uint32_t lastIndex, wordIndex, vectorWordSize;
vectorWordSize = SizeInWords(fNumBits);
lastIndex = 0;
// Scan to find the last word which has a one bit
for (wordIndex=0;
(wordIndex < vectorWordSize);
++wordIndex)
if (WordAt(wordIndex) != kZeroBits)
lastIndex = wordIndex;
// If we found no zero bits return 0;
if (lastIndex>= vectorWordSize || WordAt(lastIndex) == kZeroBits) return 0;
return lastIndex * kBitWordSize +
kBitWordSize -
BitManipulator::TrailingZeroes (WordAt(lastIndex)) - 1;
}
// ABitVector<Allocator>::FirstOneWordIndex ()
//
// Return the index of the first word with a one bit in the vector, or SizeInWords() if not found