@@ -71,6 +71,10 @@ func andSparseWithSparseBitArray(sba, other *sparseBitArray) BitArray {
71
71
}
72
72
73
73
func andSparseWithDenseBitArray (sba * sparseBitArray , other * bitArray ) BitArray {
74
+ if other .IsEmpty () {
75
+ return newSparseBitArray ()
76
+ }
77
+
74
78
// Use a duplicate of the sparse array to store the results of the
75
79
// bitwise and. More memory-efficient than allocating a new dense bit
76
80
// array.
@@ -83,14 +87,14 @@ func andSparseWithDenseBitArray(sba *sparseBitArray, other *bitArray) BitArray {
83
87
84
88
// Run through the sparse array and attempt comparisons wherever
85
89
// possible against the dense bit array.
86
- for selfIndex , selfValue := range sba .indices {
90
+ for selfIndex , selfValue := range ba .indices {
87
91
88
92
if selfValue >= uint64 (len (other .blocks )) {
89
93
// The dense bit array has been exhausted. This is the
90
94
// annoying case because we have to trim the sparse
91
95
// array to the size of the dense array.
92
- ba .blocks = ba .blocks [:selfIndex ]
93
- ba .indices = ba .indices [:selfIndex ]
96
+ ba .blocks = ba .blocks [:selfIndex - 1 ]
97
+ ba .indices = ba .indices [:selfIndex - 1 ]
94
98
95
99
// once this is done, there are no more comparisons.
96
100
// We're ready to return
@@ -99,9 +103,15 @@ func andSparseWithDenseBitArray(sba *sparseBitArray, other *bitArray) BitArray {
99
103
ba .blocks [selfIndex ] = ba .blocks [selfIndex ].and (
100
104
other .blocks [selfValue ])
101
105
102
- if ba .blocks [selfIndex ] == 0 {
103
- ba .blocks .deleteAtIndex (int64 (selfIndex ))
104
- ba .indices .deleteAtIndex (int64 (selfIndex ))
106
+ }
107
+
108
+ // Ensure any zero'd blocks in the resulting sparse
109
+ // array are deleted
110
+ for i := 0 ; i < len (ba .blocks ); i ++ {
111
+ if ba .blocks [i ] == 0 {
112
+ ba .blocks .deleteAtIndex (int64 (i ))
113
+ ba .indices .deleteAtIndex (int64 (i ))
114
+ i --
105
115
}
106
116
}
107
117
0 commit comments