Documentation ¶
Index ¶
- func Add32WithOverflow(a, b int32) (r int32, ok bool)
- func Add32to64WithOverflow(a int32, b int64) (r int32, ok bool)
- func AddWithOverflow(a, b int64) (r int64, ok bool)
- func Compare(lhs, rhs BitArray) int
- func EncodingPartsForBitLen(bitLen uint) ([]uint64, uint64)
- func MulHalfPositiveWithOverflow(a, b int64) (r int64, ok bool)
- func SizesForBitLen(bitLen uint) (uint, uint64)
- func Sub32WithOverflow(a, b int32) (r int32, ok bool)
- func Sub32to64WithOverflow(a int32, b int64) (r int32, ok bool)
- func SubWithOverflow(a, b int64) (r int64, ok bool)
- type BitArray
- func And(lhs, rhs BitArray) BitArray
- func Concat(lhs, rhs BitArray) BitArray
- func FromEncodingParts(words []uint64, lastBitsUsed uint64) (BitArray, error)
- func MakeBitArrayFromInt64(bitLen uint, val int64, valWidth uint) BitArray
- func MakeZeroBitArray(bitLen uint) BitArray
- func Next(d BitArray) BitArray
- func Not(d BitArray) BitArray
- func Or(lhs, rhs BitArray) BitArray
- func Parse(s string) (res BitArray, err error)
- func Rand(rng *rand.Rand, bitLen uint) BitArray
- func Xor(lhs, rhs BitArray) BitArray
- func (d BitArray) AsInt64(nbits uint) int64
- func (d BitArray) BitLen() uint
- func (d BitArray) Clone() BitArray
- func (d BitArray) EncodingParts() ([]uint64, uint64)
- func (d BitArray) Format(buf *bytes.Buffer)
- func (d BitArray) GetBitAtIndex(index int) (int, error)
- func (d BitArray) IsEmpty() bool
- func (d BitArray) LeftShiftAny(n int64) BitArray
- func (d BitArray) SetBitAtIndex(index, toSet int) (BitArray, error)
- func (d BitArray) Sizeof() uintptr
- func (d BitArray) String() string
- func (d BitArray) ToWidth(desiredLen uint) BitArray
- type Uint128
- func (u Uint128) Add(n uint64) Uint128
- func (u Uint128) And(o Uint128) Uint128
- func (u Uint128) Compare(o Uint128) int
- func (u Uint128) Equal(o Uint128) bool
- func (u Uint128) GetBytes() []byte
- func (u Uint128) Or(o Uint128) Uint128
- func (u Uint128) String() string
- func (u Uint128) Sub(n uint64) Uint128
- func (u Uint128) Xor(o Uint128) Uint128
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Add32WithOverflow ¶
Add32WithOverflow returns a+b. If ok is false, a+b overflowed.
func Add32to64WithOverflow ¶
Add32to64WithOverflow returns a+b. If ok is false, b was outside the int32 range or a+b overflowed.
func AddWithOverflow ¶
AddWithOverflow returns a+b. If ok is false, a+b overflowed.
func EncodingPartsForBitLen ¶
EncodingPartsForBitLen creates a word backing array and the "last bits used" value given the given total number of bits.
func MulHalfPositiveWithOverflow ¶
MulHalfPositiveWithOverflow returns a*b. b must be positive. If ok is false, a*b overflowed.
func SizesForBitLen ¶
SizesForBitLen computes the number of words and last bits used for the requested bit array size.
func Sub32WithOverflow ¶
Sub32WithOverflow returns a-b. If ok is false, a-b overflowed.
func Sub32to64WithOverflow ¶
Sub32to64WithOverflow returns a-b. If ok is false, b was outside the int32 range or a-b overflowed.
func SubWithOverflow ¶
SubWithOverflow returns a-b. If ok is false, a-b overflowed.
Types ¶
type BitArray ¶
type BitArray struct {
// contains filtered or unexported fields
}
BitArray implements a bit string of arbitrary length.
This uses a packed encoding (i.e. groups of 64 bits at a time) for memory efficiency and speed of bitwise operations (enables use of full machine registers for comparisons and logical operations), akin to the big.nat type.
There is something fancy needed to handle sorting values properly: the last group of bits must be padded right (start on the MSB) inside its word to compare properly according to pg semantics.
This type is designed for immutable instances. The functions and methods defined below never write to a bit array in-place. Of note, the ToWidth() and Next() functions will share the backing array between their operand and their result in some cases.
For portability, the size of the backing word is guaranteed to be 64 bits.
func And ¶
And computes the logical AND of two bit arrays. The caller must ensure they have the same bit size.
func FromEncodingParts ¶
FromEncodingParts creates a bit array from the encoding parts.
func MakeBitArrayFromInt64 ¶
MakeBitArrayFromInt64 creates a bit array with the specified size. The bits from the integer are written to the right of the bit array and the sign bit is extended.
func MakeZeroBitArray ¶
MakeZeroBitArray creates a bit array with the specified bit size.
func Next ¶
Next returns the next possible bit array in lexicographic order. The backing array of words is shared if possible.
func Or ¶
Or computes the logical OR of two bit arrays. The caller must ensure they have the same bit size.
func Xor ¶
Xor computes the logical XOR of two bit arrays. The caller must ensure they have the same bit size.
func (BitArray) AsInt64 ¶
AsInt64 returns the int constituted from the rightmost bits in the bit array.
func (BitArray) EncodingParts ¶
EncodingParts retrieves the encoding bits from the bit array. The words are presented in big-endian order, with the leftmost bits of the bitarray (MSB) in the MSB of each word.
func (BitArray) GetBitAtIndex ¶
GetBitAtIndex extract bit at given index in the BitArray.
func (BitArray) LeftShiftAny ¶
LeftShiftAny performs a logical left shift, with a possible negative count. The number of bits to shift can be arbitrarily large (i.e. possibly larger than 64 in absolute value).
func (BitArray) SetBitAtIndex ¶
SetBitAtIndex returns the BitArray with an updated bit at a given index.
type Uint128 ¶
type Uint128 struct {
Hi, Lo uint64
}
Uint128 is a big-endian 128 bit unsigned integer which wraps two uint64s.
func FromBytes ¶
FromBytes parses the byte slice as a 128 bit big-endian unsigned integer. The caller is responsible for ensuring the byte slice contains 16 bytes.
func FromString ¶
FromString parses a hexadecimal string as a 128-bit big-endian unsigned integer.