-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathsparsrbit.h
2447 lines (2071 loc) · 74.9 KB
/
sparsrbit.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
*******************************************************************************/
#ifndef CS2_SPARSRBIT_H
#define CS2_SPARSRBIT_H
#include "cs2/cs2.h"
#include "cs2/allocator.h"
#include "cs2/bitmanip.h"
#include "cs2/bitvectr.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 {
/***************************************************************************
* This is the definition of the ASparseBitVector class.
*
* It is a bitvector implementation as a sorted array of bits. This
* makes the storage requirements proportional to the number of 1-bits
* in the vector, instead of being proportional to the highest bit index.
*
* Each bit index is a 4-byte quantity. To reduce the memory
* requirements of the class, the bits are organized as segments,
* where each segment contains bit indices that share the two high bytes.
* The bitsegment implementation stores only the lower 2 bytes per index.
***************************************************************************/
#ifdef __64BIT__
typedef uint64_t SparseBitIndex;
#else /* ! __64BIT__ */
typedef uint32_t SparseBitIndex;
#endif
template <class Allocator>
class ASparseBitVector : private Allocator {
class SparseBitRef;
public:
static const SparseBitIndex kSegmentBits = 16;
static const SparseBitIndex kSegmentMask = 0xFFFF;
// Constructor defaults to a vector of one BitWord
ASparseBitVector (const Allocator &a = Allocator()) : Allocator(a), fBase(NULL), fNumberOfSegments(0) { }
// Copy constructor
ASparseBitVector (const ASparseBitVector &v) :
Allocator(v), fBase(NULL), fNumberOfSegments(v.fNumberOfSegments) {
size_t n = fNumberOfSegments;
if (n) {
fBase = (Segment *)Allocator::allocate(n * sizeof(Segment));
for (size_t i=0; i< n; i++)
fBase[i].copy(v.fBase[i], *this);
} else {
// sets to equiv zero set as v - either null or empty
fBase = v.fBase;
}
}
// Destructor will free bit vector storage
~ASparseBitVector () {
ClearToNull();
}
static bool hasFastRandomLookup() {
return false;
}
Allocator& allocator() { return *this;}
// Assign one sparse bit vector to another
ASparseBitVector & operator= (const ASparseBitVector<Allocator> &);
template <class A2>
ASparseBitVector & operator= (const ASparseBitVector<A2> &);
template <class B2>
ASparseBitVector & operator= (const B2 &);
// Check if two vectors are the same
bool operator== (const ASparseBitVector &) const;
bool operator!= (const ASparseBitVector &) const;
bool operator<= (const ASparseBitVector &) const;
//Bitwise operations
template <class B2>
ASparseBitVector & operator|= (const B2 &);
template <class B2>
ASparseBitVector & operator&= (const B2 &);
template <class B2>
ASparseBitVector & operator-= (const B2 &);
// And from an arbitrary vector representation
template<class B2> bool And(const B2 &vector);
// Or from an arbitrary vector representation
template<class B2> void Or(const B2 &abv);
template<class ACursor> void OrCursor(ACursor &abvc);
// Or from an array of vectors
void Or(ASparseBitVector** vs, SparseBitIndex nvs);
void Or(ASparseBitVector** vs, SparseBitIndex nvs, const ASparseBitVector &mask);
// Free any unused storage
void Compact();
// Indexing operator, grow if necessary
SparseBitRef operator[] (size_t index);
bool operator[] (size_t index) const;
// Indexing operator, do not grow
bool ValueAt(size_t index) const;
// Get first bit set to one
SparseBitIndex FirstOne() const;
// if there is a bit in the specified range, clear the highest one and set
// foundone to true. otherwise, set foundone to false
SparseBitIndex ClearLastOneIfThereIsOneInRange(SparseBitIndex low, SparseBitIndex high, bool& foundone);
// Get last bit set to one
SparseBitIndex LastOne() const;
bool hasBitWordRepresentation() const { return false; } // returns whether this bit vector is based on an array of bitWords (which it is not)
int32_t FirstOneWordIndex() const { CS2Assert(false, ("This method is not implemented")); return 0; }
int32_t LastOneWordIndex() const { CS2Assert(false, ("This method is not implemented")); return 0; }
uint32_t wordSize() const { CS2Assert(false, ("This method is not implemented")); return 0; }
uint32_t WordAt (uint32_t wordIndex) const { CS2Assert(false, ("This method is not implemented")); return 0; }
// A growth routine to maintain compatibility with the dense bit vector
void GrowTo(SparseBitIndex index, bool geometric = true, bool forceGeometric = false);
// Logical operations in-place and with output vector.
// Return value indicates if the target vector was changed.
bool And(const ASparseBitVector &inputVector);
bool And(const ASparseBitVector &inputVector,
ASparseBitVector &outputVector) const;
bool Andc(const ASparseBitVector &inputVector);
template <class B2> bool Andc(const B2 &inputVector);
bool Andc(const ASparseBitVector &inputVector,
ASparseBitVector &outputVector) const;
bool Or(const ASparseBitVector &inputVector);
bool Or(const ASparseBitVector &inputVector,
ASparseBitVector &outputVector) const;
bool OrMask(const ASparseBitVector &inputVector,
const ASparseBitVector &maskVector);
// We will not implement COMPLEMENT and OR-WITH-COMPLEMENT since they will
// by definition produce DENSE vectors when given sparse vectors on input.
//
// void Orc (const ASparseBitVector &inputVector);
// void Orc (const ASparseBitVector &inputVector,
// ASparseBitVector &outputVector) const;
// void Comp ();
// void Comp (ASparseBitVector &outputVector) const;
// EXCLUSIVE-OR and generic logical operations not currently implemented.
// If there is a good reason, this can be done.
//
// void Xor (const ASparseBitVector &inputVector);
// void Xor (const ASparseBitVector &inputVector,
// ASparseBitVector &outputVector) const;
// void Logical (const ASparseBitVector &inputVector, SparseBitIndex map);
// void Logical (const ASparseBitVector &inputVector,
// ASparseBitVector &outputVector,
// SparseBitIndex map) const;
// Dump routine
template <class str> void Dump(str &) const;
// 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();}
// Return the index of the first zero bit in the vector
SparseBitIndex LowestZero() const;
// Return the index of the first one bit in the vector
SparseBitIndex LowestOne() const;
// 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
template <class B2>
bool Intersects (const B2 &inputVector) const;
bool Intersects (const ASparseBitVector &inputVector) const;
// Determine if the intersection of this vector and the input vector
// under the given mask is non-zero
bool IntersectsWithMask (const ASparseBitVector &inputVector,
const ASparseBitVector &maskVector) const;
// Return the number of bytes of memory used by this bit vector
unsigned long MemoryUsage() const;
// Return the size in bits of the vector
SparseBitIndex SizeInBits() const;
// Return the number of '1' bits in the vector. Optionally specify the
// number of leading bits to examine.
SparseBitIndex PopulationCount (SparseBitIndex numBits = 0xEFFFFFFFul) const;
SparseBitIndex PopulationCount (const ASparseBitVector &mask) const;
private:
class Segment {
public:
Segment() : fSegment(NULL), fSize(0), fHighBits(0), fNumValues(0) { }
void allocate(size_t size, Allocator &a) {
fSegment = (uint16_t *) a.allocate(size * sizeof(uint16_t));
fSize = static_cast<uint16_t>(size);
fNumValues = 0;
}
template <class S2>
void copy(S2 &s, Allocator &a) {
allocate(s.PopulationCount(), a);
fHighBits = s.fHighBits;
fNumValues = s.PopulationCount();
memcpy(fSegment, s.Indices(), fNumValues * sizeof(uint16_t));
}
// append elements of s starting at fromIndex to toIndex-1 to this Segment
template <class S2>
void append(S2 &s, uint16_t fromIndex, uint16_t toIndex) {
size_t n = toIndex - fromIndex;
memcpy(&fSegment[fNumValues], &s.Indices()[fromIndex], n * sizeof(uint16_t));
fNumValues += static_cast<uint32_t>(n);
}
// append elements of s to this Segment
template <class S2>
void append(S2 &s) {
append(s, 0, s.PopulationCount());
}
void append(uint16_t v) {
fSegment[fNumValues++] = v;
}
void adopt(size_t numValues, size_t size, uint16_t *bits) {
fSegment = bits;
fSize = static_cast<uint16_t>(size);
fNumValues = static_cast<uint32_t>(numValues);
}
void adopt(uint16_t highbits, size_t numValues, size_t size, uint16_t *bits) {
fHighBits = highbits;
adopt(numValues, size, bits);
}
// shallow swap of this with s (cannot override Swap method in cs2.h as Segment is private)
void swap(Segment &s) {
uint16_t *data = fSegment;
uint16_t size = fSize;
uint16_t highBits = fHighBits;
uint16_t numValues = fNumValues;
adopt(s.fHighBits, s.fNumValues, s.fSize, s.fSegment);
s.adopt(highBits, numValues, size, data);
}
void reallocate(size_t size, Allocator &a) {
fSegment = (uint16_t *) a.reallocate(size*sizeof(uint16_t),
fSegment,
fSize * sizeof(uint16_t));
fSize = static_cast<uint16_t>(size);
}
void deallocate(Allocator &a) { a.deallocate(fSegment, fSize*sizeof(uint16_t)); }
SparseBitIndex AllocatedSize() const { return SparseBitIndex(fSize); }
SparseBitIndex HighBits() const { return SparseBitIndex(fHighBits)<<16; }
SparseBitIndex IsZero() const { return fNumValues==0;}
SparseBitIndex PopulationCount() const { return fNumValues;}
SparseBitIndex FirstOne() const { return HighBits() + fSegment[0]; }
SparseBitIndex LastOne() const { return HighBits() + fSegment[fNumValues-1]; }
uint16_t *Indices() const { return fSegment; }
bool operator==(const Segment &s1) const {
if (fHighBits != s1.fHighBits) return false;
if (fNumValues != s1.fNumValues) return false;
return !memcmp(fSegment, s1.fSegment, sizeof(uint16_t)*PopulationCount());
}
size_t MemoryUsage() {
return sizeof(Segment)+fSize*sizeof(uint16_t);
}
// TODO: need to add destructor, then change everywhere that deallocates to rely on this destructor
private:
uint16_t *fSegment;
uint16_t fSize;
public:
uint16_t fHighBits;
uint32_t fNumValues;
};
Segment *fBase;
SparseBitIndex fNumberOfSegments;
Segment *FindSegment(SparseBitIndex index) const;
Segment *AddSegment(SparseBitIndex index, SparseBitIndex count);
Segment *AddSegment(SparseBitIndex index, SparseBitIndex count, uint16_t *bits);
Segment *OrSegment(SparseBitIndex index, SparseBitIndex count, uint16_t *bits);
void RemoveSegment(SparseBitIndex index);
void RemoveSegmentAt(SparseBitIndex segmentindex);
SparseBitIndex FindIndex(const Segment &, uint16_t, SparseBitIndex l=0, SparseBitIndex h=0) const;
SparseBitIndex AdvanceIndex(const Segment &, uint16_t, SparseBitIndex l=0, SparseBitIndex h=0) const;
// Grow thisSegment to at least the given size.
//
// if numValues is given, then it must accommodate numValues. Thus if numValues exceeds given size then grow size by some factor more.
// The new size is then guaranteed to exceed numValues.
void GrowSegment(Segment &thisSegment, uint32_t size, uint32_t numValues = 0);
void SetSegment(Segment &, SparseBitIndex);
void ClearSegment(Segment &, SparseBitIndex);
bool GetSegment(Segment &, SparseBitIndex) const;
SparseBitIndex ValueAtSegment(const Segment &, SparseBitIndex) const;
void TruncateSegment(Segment &);
void CompactSegment(Segment &);
template <class S2>
void CopySegment(Segment &,const S2 &);
bool OrSegment(Segment &, const Segment &);
bool OrSegment(const Segment &, const Segment &, Segment &);
bool OrSegmentMask(Segment &, const Segment &, const Segment &);
bool AndSegment(Segment &, const Segment &);
bool AndSegment(const Segment &, const Segment &, Segment &);
bool AndcSegment(Segment &, const Segment &);
bool AndcSegment(const Segment &, const Segment &, Segment &);
bool IsSubset(const Segment &thisSegment, const Segment &inputSegment ) const;
template <class str> void DumpSegment(str &out, const Segment &vector) const;
class SparseBitRef {
public:
SparseBitRef (ASparseBitVector &vector, SparseBitIndex index) : fIndex(index), fVector(vector) {}
// ~SparseBitRef() destructor intentionally omitted
SparseBitRef (const SparseBitRef &r) : fIndex(r.fIndex), fVector(r.fVector) {}
operator bool();
SparseBitRef& operator= (bool value);
private:
// should not be assigned
SparseBitRef &operator= (const SparseBitRef&);
// set/clear with shifts. To be used only from operator=
SparseBitRef &Set();
SparseBitRef &Clear ();
SparseBitIndex fIndex;
ASparseBitVector &fVector;
};
public:
class Cursor {
public:
Cursor (const ASparseBitVector &adoptVector);
Cursor (const Cursor &adoptCursor);
// ~Cursor(); // destructor intentionally omitted
void SetToFirstOne();
void SetToLastOne();
bool SetToNextOne();
bool SetToNextOneAfter(SparseBitIndex);
bool SetToPreviousOne();
bool Valid() const;
operator SparseBitIndex() const;
Cursor &operator= (SparseBitIndex);
private:
const ASparseBitVector &fVector;
const uint16_t *fSegment;
uint32_t fSegmentPopCount;
uint32_t fSegmentHighBits;
uint32_t fSegmentIndex;
uint32_t fSegmentOffset;
Cursor &operator= (const Cursor &adoptCursor);
};
class IntersectionCursor {
public:
IntersectionCursor (const ASparseBitVector &adoptVector1,
const ASparseBitVector &adoptVector2) :
c1(adoptVector1), c2(adoptVector2) {}
IntersectionCursor (const IntersectionCursor &adoptCursor) :
c1(adoptCursor.c1), c2(adoptCursor.c2) {}
// ~Cursor(); // destructor intentionally omitted
bool SetToFirstOne() {
c1.SetToFirstOne();
c2.SetToFirstOne();
if (Valid())
return FindNextCommonItem();
return false;
}
bool SetToNextOne() {
if (c1<=c2) {
if (!c1.SetToNextOne()) return false;
} else if (!c2.SetToNextOne()) return false;
return FindNextCommonItem();
}
bool SetToNextOneAfter(SparseBitIndex);
bool SetToPreviousOne();
bool Valid() const {
return c1.Valid() && c2.Valid();
}
operator SparseBitIndex() const {
return c1;
}
IntersectionCursor &operator= (SparseBitIndex);
private:
bool FindNextCommonItem() {
while (c1!=c2) {
if (c1<c2) {
if (!c1.SetToNextOneAfter(c2-1)) return false;
} else
if (!c2.SetToNextOneAfter(c1-1)) return false;
}
return true;
}
Cursor c1,c2;
};
class MaskCursor {
public:
MaskCursor (const ASparseBitVector &adoptVector,
const ASparseBitVector &maskVector) :
c(adoptVector), m(maskVector) {}
MaskCursor (const MaskCursor &adoptCursor) :
c(adoptCursor.c), m(adoptCursor.m) {}
// ~Cursor(); // destructor intentionally omitted
bool SetToFirstOne() {
c.SetToFirstOne();
m.SetToFirstOne();
if (c.Valid()) return FindNextMaskedItem();
return false;
}
bool SetToNextOne() {
if (c.SetToNextOne()) return FindNextMaskedItem();
return false;
}
bool SetToNextOneAfter(SparseBitIndex);
bool SetToPreviousOne();
bool Valid() const {
return c.Valid();
}
operator SparseBitIndex() const {
return c;
}
IntersectionCursor &operator= (SparseBitIndex);
private:
bool FindNextMaskedItem() {
if (m.Valid()) {
while (c>=m) {
if (c==m) {
if (!c.SetToNextOne()) return false;
}
if (!m.SetToNextOneAfter(c-1))
break;
}
}
return true;
}
Cursor c,m;
};
template <class A2>
friend class ASparseBitVector;
template <class A2>
friend
inline void Swap(ASparseBitVector<A2> &vectorA, ASparseBitVector<A2> &vectorB);
};
// SparseBitRef methods
template <class Allocator>
inline ASparseBitVector<Allocator>::SparseBitRef::operator bool() {
return fVector.ValueAt(fIndex);
}
template <class Allocator>
inline typename ASparseBitVector<Allocator>::SparseBitRef& ASparseBitVector<Allocator>::SparseBitRef::operator= (bool bitValue) {
CS2Assert (bitValue == 0 || bitValue == 1, ("Incorrect bool value"));
ASparseBitVector<Allocator> &bitArray = fVector;
if (bitValue) return Set();
else return Clear();
}
template <class Allocator>
inline typename ASparseBitVector<Allocator>::SparseBitRef& ASparseBitVector<Allocator>::SparseBitRef::Set() {
size_t i=0, n = fVector.fNumberOfSegments;
uint16_t highBits = fIndex>>16;
Segment *base;
if (n) {
base = fVector.fBase;
for (i=0; i< n; i++) {
if (base[i].fHighBits >= highBits) {
if (base[i].fHighBits == highBits) {
fVector.SetSegment(base[i], fIndex);
return *this;
}
break;
}
}
base = (Segment *)fVector.reallocate((n+1)*sizeof(Segment),
base, n*sizeof(Segment));
memmove(&base[i+1],&base[i],(n-i)*sizeof(Segment));
} else {
base = (Segment *)fVector.allocate((n+1)*sizeof(Segment));
}
base[i].allocate(sizeof(size_t)/sizeof(uint16_t), fVector);
base[i].fNumValues=1;
base[i].fHighBits=highBits;
base[i].Indices()[0]=fIndex;
fVector.fBase = base;
fVector.fNumberOfSegments=static_cast<SparseBitIndex>(n+1);
return *this;
}
template <class Allocator>
inline typename ASparseBitVector<Allocator>::SparseBitRef& ASparseBitVector<Allocator>::SparseBitRef::Clear () {
typename ASparseBitVector<Allocator>::Segment *segment = fVector.FindSegment(fIndex);
if (segment) {
fVector.ClearSegment(*segment, fIndex);
if (segment->IsZero()) { fVector.RemoveSegment(fIndex); }
}
return *this;
}
// ASparseBitVector<Allocator>::operator= (ASparseBitVector)
//
// Assign a sparse bit vector to another.
template <class Allocator>
inline ASparseBitVector<Allocator> &
ASparseBitVector<Allocator>::operator= (const ASparseBitVector<Allocator> &vector) {
if (vector.IsNull())
ClearToNull();
else
Clear();
if (vector.fNumberOfSegments) {
fNumberOfSegments = vector.fNumberOfSegments;
fBase = (Segment *)Allocator::allocate(fNumberOfSegments * sizeof(Segment));
SparseBitIndex i;
for (i=0; i< fNumberOfSegments; i++)
fBase[i].copy(vector.fBase[i], *this);
}
return *this;
}
//
// Swap one sparse bit vector with another.
template <class Allocator>
inline void Swap(ASparseBitVector<Allocator> &vectorA, ASparseBitVector<Allocator> &vectorB) {
typename ASparseBitVector<Allocator>::Segment *base;
SparseBitIndex numberOfSegments;
base = vectorA.fBase;
numberOfSegments = vectorA.fNumberOfSegments;
vectorA.fBase = vectorB.fBase;
vectorA.fNumberOfSegments = vectorB.fNumberOfSegments;
vectorB.fBase = base;
vectorB.fNumberOfSegments = numberOfSegments;
}
template <class Allocator>
template <class A2>
inline ASparseBitVector<Allocator> &
ASparseBitVector<Allocator>::operator= (const ASparseBitVector<A2> &vector) {
if (vector.IsNull())
ClearToNull();
else
Clear();
if (vector.fNumberOfSegments) {
fNumberOfSegments = vector.fNumberOfSegments;
fBase = (Segment *)Allocator::allocate(fNumberOfSegments * sizeof(Segment));
memset(fBase, 0, fNumberOfSegments * sizeof(Segment));
SparseBitIndex i;
for (i=0; i< fNumberOfSegments; i++)
fBase[i].copy(vector.fBase[i], *this);
}
return *this;
}
template <class Allocator>
template <class B2>
inline ASparseBitVector<Allocator> &
ASparseBitVector<Allocator>::operator= (const B2 &vector) {
Clear();
typename B2::Cursor c1(vector), c2(vector);
c1.SetToFirstOne();
c2.SetToFirstOne();
while (c1.Valid()) {
// new segment
SparseBitIndex highbits = c1 &~kSegmentMask, count=1;
for (c1.SetToNextOne();
c1.Valid() && ((c1&~kSegmentMask) == highbits);
c1.SetToNextOne()) {
count++;
}
Segment *s = AddSegment(highbits, count);
uint16_t *sp = s->Indices();
s->fNumValues = count;
while (count > 0) {
*sp++ = c2;
c2.SetToNextOne();
count-=1;
}
}
return *this;
}
// ASparseBitVector<Allocator>::Compact
// Release any unused memory
template <class Allocator>
inline void ASparseBitVector<Allocator>::Compact() {
size_t i;
for (i=0; i< fNumberOfSegments; i++)
CompactSegment(fBase[i]);
}
// ASparseBitVector<Allocator>::operator[]
//
// Indexing operator, grow if necessary
template <class Allocator>
inline typename ASparseBitVector<Allocator>::SparseBitRef ASparseBitVector<Allocator>::operator[] (size_t index) {
return SparseBitRef (*this, static_cast<SparseBitIndex>(index));
}
template <class Allocator>
inline bool ASparseBitVector<Allocator>::operator[] (size_t index) const {
return ValueAt(index);
}
template <class Allocator>
inline bool
ASparseBitVector<Allocator>::ValueAt(size_t elementIndex) const
{
Segment *s =FindSegment(static_cast<SparseBitIndex>(elementIndex));
if (s) return GetSegment(*s, static_cast<SparseBitIndex>(elementIndex));
return false;
}
template <class Allocator>
inline SparseBitIndex
ASparseBitVector<Allocator>::FirstOne() const
{
CS2Assert(!IsZero(), ("Could not find any 1-bits looking for FirstOne"));
Segment &s = fBase[0];
return s.HighBits() | s.Indices()[0];
}
template <class Allocator>
inline SparseBitIndex
ASparseBitVector<Allocator>::LastOne() const
{
CS2Assert(!IsZero(), ("Could not find any 1-bits looking for LastOne"));
Segment &s = fBase[fNumberOfSegments-1];
return s.HighBits() | s.Indices()[s.fNumValues-1];
}
template <class Allocator>
inline SparseBitIndex
ASparseBitVector<Allocator>::ClearLastOneIfThereIsOneInRange(SparseBitIndex low, SparseBitIndex high, bool& foundone)
{
if (IsZero()) {
foundone = false;
return ~(SparseBitIndex) 0;
} else {
Segment &s = fBase[fNumberOfSegments-1];
SparseBitIndex ret = s.HighBits() | s.Indices()[s.fNumValues-1];
CS2Assert(ret <= high, ("sparsebitvector and tableof disagree on highbounds"));
if (s.fNumValues<=1)
RemoveSegmentAt(fNumberOfSegments-1);
else
s.fNumValues-=1;
foundone = true;
return ret;
}
}
// ASparseBitVector<Allocator>::GrowTo
//
// A growth routine to maintain compatibility with the dense bit vector
// class. This routine does nothing except make non-null.
template <class Allocator>
inline void ASparseBitVector<Allocator>::GrowTo (SparseBitIndex index, bool geometric, bool forceGeometric) {
if (IsZero()) {
Clear();
return;
}
}
template <class Allocator>
inline void ASparseBitVector<Allocator>::Clear () {
if (IsZero()) {
// unallocated, but non-null
fBase = (Segment *) 1;
return;
}
size_t i, n = fNumberOfSegments;
for (i=0; i<n; i++) {
Segment *s = &fBase[i];
s->deallocate(*this);
}
Allocator::deallocate(fBase, sizeof(Segment)*fNumberOfSegments);
fNumberOfSegments=0;
// unallocated, but non-null
fBase = (Segment *) 1;
}
template <class Allocator>
inline void ASparseBitVector<Allocator>::ClearToNull () {
Clear();
fBase=NULL;
}
template <class Allocator>
inline ASparseBitVector<Allocator>::Cursor::Cursor (const ASparseBitVector<Allocator> &adoptVector) :
fVector(adoptVector) {}
template <class Allocator>
inline ASparseBitVector<Allocator>::Cursor::Cursor (const typename ASparseBitVector<Allocator>::Cursor &adoptCursor) :
fVector(adoptCursor.fVector),
fSegment(adoptCursor.fSegment),
fSegmentPopCount(adoptCursor.fSegmentPopCount),
fSegmentHighBits(adoptCursor.fSegmentHighBits),
fSegmentIndex(adoptCursor.fSegmentIndex),
fSegmentOffset(adoptCursor.fSegmentOffset) {}
template <class Allocator>
inline bool ASparseBitVector<Allocator>::Cursor::Valid () const {
return fSegmentIndex < fVector.fNumberOfSegments;
}
template <class Allocator>
inline ASparseBitVector<Allocator>::Cursor::operator SparseBitIndex() const {
CS2Assert(Valid(), ("Expecting valid cursor"));
return fSegmentHighBits | fSegment[fSegmentOffset];
}
template <class Allocator>
inline void ASparseBitVector<Allocator>::Cursor::SetToFirstOne() {
fSegmentIndex=0;
fSegmentOffset=0;
if (Valid()) {
Segment &s=fVector.fBase[fSegmentIndex];
fSegment = s.Indices();
fSegmentPopCount = s.PopulationCount()-1;
fSegmentHighBits = s.HighBits();
}
}
template <class Allocator>
inline bool ASparseBitVector<Allocator>::Cursor::SetToNextOne() {
if (fSegmentOffset < fSegmentPopCount) {
// Same segment;
fSegmentOffset+=1;
return true;
}
CS2Assert(Valid(), ("Cannot advance an invalid cursor"));
CS2Assert(fSegmentPopCount == fVector.fBase[fSegmentIndex].PopulationCount()-1,
("ASparseBitVector updated durign traversal %d %d", fSegmentPopCount, fVector.fBase[fSegmentIndex].PopulationCount()-1));
fSegmentIndex+=1;
if (!Valid()) return false;
fSegmentOffset=0;
Segment &s=fVector.fBase[fSegmentIndex];
fSegment = s.Indices();
fSegmentPopCount = s.PopulationCount()-1;
fSegmentHighBits = s.HighBits();
return true;
}
template <class Allocator>
inline bool ASparseBitVector<Allocator>::Cursor::SetToPreviousOne() {
CS2Assert(Valid(), ("Cannot advance an invalid cursor"));
if (fSegmentOffset > 0) {
// Same segment;
fSegmentOffset-=1;
return true;
}
else {
fSegmentIndex-=1;
if (!Valid()) return false;
Segment &s=fVector.fBase[fSegmentIndex];
fSegment = s.Indices();
fSegmentPopCount = s.PopulationCount()-1;
fSegmentHighBits = s.HighBits();
fSegmentOffset = fSegmentPopCount;
return true;
}
}
template <class Allocator>
inline bool ASparseBitVector<Allocator>::Cursor::SetToNextOneAfter(SparseBitIndex next) {
CS2Assert(Valid(), ("Cannot advance an invalid cursor"));
// Find one more than next
next+=1;
// Find segment
SparseBitIndex index=fSegmentOffset;
SparseBitIndex nexthi = next & ~kSegmentMask;
SparseBitIndex nextlo = next & kSegmentMask;
while (fSegmentHighBits < nexthi) {
fSegmentIndex+=1;
if (!Valid()) return false;
index=0;
Segment &s = fVector.fBase[fSegmentIndex];
fSegment = s.Indices();
fSegmentPopCount = s.PopulationCount()-1;
fSegmentHighBits = s.HighBits();
}
if (fSegmentHighBits == nexthi) {
Segment &s = fVector.fBase[fSegmentIndex];
index = fVector.AdvanceIndex(s, nextlo, index);
if (index > fSegmentPopCount) {
fSegmentIndex+=1;
if (!Valid()) return false;
index=0;
Segment &s = fVector.fBase[fSegmentIndex];
fSegment = s.Indices();
fSegmentPopCount = s.PopulationCount()-1;
fSegmentHighBits = s.HighBits();
}
} else
index=0;
fSegmentOffset=index;
return true;
}
template <class Allocator>
inline void ASparseBitVector<Allocator>::Cursor::SetToLastOne() {
if (fVector.fNumberOfSegments>0) {
fSegmentIndex=fVector.fNumberOfSegments-1;
Segment &s = fVector.fBase[fSegmentIndex];
fSegment=s.Indices();
fSegmentPopCount=fSegmentOffset=s.PopulationCount()-1;
fSegmentHighBits=s.HighBits();
} else
fSegmentIndex=0;
}
// Return the number of '1' bits in the vector. Optionally specify the
// number of leading bits to examine.
template <class Allocator>
inline SparseBitIndex ASparseBitVector<Allocator>::PopulationCount (SparseBitIndex numBits) const{
CS2Assert(numBits==0xEFFFFFFFul,("Population count subset not implemented"));
size_t i, n = fNumberOfSegments;
SparseBitIndex ret=0;
for (i=0; i<n; i++) {
Segment *s = &fBase[i];
ret += s->PopulationCount();
}
return ret;
}
template <class Allocator>
inline SparseBitIndex ASparseBitVector<Allocator>::PopulationCount (const ASparseBitVector<Allocator> &mask) const{
SparseBitIndex ret=0;
typename ASparseBitVector<Allocator>::IntersectionCursor c(*this, mask);
for (c.SetToFirstOne(); c.Valid(); c.SetToNextOne())
ret+=1;
return ret;
}
// ASparseBitVector<Allocator>::SizeInBits
//
// Return the number of bits "allocated" in this vector.
template <class Allocator>
inline SparseBitIndex ASparseBitVector<Allocator>::SizeInBits() const {
return PopulationCount();
}
// ASparseBitVector<Allocator>::IsZero
//
// Determine if this vector is zero
template <class Allocator>
inline bool ASparseBitVector<Allocator>::IsZero() const {
return fNumberOfSegments==0;
}
// ASparseBitVector<Allocator>::IsNull
//
// Determine if this vector is null (and also is zero)
template <class Allocator>
inline bool ASparseBitVector<Allocator>::IsNull() const {
return (fNumberOfSegments==0) && (fBase == NULL);
}
template <class Allocator>
inline typename ASparseBitVector<Allocator>::Segment *ASparseBitVector<Allocator>::FindSegment(SparseBitIndex index) const {
size_t i, n = fNumberOfSegments;
for (i=0; i< n; i++) {
if (fBase[i].fHighBits >= index>>16) {
if (fBase[i].fHighBits == index>>16) return &fBase[i];
return NULL;
}
}
return NULL;
}
template <class Allocator>
inline
bool ASparseBitVector<Allocator>::Intersects (const ASparseBitVector<Allocator> &inputVector) const{
size_t i1=0,i2=0,n1 = fNumberOfSegments, n2 = inputVector.fNumberOfSegments;
if (n1==0 || n2==0) return false;