File tree Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change @@ -421,18 +421,30 @@ func FromBitSet(bitset *bitset.BitSet) *Bitmap {
421
421
// ToArray creates a new slice containing all of the integers stored in the Bitmap in sorted order
422
422
func (rb * Bitmap ) ToArray () []uint32 {
423
423
array := make ([]uint32 , rb .GetCardinality ())
424
+ ar := rb .toArray (& array )
425
+ return * ar
426
+ }
427
+
428
+ func (rb * Bitmap ) toArray (array * []uint32 ) * []uint32 {
424
429
pos := 0
425
430
pos2 := 0
426
431
427
432
for pos < rb .highlowcontainer .size () {
428
433
hs := uint32 (rb .highlowcontainer .getKeyAtIndex (pos )) << 16
429
434
c := rb .highlowcontainer .getContainerAtIndex (pos )
430
435
pos ++
431
- pos2 = c .fillLeastSignificant16bits (array , pos2 , hs )
436
+ pos2 = c .fillLeastSignificant16bits (* array , pos2 , hs )
432
437
}
433
438
return array
434
439
}
435
440
441
+ // ToExistingArray stores all of the integers stored in the Bitmap in sorted order in the
442
+ // slice that is given to ToExistingArray. It is the callers duty to make sure the slice
443
+ // has the right size.
444
+ func (rb * Bitmap ) ToExistingArray (array * []uint32 ) * []uint32 {
445
+ return rb .toArray (array )
446
+ }
447
+
436
448
// GetSizeInBytes estimates the memory usage of the Bitmap. Note that this
437
449
// might differ slightly from the amount of bytes required for persistent storage
438
450
func (rb * Bitmap ) GetSizeInBytes () uint64 {
Original file line number Diff line number Diff line change @@ -1827,6 +1827,20 @@ func TestBitmap(t *testing.T) {
1827
1827
1828
1828
assert .True (t , valide )
1829
1829
})
1830
+
1831
+ t .Run ("ToExistingArray-Test" , func (t * testing.T ) {
1832
+ values := make ([]uint32 , 0 , 110 )
1833
+ rb := NewBitmap ()
1834
+
1835
+ for i := 10 ; i < 120 ; i ++ {
1836
+ values = append (values , uint32 (i ))
1837
+ }
1838
+ rb .AddMany (values )
1839
+ assert .Equal (t , values , rb .ToArray ())
1840
+ existing := make ([]uint32 , len (values ))
1841
+ buf := rb .ToExistingArray (& existing )
1842
+ assert .Equal (t , values , * buf )
1843
+ })
1830
1844
}
1831
1845
1832
1846
func TestXORtest4 (t * testing.T ) {
You can’t perform that action at this time.
0 commit comments