MCPcopy Create free account
hub / github.com/Workiva/go-datastructures / nandSparseWithSparseBitArray

Function nandSparseWithSparseBitArray

bitarray/nand.go:19–84  ·  view source on GitHub ↗
(sba, other *sparseBitArray)

Source from the content-addressed store, hash-verified

17package bitarray
18
19func nandSparseWithSparseBitArray(sba, other *sparseBitArray) BitArray {
20 // nand is an operation on the incoming array only, so the size will never
21 // be more than the incoming array, regardless of the size of the other
22 max := len(sba.indices)
23 indices := make(uintSlice, 0, max)
24 blocks := make(blocks, 0, max)
25
26 selfIndex := 0
27 otherIndex := 0
28 var resultBlock block
29
30 // move through the array and compare the blocks if they happen to
31 // intersect
32 for {
33 if selfIndex == len(sba.indices) {
34 // The bitarray being operated on is exhausted, so just return
35 break
36 } else if otherIndex == len(other.indices) {
37 // The other array is exhausted. In this case, we assume that we
38 // are calling nand on empty bit arrays, which is the same as just
39 // copying the value in the sba array
40 indices = append(indices, sba.indices[selfIndex])
41 blocks = append(blocks, sba.blocks[selfIndex])
42 selfIndex++
43 continue
44 }
45
46 selfValue := sba.indices[selfIndex]
47 otherValue := other.indices[otherIndex]
48
49 switch {
50 case otherValue < selfValue:
51 // The `sba` bitarray has a block with a index position
52 // greater than us. We want to compare with that block
53 // if possible, so move our `other` index closer to that
54 // block's index.
55 otherIndex++
56
57 case otherValue > selfValue:
58 // Here, the sba array has blocks that the other array doesn't
59 // have. In this case, we just copy exactly the sba array values
60 indices = append(indices, selfValue)
61 blocks = append(blocks, sba.blocks[selfIndex])
62
63 // This is the exact logical inverse of the above case.
64 selfIndex++
65
66 default:
67 // Here, our indices match for both `sba` and `other`.
68 // Time to do the bitwise AND operation and add a block
69 // to our result list if the block has values in it.
70 resultBlock = sba.blocks[selfIndex].nand(other.blocks[otherIndex])
71 if resultBlock > 0 {
72 indices = append(indices, selfValue)
73 blocks = append(blocks, resultBlock)
74 }
75 selfIndex++
76 otherIndex++

Callers 4

NandMethod · 0.85

Calls 1

nandMethod · 0.80

Used in the wild real call sites across dependent graphs

searching dependent graphs…