Documentation ¶
Overview ¶
Package bitfield is an abstraction type for bitfield operations.
A bitfield is also known as a Bitlist or BitvectorN in Ethereum 2.0 spec. Both variants are static arrays in that they cannot dynamically change in size after being constructed. These data types represent a list of bits whose value is treated akin to a boolean. The bits are in little endian order.
BitvectorN - A list of bits that is fixed in size. Bitlist - A list of bits that is determined at runtime.
The key difference between a bitvector and a bitlist is how they track the number of bits in the array. A bitvectorN is known to have N bits at compile time, so the length is always N no matter how the bitvector is instantiated. Whereas the bitlist can be created with size N at runtime. The bitlist uses the most significant bit in little endian order to indicate the start of the bitlist while in the byte representation.
Index ¶
- Variables
- type Bitfield
- type Bitlist
- func (b Bitlist) And(c Bitlist) (Bitlist, error)
- func (b Bitlist) BitAt(idx uint64) bool
- func (b Bitlist) BitIndices() []int
- func (b Bitlist) Bytes() []byte
- func (b Bitlist) BytesNoTrim() []byte
- func (b Bitlist) Contains(c Bitlist) (bool, error)
- func (b Bitlist) Count() uint64
- func (b Bitlist) Len() uint64
- func (b Bitlist) Not() Bitlist
- func (b Bitlist) Or(c Bitlist) (Bitlist, error)
- func (b Bitlist) Overlaps(c Bitlist) (bool, error)
- func (b Bitlist) SetBitAt(idx uint64, val bool)
- func (b Bitlist) ToBitlist64() (*Bitlist64, error)
- func (b Bitlist) Xor(c Bitlist) (Bitlist, error)
- type Bitlist64
- func (b *Bitlist64) And(c *Bitlist64) (*Bitlist64, error)
- func (b *Bitlist64) AndCount(c *Bitlist64) (uint64, error)
- func (b *Bitlist64) BitAt(idx uint64) bool
- func (b *Bitlist64) BitIndices() []int
- func (b *Bitlist64) Bytes() []byte
- func (b *Bitlist64) Clone() *Bitlist64
- func (b *Bitlist64) Contains(c *Bitlist64) (bool, error)
- func (b *Bitlist64) Count() uint64
- func (b *Bitlist64) Len() uint64
- func (b *Bitlist64) NoAllocAnd(c, ret *Bitlist64) error
- func (b *Bitlist64) NoAllocBitIndices(ret []int)
- func (b *Bitlist64) NoAllocNot(ret *Bitlist64)
- func (b *Bitlist64) NoAllocOr(c, ret *Bitlist64) error
- func (b *Bitlist64) NoAllocXor(c, ret *Bitlist64) error
- func (b *Bitlist64) Not() *Bitlist64
- func (b *Bitlist64) Or(c *Bitlist64) (*Bitlist64, error)
- func (b *Bitlist64) OrCount(c *Bitlist64) (uint64, error)
- func (b *Bitlist64) Overlaps(c *Bitlist64) (bool, error)
- func (b *Bitlist64) SetBitAt(idx uint64, val bool)
- func (b *Bitlist64) ToBitlist() Bitlist
- func (b *Bitlist64) Xor(c *Bitlist64) (*Bitlist64, error)
- func (b *Bitlist64) XorCount(c *Bitlist64) (uint64, error)
- type Bitvector128
- func (b Bitvector128) BitAt(idx uint64) bool
- func (b Bitvector128) BitIndices() []int
- func (b Bitvector128) Bytes() []byte
- func (b Bitvector128) Contains(c Bitvector128) (bool, error)
- func (b Bitvector128) Count() uint64
- func (b Bitvector128) Len() uint64
- func (b Bitvector128) Or(c Bitvector128) (Bitvector128, error)
- func (b Bitvector128) Overlaps(c Bitvector128) (bool, error)
- func (b Bitvector128) SetBitAt(idx uint64, val bool)
- func (b Bitvector128) Shift(i int)
- type Bitvector16
- func (b Bitvector16) BitAt(idx uint64) bool
- func (b Bitvector16) BitIndices() []int
- func (b Bitvector16) Bytes() []byte
- func (b Bitvector16) Contains(c Bitvector16) (bool, error)
- func (b Bitvector16) Count() uint64
- func (b Bitvector16) Len() uint64
- func (b Bitvector16) Or(c Bitvector16) (Bitvector16, error)
- func (b Bitvector16) Overlaps(c Bitvector16) (bool, error)
- func (b Bitvector16) SetBitAt(idx uint64, val bool)
- func (b Bitvector16) Shift(i int)
- type Bitvector256
- type Bitvector32
- type Bitvector4
- type Bitvector512
- type Bitvector64
- type Bitvector8
- func (b Bitvector8) BitAt(idx uint64) bool
- func (b Bitvector8) BitIndices() []int
- func (b Bitvector8) Bytes() []byte
- func (b Bitvector8) Contains(c Bitvector8) (bool, error)
- func (b Bitvector8) Count() uint64
- func (b Bitvector8) Len() uint64
- func (b Bitvector8) Or(c Bitvector8) (Bitvector8, error)
- func (b Bitvector8) Overlaps(c Bitvector8) (bool, error)
- func (b Bitvector8) SetBitAt(idx uint64, val bool)
Constants ¶
This section is empty.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type Bitfield ¶
type Bitfield interface { // BitAt returns true if the bit at the given index is 1. BitAt(idx uint64) bool // SetBitAt sets the bit at the given index to val. SetBitAt(idx uint64, val bool) // Len returns the length of the bitfield. Len() uint64 // Count returns the number of 1s in the bitfield. Count() uint64 // Bytes returns the bytes value of the bitfield, without the length bit. Bytes() []byte // BitIndices returns the indices which have a 1. BitIndices() []int }
Bitfield is the abstraction implemented by Bitlist or BitvectorN.
type Bitlist ¶
type Bitlist []byte
Bitlist is a bitfield implementation backed by an array of bytes. The most significant bit in the array of bytes indicates the start position of the bitfield.
Examples of the underlying byte array as bitlist:
byte{0b00001000} is a bitlist with 3 bits which are all zero. bits=[0,0,0] byte{0b00011111} is a bitlist with 4 bits which are all one. bits=[1,1,1,1] byte{0b00011000, 0b00000001} is a bitlist with 8 bits. bits=[0,0,0,1,1,0,0,0] byte{0b00011000, 0b00000010} is a bitlist with 9 bits. bits=[0,0,0,0,1,1,0,0,0]
func (Bitlist) And ¶
And returns the AND result of the two bitfields. This method will return an error if the bitlists are not the same length.
func (Bitlist) BitAt ¶
BitAt returns the bit value at the given index. If the index requested exceeds the number of bits in the bitlist, then this method returns false.
func (Bitlist) BitIndices ¶
BitIndices returns the list of indices that are set to 1.
func (Bitlist) Bytes ¶
Bytes returns the trimmed underlying byte array without the length bit. The leading zeros in the bitlist will be trimmed to the smallest byte length representation of the bitlist. This may produce an empty byte slice if all bits were zero.
func (Bitlist) BytesNoTrim ¶
BytesNoTrim returns the underlying byte array without the length bit. No trimming of leading zeros occurs, only size bit is cleared.
func (Bitlist) Contains ¶
Contains returns true if the bitlist contains all of the bits from the provided argument bitlist. This method will return an error if bitlists are not the same length.
func (Bitlist) Len ¶
Len of the bitlist returns the number of bits available in the underlying byte array.
func (Bitlist) Or ¶
Or returns the OR result of the two bitfields. This method will return an error if the bitlists are not the same length.
func (Bitlist) Overlaps ¶
Overlaps returns true if the bitlist contains one of the bits from the provided argument bitlist. This method will return an error if bitlists are not the same length.
func (Bitlist) SetBitAt ¶
SetBitAt will set the bit at the given index to the given value. If the index requested exceeds the number of bits in the bitlist, then this method returns false.
func (Bitlist) ToBitlist64 ¶
ToBitlist64 converts []byte backed bitlist into []uint64 backed bitlist.
type Bitlist64 ¶
type Bitlist64 struct {
// contains filtered or unexported fields
}
Bitlist64 is a bitfield implementation backed by an array of uint64.
func NewBitlist64 ¶
NewBitlist64 creates a new bitlist of size `n`.
func NewBitlist64From ¶
NewBitlist64From creates a new bitlist for a given uint64 array.
func NewBitlist64FromBytes ¶
NewBitlist64FromBytes creates a new bitlist for a given array of bytes. Size of the bitlist is explicitly specified via `n` (since number of bits required may not align perfectly to the word size).
func (*Bitlist64) And ¶
And returns the AND result of the two bitfields (intersection). This method will return an error if the bitlists are not the same length.
func (*Bitlist64) AndCount ¶
AndCount calculates number of bits set in an intersection of two bitfields. This method will return an error if the bitlists are not the same length.
func (*Bitlist64) BitAt ¶
BitAt returns the bit value at the given index. If the index requested exceeds the number of bits in the bitlist, then this method returns false.
func (*Bitlist64) BitIndices ¶
BitIndices returns list of bit indexes of bitlist where value is set to true.
func (*Bitlist64) Bytes ¶
Bytes returns underlying array of uint64s as an array of bytes. The leading zeros in the bitlist will be trimmed to the smallest byte length representation of the bitlist. This may produce an empty byte slice if all bits were zero.
func (*Bitlist64) Contains ¶
Contains returns true if the bitlist contains all of the bits from the provided argument bitlist i.e. if `b` is a superset of `c`. This method will return an error if bitlists are not the same length.
func (*Bitlist64) Len ¶
Len returns the number of bits in a bitlist (note that underlying array can be bigger).
func (*Bitlist64) NoAllocAnd ¶
NoAllocAnd computes the AND result of the two bitfields (intersection). Result is written into provided variable, so no allocation takes place inside the function. This method will return an error if the bitlists are not the same length.
func (*Bitlist64) NoAllocBitIndices ¶
NoAllocBitIndices returns list of bit indexes of bitlist where value is set to true. No allocation happens inside the function, so number of returned indexes is capped by the capacity of the ret param.
Expected usage pattern:
b := NewBitlist64(n) indices := make([]int, b.Count()) b.NoAllocBitIndices(indices)
func (*Bitlist64) NoAllocNot ¶
NoAllocNot returns the NOT result of the bitfield (complement). Result is written into provided variable, so no allocation takes place inside the function.
func (*Bitlist64) NoAllocOr ¶
NoAllocOr computes the OR result of the two bitfields (union). Result is written into provided variable, so no allocation takes place inside the function. This method will return an error if the bitlists are not the same length.
func (*Bitlist64) NoAllocXor ¶
NoAllocXor returns the XOR result of the two bitfields (symmetric difference). Result is written into provided variable, so no allocation takes place inside the function. This method will return an error if the bitlists are not the same length.
func (*Bitlist64) Or ¶
Or returns the OR result of the two bitfields (union). This method will return an error if the bitlists are not the same length.
func (*Bitlist64) OrCount ¶
OrCount calculates number of bits set in a union of two bitfields. This method will return an error if the bitlists are not the same length.
func (*Bitlist64) Overlaps ¶
Overlaps returns true if the bitlist contains one of the bits from the provided argument bitlist. This method will return an error if bitlists are not the same length.
func (*Bitlist64) SetBitAt ¶
SetBitAt will set the bit at the given index to the given value. If the index requested exceeds the number of bits in the bitlist, then this method returns false.
func (*Bitlist64) ToBitlist ¶
ToBitlist converts []uint64 backed bitlist into []byte backed bitlist.
type Bitvector128 ¶
type Bitvector128 []byte
Bitvector128 is a bitfield with a fixed defined size of 128. There is no length bit present in the underlying byte array.
func NewBitvector128 ¶
func NewBitvector128() Bitvector128
NewBitvector128 creates a new bitvector of size 128.
func (Bitvector128) BitAt ¶
func (b Bitvector128) BitAt(idx uint64) bool
BitAt returns the bit value at the given index. If the index requested exceeds the number of bits in the bitvector, then this method returns false.
func (Bitvector128) BitIndices ¶
func (b Bitvector128) BitIndices() []int
BitIndices returns the list of indices that are set to 1.
func (Bitvector128) Bytes ¶
func (b Bitvector128) Bytes() []byte
Bytes returns the bytes data representing the Bitvector128.
func (Bitvector128) Contains ¶
func (b Bitvector128) Contains(c Bitvector128) (bool, error)
Contains returns true if the bitlist contains all of the bits from the provided argument bitlist. This method will return an error if bitlists are not the same length.
func (Bitvector128) Count ¶
func (b Bitvector128) Count() uint64
Count returns the number of 1s in the bitvector.
func (Bitvector128) Len ¶
func (b Bitvector128) Len() uint64
Len returns the number of bits in the bitvector.
func (Bitvector128) Or ¶
func (b Bitvector128) Or(c Bitvector128) (Bitvector128, error)
Or returns the OR result of the two bitfields. This method will return an error if the bitlists are not the same length.
func (Bitvector128) Overlaps ¶
func (b Bitvector128) Overlaps(c Bitvector128) (bool, error)
Overlaps returns true if the bitlist contains one of the bits from the provided argument bitlist. This method will return an error if bitlists are not the same length.
func (Bitvector128) SetBitAt ¶
func (b Bitvector128) SetBitAt(idx uint64, val bool)
SetBitAt will set the bit at the given index to the given value. If the index requested exceeds the number of bits in the bitvector, then this method returns false.
func (Bitvector128) Shift ¶
func (b Bitvector128) Shift(i int)
Shift bitvector by i. If i >= 0, perform left shift, otherwise right shift.
type Bitvector16 ¶
type Bitvector16 []byte
Bitvector16 is a bitfield with a fixed defined size of 16. There is no length bit present in the underlying byte array.
func NewBitvector16 ¶
func NewBitvector16() Bitvector16
NewBitvector16 creates a new bitvector of size 16.
func (Bitvector16) BitAt ¶
func (b Bitvector16) BitAt(idx uint64) bool
BitAt returns the bit value at the given index. If the index requested exceeds the number of bits in the bitvector, then this method returns false.
func (Bitvector16) BitIndices ¶
func (b Bitvector16) BitIndices() []int
BitIndices returns the list of indices which are set to 1.
func (Bitvector16) Bytes ¶
func (b Bitvector16) Bytes() []byte
Bytes returns the bytes data representing the bitvector16.
func (Bitvector16) Contains ¶
func (b Bitvector16) Contains(c Bitvector16) (bool, error)
Contains returns true if the bitlist contains all of the bits from the provided argument bitlist. This method will return an error if bitlists are not the same length.
func (Bitvector16) Count ¶
func (b Bitvector16) Count() uint64
Count returns the number of 1s in the bitvector.
func (Bitvector16) Len ¶
func (b Bitvector16) Len() uint64
Len returns the number of bits in the bitvector.
func (Bitvector16) Or ¶
func (b Bitvector16) Or(c Bitvector16) (Bitvector16, error)
Or returns the OR result of the two bitfields. This method will return an error if the bitlists are not the same length.
func (Bitvector16) Overlaps ¶
func (b Bitvector16) Overlaps(c Bitvector16) (bool, error)
Overlaps returns true if the bitlist contains one of the bits from the provided argument bitlist. This method will return an error if bitlists are not the same length.
func (Bitvector16) SetBitAt ¶
func (b Bitvector16) SetBitAt(idx uint64, val bool)
SetBitAt will set the bit at the given index to the given value. If the index requested exceeds the number of bits in the bitvector, then this method returns false.
func (Bitvector16) Shift ¶
func (b Bitvector16) Shift(i int)
Shift bitvector by i. If i >= 0, perform left shift, otherwise right shift.
type Bitvector256 ¶
type Bitvector256 []byte
Bitvector256 is a bitfield with a fixed defined size of 256. There is no length bit present in the underlying byte array.
func NewBitvector256 ¶
func NewBitvector256() Bitvector256
NewBitvector256 creates a new bitvector of size 256.
func (Bitvector256) BitAt ¶
func (b Bitvector256) BitAt(idx uint64) bool
BitAt returns the bit value at the given index. If the index requested exceeds the number of bits in the bitvector, then this method returns false.
func (Bitvector256) BitIndices ¶
func (b Bitvector256) BitIndices() []int
BitIndices returns the list of indices that are set to 1.
func (Bitvector256) Bytes ¶
func (b Bitvector256) Bytes() []byte
Bytes returns the bytes data representing the Bitvector256.
func (Bitvector256) Count ¶
func (b Bitvector256) Count() uint64
Count returns the number of 1s in the bitvector.
func (Bitvector256) Len ¶
func (b Bitvector256) Len() uint64
Len returns the number of bits in the bitvector.
func (Bitvector256) SetBitAt ¶
func (b Bitvector256) SetBitAt(idx uint64, val bool)
SetBitAt will set the bit at the given index to the given value. If the index requested exceeds the number of bits in the bitvector, then this method returns false.
func (Bitvector256) Shift ¶
func (b Bitvector256) Shift(i int)
Shift bitvector by i. If i >= 0, perform left shift, otherwise right shift.
type Bitvector32 ¶
type Bitvector32 []byte
Bitvector32 is a bitfield with a fixed defined size of 32. There is no length bit present in the underlying byte array.
func NewBitvector32 ¶
func NewBitvector32() Bitvector32
NewBitvector32 creates a new bitvector of size 32.
func (Bitvector32) BitAt ¶
func (b Bitvector32) BitAt(idx uint64) bool
BitAt returns the bit value at the given index. If the index requested exceeds the number of bits in the bitvector, then this method returns false.
func (Bitvector32) BitIndices ¶
func (b Bitvector32) BitIndices() []int
BitIndices returns the list of indices which are set to 1.
func (Bitvector32) Bytes ¶
func (b Bitvector32) Bytes() []byte
Bytes returns the bytes data representing the bitvector32.
func (Bitvector32) Count ¶
func (b Bitvector32) Count() uint64
Count returns the number of 1s in the bitvector.
func (Bitvector32) Len ¶
func (b Bitvector32) Len() uint64
Len returns the number of bits in the bitvector.
func (Bitvector32) SetBitAt ¶
func (b Bitvector32) SetBitAt(idx uint64, val bool)
SetBitAt will set the bit at the given index to the given value. If the index requested exceeds the number of bits in the bitvector, then this method returns false.
type Bitvector4 ¶
type Bitvector4 []byte
Bitvector4 is a bitfield with a known size of 4. There is no length bit present in the underlying byte array.
func NewBitvector4 ¶
func NewBitvector4() Bitvector4
NewBitvector4 creates a new bitvector of size 4.
func (Bitvector4) BitAt ¶
func (b Bitvector4) BitAt(idx uint64) bool
BitAt returns the bit value at the given index. If the index requested exceeds the number of bits in the bitvector, then this method returns false.
func (Bitvector4) BitIndices ¶
func (b Bitvector4) BitIndices() []int
BitIndices returns the list of indices that are set to 1.
func (Bitvector4) Bytes ¶
func (b Bitvector4) Bytes() []byte
Bytes returns the bytes data representing the bitvector4. This method bitmasks the underlying data to ensure that it is an accurate representation.
func (Bitvector4) Count ¶
func (b Bitvector4) Count() uint64
Count returns the number of 1s in the bitvector.
func (Bitvector4) Len ¶
func (b Bitvector4) Len() uint64
Len returns the number of bits in the bitvector.
func (Bitvector4) SetBitAt ¶
func (b Bitvector4) SetBitAt(idx uint64, val bool)
SetBitAt will set the bit at the given index to the given value. If the index requested exceeds the number of bits in the bitvector, then this method returns false.
func (Bitvector4) Shift ¶
func (b Bitvector4) Shift(i int)
Shift bitvector by i. If i >= 0, perform left shift, otherwise right shift.
type Bitvector512 ¶
type Bitvector512 []byte
Bitvector512 is a bitfield with a fixed defined size of 512. There is no length bit present in the underlying byte array.
func NewBitvector512 ¶
func NewBitvector512() Bitvector512
NewBitvector512 creates a new bitvector of size 512.
func (Bitvector512) BitAt ¶
func (b Bitvector512) BitAt(idx uint64) bool
BitAt returns the bit value at the given index. If the index requested exceeds the number of bits in the bitvector, then this method returns false.
func (Bitvector512) BitIndices ¶
func (b Bitvector512) BitIndices() []int
BitIndices returns the list of indices that are set to 1.
func (Bitvector512) Bytes ¶
func (b Bitvector512) Bytes() []byte
Bytes returns the bytes data representing the Bitvector512.
func (Bitvector512) Count ¶
func (b Bitvector512) Count() uint64
Count returns the number of 1s in the bitvector.
func (Bitvector512) Len ¶
func (b Bitvector512) Len() uint64
Len returns the number of bits in the bitvector.
func (Bitvector512) SetBitAt ¶
func (b Bitvector512) SetBitAt(idx uint64, val bool)
SetBitAt will set the bit at the given index to the given value. If the index requested exceeds the number of bits in the bitvector, then this method returns false.
func (Bitvector512) Shift ¶
func (b Bitvector512) Shift(i int)
Shift bitvector by i. If i >= 0, perform left shift, otherwise right shift.
type Bitvector64 ¶
type Bitvector64 []byte
Bitvector64 is a bitfield with a fixed defined size of 64. There is no length bit present in the underlying byte array.
func NewBitvector64 ¶
func NewBitvector64() Bitvector64
NewBitvector64 creates a new bitvector of size 64.
func (Bitvector64) BitAt ¶
func (b Bitvector64) BitAt(idx uint64) bool
BitAt returns the bit value at the given index. If the index requested exceeds the number of bits in the bitvector, then this method returns false.
func (Bitvector64) BitIndices ¶
func (b Bitvector64) BitIndices() []int
BitIndices returns the list of indices which are set to 1.
func (Bitvector64) Bytes ¶
func (b Bitvector64) Bytes() []byte
Bytes returns the bytes data representing the bitvector64.
func (Bitvector64) Count ¶
func (b Bitvector64) Count() uint64
Count returns the number of 1s in the bitvector.
func (Bitvector64) Len ¶
func (b Bitvector64) Len() uint64
Len returns the number of bits in the bitvector.
func (Bitvector64) SetBitAt ¶
func (b Bitvector64) SetBitAt(idx uint64, val bool)
SetBitAt will set the bit at the given index to the given value. If the index requested exceeds the number of bits in the bitvector, then this method returns false.
func (Bitvector64) Shift ¶
func (b Bitvector64) Shift(i int)
Shift bitvector by i. If i >= 0, perform left shift, otherwise right shift.
type Bitvector8 ¶
type Bitvector8 []byte
Bitvector8 is a bitfield with a fixed defined size of 8. There is no length bit present in the underlying byte array.
func NewBitvector8 ¶
func NewBitvector8() Bitvector8
NewBitvector8 creates a new bitvector of size 8.
func (Bitvector8) BitAt ¶
func (b Bitvector8) BitAt(idx uint64) bool
BitAt returns the bit value at the given index. If the index requested exceeds the number of bits in the bitvector, then this method returns false.
func (Bitvector8) BitIndices ¶
func (b Bitvector8) BitIndices() []int
BitIndices returns the list of indices that are set to 1.
func (Bitvector8) Bytes ¶
func (b Bitvector8) Bytes() []byte
Bytes returns the bytes data representing the Bitvector8.
func (Bitvector8) Contains ¶
func (b Bitvector8) Contains(c Bitvector8) (bool, error)
Contains returns true if the bitlist contains all of the bits from the provided argument bitlist. This method will return an error if bitlists are not the same length or not `bitvector8BitSize`.
func (Bitvector8) Count ¶
func (b Bitvector8) Count() uint64
Count returns the number of 1s in the bitvector.
func (Bitvector8) Len ¶
func (b Bitvector8) Len() uint64
Len returns the number of bits in the bitvector.
func (Bitvector8) Or ¶
func (b Bitvector8) Or(c Bitvector8) (Bitvector8, error)
Or returns the OR result of the two bitfields. This method will return an error if the bitlists are not the same length.
func (Bitvector8) Overlaps ¶
func (b Bitvector8) Overlaps(c Bitvector8) (bool, error)
Overlaps returns true if the bitlist contains one of the bits from the provided argument bitlist. This method will return an error if bitlists are not the same length.
func (Bitvector8) SetBitAt ¶
func (b Bitvector8) SetBitAt(idx uint64, val bool)
SetBitAt will set the bit at the given index to the given value. If the index requested exceeds the number of bits in the bitvector, then this method returns false.