Debian Codegraph @

bitvector128.go

raw symbols SCIP
1 package bitfieldscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/
2
3 import (
4 "encoding/binaryscip-go gomod github.com/golang/go/src go1.21 `encoding/binary`/"
5 "math/bitsscip-go gomod github.com/golang/go/src go1.21 `math/bits`/"
6 )
7
8 var _ = Bitfieldscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitfield#(Bitvector128scip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#{})
9
10 // Bitvector128 is a bitfield with a fixed defined size of 128. There is no length bit
11 // present in the underlying byte array.
12 type Bitvector128scip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128# []byte
13
14 const bitvector128ByteSizescip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/bitvector128ByteSize. = 16
15 const bitvector128BitSizescip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/bitvector128BitSize. = bitvector128ByteSizescip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/bitvector128ByteSize. * 8
16
17 // NewBitvector128 creates a new bitvector of size 128.
18 func NewBitvector128scip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/NewBitvector128().() Bitvector128scip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128# {
19 byteArray := [bitvector128ByteSizescip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/bitvector128ByteSize.]byte{}
20 return byteArray[:]
21 }
22
23 // BitAt returns the bit value at the given index. If the index requested
24 // exceeds the number of bits in the bitvector, then this method returns false.
25 func (b Bitvector128scip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#) BitAtscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#BitAt().(idx uint64) bool {
26 // Out of bounds, must be false.
27 if idx >= b.Lenscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#Len().() || len(b) != bitvector128ByteSizescip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/bitvector128ByteSize. {
28 return false
29 }
30
31 i := uint8(1 << (idx % 8))
32 return b[idx/8]&i == i
33 }
34
35 // SetBitAt will set the bit at the given index to the given value. If the index
36 // requested exceeds the number of bits in the bitvector, then this method returns
37 // false.
38 func (b Bitvector128scip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#) SetBitAtscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#SetBitAt().(idx uint64, val bool) {
39 // Out of bounds, do nothing.
40 if idx >= b.Lenscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#Len().() || len(b) != bitvector128ByteSizescip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/bitvector128ByteSize. {
41 return
42 }
43
44 bit := uint8(1 << (idx % 8))
45 if val {
46 b[idx/8] |= bit
47 } else {
48 b[idx/8] &^= bit
49 }
50 }
51
52 // Len returns the number of bits in the bitvector.
53 func (b Bitvector128scip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#) Lenscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#Len().() uint64 {
54 return bitvector128BitSizescip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/bitvector128BitSize.
55 }
56
57 // Count returns the number of 1s in the bitvector.
58 func (b Bitvector128scip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#) Countscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#Count().() uint64 {
59 if len(b) == 0 {
60 return 0
61 }
62 c := 0
63 for i, bt := range b {
64 if i >= bitvector128ByteSizescip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/bitvector128ByteSize. {
65 break
66 }
67 c += bitsscip-go gomod github.com/golang/go/src go1.21 `math/bits`/.OnesCount8scip-go gomod github.com/golang/go/src go1.21 `math/bits`/OnesCount8().(bt)
68 }
69 return uint64(c)
70 }
71
72 // Bytes returns the bytes data representing the Bitvector128.
73 func (b Bitvector128scip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#) Bytesscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#Bytes().() []byte {
74 if len(b) == 0 {
75 return []byte{}
76 }
77 ln := minscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/min().(len(b), bitvector128ByteSizescip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/bitvector128ByteSize.)
78 ret := make([]byte, ln)
79 copy(ret, b[:ln])
80 return ret[:]
81 }
82
83 // Shift bitvector by i. If i >= 0, perform left shift, otherwise right shift.
84 func (b Bitvector128scip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#) Shiftscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#Shift().(i int) {
85 if len(b) == 0 {
86 return
87 }
88
89 // Shifting greater than 128 bits is pointless and can have unexpected behavior.
90 if i > bitvector128BitSizescip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/bitvector128BitSize. {
91 i = bitvector128BitSizescip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/bitvector128BitSize.
92 } else if i < -bitvector128BitSizescip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/bitvector128BitSize. {
93 i = -bitvector128BitSizescip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/bitvector128BitSize.
94 }
95 if i >= 0 {
96 num := binaryscip-go gomod github.com/golang/go/src go1.21 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.21 `encoding/binary`/BigEndian..Uint64scip-go gomod github.com/golang/go/src go1.21 `encoding/binary`/bigEndian#Uint64().(b)
97 num <<= uint8(i)
98 binaryscip-go gomod github.com/golang/go/src go1.21 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.21 `encoding/binary`/BigEndian..PutUint64scip-go gomod github.com/golang/go/src go1.21 `encoding/binary`/bigEndian#PutUint64().(b, num)
99 } else {
100 num := binaryscip-go gomod github.com/golang/go/src go1.21 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.21 `encoding/binary`/BigEndian..Uint64scip-go gomod github.com/golang/go/src go1.21 `encoding/binary`/bigEndian#Uint64().(b)
101 num >>= uint8(i * -1)
102 binaryscip-go gomod github.com/golang/go/src go1.21 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.21 `encoding/binary`/BigEndian..PutUint64scip-go gomod github.com/golang/go/src go1.21 `encoding/binary`/bigEndian#PutUint64().(b, num)
103 }
104 }
105
106 // BitIndices returns the list of indices that are set to 1.
107 func (b Bitvector128scip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#) BitIndicesscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#BitIndices().() []int {
108 indices := make([]int, 0, bitvector128BitSizescip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/bitvector128BitSize.)
109 for i, bt := range b {
110 if i >= bitvector128ByteSizescip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/bitvector128ByteSize. {
111 break
112 }
113 for j := 0; j < 8; j++ {
114 bit := byte(1 << uint(j))
115 if bt&bit == bit {
116 indices = append(indices, i*8+j)
117 }
118 }
119 }
120
121 return indices
122 }
123
124 // Contains returns true if the bitlist contains all of the bits from the provided argument
125 // bitlist. This method will return an error if bitlists are not the same length.
126 func (b Bitvector128scip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#) Containsscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#Contains().(c Bitvector128scip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#) (bool, error) {
127 if b.Lenscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#Len().() != c.Lenscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#Len().() {
128 return false, ErrBitvectorDifferentLengthscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/ErrBitvectorDifferentLength.
129 }
130
131 // To ensure all of the bits in c are present in b, we iterate over every byte, combine
132 // the byte from b and c, then XOR them against b. If the result of this is non-zero, then we
133 // are assured that a byte in c had bits not present in b.
134 for i := 0; i < len(b); i++ {
135 if b[i]^(b[i]|c[i]) != 0 {
136 return false, nil
137 }
138 }
139
140 return true, nil
141 }
142
143 // Overlaps returns true if the bitlist contains one of the bits from the provided argument
144 // bitlist. This method will return an error if bitlists are not the same length.
145 func (b Bitvector128scip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#) Overlapsscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#Overlaps().(c Bitvector128scip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#) (bool, error) {
146 lenB, lenC := b.Lenscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#Len().(), c.Lenscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#Len().()
147 if b.Lenscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#Len().() != c.Lenscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#Len().() {
148 return false, ErrBitvectorDifferentLengthscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/ErrBitvectorDifferentLength.
149 }
150
151 if lenB == 0 || lenC == 0 {
152 return false, nil
153 }
154
155 // To ensure all of the bits in c are not overlapped in b, we iterate over every byte, invert b
156 // and xor the byte from b and c, then and it against c. If the result is non-zero, then
157 // we can be assured that byte in c had bits not overlapped in b.
158 for i := 0; i < len(b); i++ {
159 // If this byte is the last byte in the array, mask the length bit.
160 mask := uint8(0xFF)
161
162 if (^b[i]^c[i])&c[i]&mask != 0 {
163 return true, nil
164 }
165 }
166 return false, nil
167 }
168
169 // Or returns the OR result of the two bitfields. This method will return an error if the bitlists are not the same length.
170 func (b Bitvector128scip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#) Orscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#Or().(c Bitvector128scip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#) (Bitvector128scip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#, error) {
171 if b.Lenscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#Len().() != c.Lenscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector128#Len().() {
172 return nil, ErrBitvectorDifferentLengthscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/ErrBitvectorDifferentLength.
173 }
174
175 ret := make([]byte, len(b))
176 for i := 0; i < len(b); i++ {
177 ret[i] = b[i] | c[i]
178 }
179
180 return ret, nil
181 }