| 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#(Bitvector512scip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector512#{}) |
| 9 | |
| 10 | // Bitvector512 is a bitfield with a fixed defined size of 512. There is no length bit |
| 11 | // present in the underlying byte array. |
| 12 | type Bitvector512scip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector512# []byte |
| 13 | |
| 14 | const bitvector512ByteSizescip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/bitvector512ByteSize. = 64 |
| 15 | const bitvector512BitSizescip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/bitvector512BitSize. = bitvector512ByteSizescip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/bitvector512ByteSize. * 8 |
| 16 | |
| 17 | // NewBitvector512 creates a new bitvector of size 512. |
| 18 | func NewBitvector512scip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/NewBitvector512().() Bitvector512scip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector512# { |
| 19 | byteArray := [bitvector512ByteSizescip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/bitvector512ByteSize.]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 Bitvector512scip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector512#) BitAtscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector512#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`/Bitvector512#Len().() || len(b) != bitvector512ByteSizescip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/bitvector512ByteSize. { |
| 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 Bitvector512scip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector512#) SetBitAtscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector512#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`/Bitvector512#Len().() || len(b) != bitvector512ByteSizescip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/bitvector512ByteSize. { |
| 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 Bitvector512scip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector512#) Lenscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector512#Len().() uint64 { |
| 54 | return bitvector512BitSizescip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/bitvector512BitSize. |
| 55 | } |
| 56 | |
| 57 | // Count returns the number of 1s in the bitvector. |
| 58 | func (b Bitvector512scip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector512#) Countscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector512#Count().() uint64 { |
| 59 | if len(b) == 0 { |
| 60 | return 0 |
| 61 | } |
| 62 | c := 0 |
| 63 | for i, bt := range b { |
| 64 | if i >= bitvector512ByteSizescip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/bitvector512ByteSize. { |
| 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 Bitvector512. |
| 73 | func (b Bitvector512scip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector512#) Bytesscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector512#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), bitvector512ByteSizescip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/bitvector512ByteSize.) |
| 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 Bitvector512scip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector512#) Shiftscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector512#Shift().(i int) { |
| 85 | if len(b) == 0 { |
| 86 | return |
| 87 | } |
| 88 | |
| 89 | // Shifting greater than 1024 bits is pointless and can have unexpected behavior. |
| 90 | if i > bitvector512BitSizescip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/bitvector512BitSize. { |
| 91 | i = bitvector512BitSizescip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/bitvector512BitSize. |
| 92 | } else if i < -bitvector512BitSizescip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/bitvector512BitSize. { |
| 93 | i = -bitvector512BitSizescip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/bitvector512BitSize. |
| 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 Bitvector512scip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector512#) BitIndicesscip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/Bitvector512#BitIndices().() []int { |
| 108 | indices := make([]int, 0, bitvector512BitSizescip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/bitvector512BitSize.) |
| 109 | for i, bt := range b { |
| 110 | if i >= bitvector512ByteSizescip-go gomod github.com/OffchainLabs/go-bitfield . `github.com/OffchainLabs/go-bitfield`/bitvector512ByteSize. { |
| 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 | } |