github.com/djordje200179/extendedlibrary/datastructures@v1.7.1-0.20240227175559-d09520a92dd4/cols/bitarray/reduce_operations.go (about) 1 package bitarray 2 3 import "math/bits" 4 5 // All returns true if all bits are set to 1. 6 func (array *Array) All() bool { 7 sliceSize := len(array.slice) 8 if array.lastElemOff != 0 { 9 lastElementMask := uint8(0xFF) >> (8 - array.lastElemOff) 10 lastElement := array.slice[sliceSize-1] 11 12 if lastElement != lastElementMask { 13 return false 14 } 15 16 sliceSize-- 17 } 18 19 for i := range sliceSize - 1 { 20 if array.slice[i] != 0xFF { 21 return false 22 } 23 } 24 25 return true 26 } 27 28 // Any returns true if any bit is set to 1. 29 func (array *Array) Any() bool { 30 sliceSize := len(array.slice) 31 if array.lastElemOff != 0 { 32 lastElementMask := uint8(0xFF) >> (8 - array.lastElemOff) 33 lastElement := array.slice[sliceSize-1] 34 35 if lastElement&lastElementMask != 0 { 36 return true 37 } 38 39 sliceSize-- 40 } 41 42 for i := range sliceSize - 1 { 43 if array.slice[i] != 0 { 44 return true 45 } 46 } 47 48 return false 49 } 50 51 // None returns true if no bit is set to 1. 52 func (array *Array) None() bool { 53 sliceSize := len(array.slice) 54 if array.lastElemOff != 0 { 55 lastElementMask := uint8(0xFF) >> (8 - array.lastElemOff) 56 lastElement := array.slice[sliceSize-1] 57 58 if lastElement&lastElementMask != 0 { 59 return false 60 } 61 62 sliceSize-- 63 } 64 65 for i := range sliceSize - 1 { 66 if array.slice[i] != 0 { 67 return false 68 } 69 } 70 71 return true 72 } 73 74 // Count returns the number of bits set to 1. 75 func (array *Array) Count() int { 76 count := 0 77 78 sliceSize := len(array.slice) 79 if array.lastElemOff != 0 { 80 lastElementMask := uint8(0xFF) >> (8 - array.lastElemOff) 81 lastElement := array.slice[sliceSize-1] 82 83 count += bits.OnesCount8(lastElement & lastElementMask) 84 85 sliceSize-- 86 } 87 88 for i := range sliceSize - 1 { 89 elem := array.slice[i] 90 91 count += bits.OnesCount8(elem) 92 } 93 94 return count 95 }