Skip to content

Commit 8ba3709

Browse files
Merge pull request Workiva#199 from bcicen/fix-170
RM-30900 Drop zero-block values in sparse vs dense AND
2 parents f338bf4 + ca2ef6d commit 8ba3709

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

bitarray/and.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ func andSparseWithSparseBitArray(sba, other *sparseBitArray) BitArray {
7171
}
7272

7373
func andSparseWithDenseBitArray(sba *sparseBitArray, other *bitArray) BitArray {
74+
if other.IsEmpty() {
75+
return newSparseBitArray()
76+
}
77+
7478
// Use a duplicate of the sparse array to store the results of the
7579
// bitwise and. More memory-efficient than allocating a new dense bit
7680
// array.
@@ -89,15 +93,26 @@ func andSparseWithDenseBitArray(sba *sparseBitArray, other *bitArray) BitArray {
8993
// The dense bit array has been exhausted. This is the
9094
// annoying case because we have to trim the sparse
9195
// 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]
9498

9599
// once this is done, there are no more comparisons.
96100
// We're ready to return
97101
break
98102
}
99103
ba.blocks[selfIndex] = ba.blocks[selfIndex].and(
100104
other.blocks[selfValue])
105+
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--
115+
}
101116
}
102117

103118
return ba

0 commit comments

Comments
 (0)