This repository has been archived by the owner on Jun 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reduce_operations.go
95 lines (72 loc) · 1.71 KB
/
reduce_operations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package bitarray
import "math/bits"
// All returns true if all bits are set to 1.
func (array *Array) All() bool {
sliceSize := len(array.slice)
if array.lastElemOff != 0 {
lastElementMask := uint8(0xFF) >> (8 - array.lastElemOff)
lastElement := array.slice[sliceSize-1]
if lastElement != lastElementMask {
return false
}
sliceSize--
}
for i := 0; i < sliceSize-1; i++ {
if array.slice[i] != 0xFF {
return false
}
}
return true
}
// Any returns true if any bit is set to 1.
func (array *Array) Any() bool {
sliceSize := len(array.slice)
if array.lastElemOff != 0 {
lastElementMask := uint8(0xFF) >> (8 - array.lastElemOff)
lastElement := array.slice[sliceSize-1]
if lastElement&lastElementMask != 0 {
return true
}
sliceSize--
}
for i := 0; i < sliceSize-1; i++ {
if array.slice[i] != 0 {
return true
}
}
return false
}
// None returns true if no bit is set to 1.
func (array *Array) None() bool {
sliceSize := len(array.slice)
if array.lastElemOff != 0 {
lastElementMask := uint8(0xFF) >> (8 - array.lastElemOff)
lastElement := array.slice[sliceSize-1]
if lastElement&lastElementMask != 0 {
return false
}
sliceSize--
}
for i := 0; i < sliceSize-1; i++ {
if array.slice[i] != 0 {
return false
}
}
return true
}
// Count returns the number of bits set to 1.
func (array *Array) Count() int {
count := 0
sliceSize := len(array.slice)
if array.lastElemOff != 0 {
lastElementMask := uint8(0xFF) >> (8 - array.lastElemOff)
lastElement := array.slice[sliceSize-1]
count += bits.OnesCount8(lastElement & lastElementMask)
sliceSize--
}
for i := 0; i < sliceSize-1; i++ {
elem := array.slice[i]
count += bits.OnesCount8(elem)
}
return count
}