(sba, other *sparseBitArray)
| 17 | package bitarray |
| 18 | |
| 19 | func andSparseWithSparseBitArray(sba, other *sparseBitArray) BitArray { |
| 20 | max := maxInt64(int64(len(sba.indices)), int64(len(other.indices))) |
| 21 | indices := make(uintSlice, 0, max) |
| 22 | blocks := make(blocks, 0, max) |
| 23 | |
| 24 | selfIndex := 0 |
| 25 | otherIndex := 0 |
| 26 | var resultBlock block |
| 27 | |
| 28 | // move through the array and compare the blocks if they happen to |
| 29 | // intersect |
| 30 | for { |
| 31 | if selfIndex == len(sba.indices) || otherIndex == len(other.indices) { |
| 32 | // One of the arrays has been exhausted. We don't need |
| 33 | // to compare anything else for a bitwise and; the |
| 34 | // operation is complete. |
| 35 | break |
| 36 | } |
| 37 | |
| 38 | selfValue := sba.indices[selfIndex] |
| 39 | otherValue := other.indices[otherIndex] |
| 40 | |
| 41 | switch { |
| 42 | case otherValue < selfValue: |
| 43 | // The `sba` bitarray has a block with a index position |
| 44 | // greater than us. We want to compare with that block |
| 45 | // if possible, so move our `other` index closer to that |
| 46 | // block's index. |
| 47 | otherIndex++ |
| 48 | |
| 49 | case otherValue > selfValue: |
| 50 | // This is the exact logical inverse of the above case. |
| 51 | selfIndex++ |
| 52 | |
| 53 | default: |
| 54 | // Here, our indices match for both `sba` and `other`. |
| 55 | // Time to do the bitwise AND operation and add a block |
| 56 | // to our result list if the block has values in it. |
| 57 | resultBlock = sba.blocks[selfIndex].and(other.blocks[otherIndex]) |
| 58 | if resultBlock > 0 { |
| 59 | indices = append(indices, selfValue) |
| 60 | blocks = append(blocks, resultBlock) |
| 61 | } |
| 62 | selfIndex++ |
| 63 | otherIndex++ |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | return &sparseBitArray{ |
| 68 | indices: indices, |
| 69 | blocks: blocks, |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func andSparseWithDenseBitArray(sba *sparseBitArray, other *bitArray) BitArray { |
| 74 | if other.IsEmpty() { |
searching dependent graphs…