utils

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 16, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add32WithOverflow

func Add32WithOverflow(a, b int32) (r int32, ok bool)

Add32WithOverflow returns a+b. If ok is false, a+b overflowed.

func Add32to64WithOverflow

func Add32to64WithOverflow(a int32, b int64) (r int32, ok bool)

Add32to64WithOverflow returns a+b. If ok is false, b was outside the int32 range or a+b overflowed.

func AddWithOverflow

func AddWithOverflow(a, b int64) (r int64, ok bool)

AddWithOverflow returns a+b. If ok is false, a+b overflowed.

func Compare

func Compare(lhs, rhs BitArray) int

Compare compares two bit arrays. They can have mixed sizes.

func EncodingPartsForBitLen

func EncodingPartsForBitLen(bitLen uint) ([]uint64, uint64)

EncodingPartsForBitLen creates a word backing array and the "last bits used" value given the given total number of bits.

func MulHalfPositiveWithOverflow

func MulHalfPositiveWithOverflow(a, b int64) (r int64, ok bool)

MulHalfPositiveWithOverflow returns a*b. b must be positive. If ok is false, a*b overflowed.

func SizesForBitLen

func SizesForBitLen(bitLen uint) (uint, uint64)

SizesForBitLen computes the number of words and last bits used for the requested bit array size.

func Sub32WithOverflow

func Sub32WithOverflow(a, b int32) (r int32, ok bool)

Sub32WithOverflow returns a-b. If ok is false, a-b overflowed.

func Sub32to64WithOverflow

func Sub32to64WithOverflow(a int32, b int64) (r int32, ok bool)

Sub32to64WithOverflow returns a-b. If ok is false, b was outside the int32 range or a-b overflowed.

func SubWithOverflow

func SubWithOverflow(a, b int64) (r int64, ok bool)

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

func And(lhs, rhs BitArray) BitArray

And computes the logical AND of two bit arrays. The caller must ensure they have the same bit size.

func Concat

func Concat(lhs, rhs BitArray) BitArray

Concat concatenates two bit arrays.

func FromEncodingParts

func FromEncodingParts(words []uint64, lastBitsUsed uint64) (BitArray, error)

FromEncodingParts creates a bit array from the encoding parts.

func MakeBitArrayFromInt64

func MakeBitArrayFromInt64(bitLen uint, val int64, valWidth uint) BitArray

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

func MakeZeroBitArray(bitLen uint) BitArray

MakeZeroBitArray creates a bit array with the specified bit size.

func Next

func Next(d BitArray) BitArray

Next returns the next possible bit array in lexicographic order. The backing array of words is shared if possible.

func Not

func Not(d BitArray) BitArray

Not computes the complement of a bit array.

func Or

func Or(lhs, rhs BitArray) BitArray

Or computes the logical OR of two bit arrays. The caller must ensure they have the same bit size.

func Parse

func Parse(s string) (res BitArray, err error)

Parse parses a bit array from the specified string.

func Rand

func Rand(rng *rand.Rand, bitLen uint) BitArray

Rand generates a random bit array of the specified length.

func Xor

func Xor(lhs, rhs BitArray) BitArray

Xor computes the logical XOR of two bit arrays. The caller must ensure they have the same bit size.

func (BitArray) AsInt64

func (d BitArray) AsInt64(nbits uint) int64

AsInt64 returns the int constituted from the rightmost bits in the bit array.

func (BitArray) BitLen

func (d BitArray) BitLen() uint

BitLen returns the number of bits stored.

func (BitArray) Clone

func (d BitArray) Clone() BitArray

Clone makes a copy of the bit array.

func (BitArray) EncodingParts

func (d BitArray) EncodingParts() ([]uint64, uint64)

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) Format

func (d BitArray) Format(buf *bytes.Buffer)

Format prints out the bit array to the buffer.

func (BitArray) GetBitAtIndex

func (d BitArray) GetBitAtIndex(index int) (int, error)

GetBitAtIndex extract bit at given index in the BitArray.

func (BitArray) IsEmpty

func (d BitArray) IsEmpty() bool

IsEmpty returns true iff the array is empty.

func (BitArray) LeftShiftAny

func (d BitArray) LeftShiftAny(n int64) BitArray

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

func (d BitArray) SetBitAtIndex(index, toSet int) (BitArray, error)

SetBitAtIndex returns the BitArray with an updated bit at a given index.

func (BitArray) Sizeof

func (d BitArray) Sizeof() uintptr

Sizeof returns the size in bytes of the bit array and its components.

func (BitArray) String

func (d BitArray) String() string

String implements the fmt.Stringer interface.

func (BitArray) ToWidth

func (d BitArray) ToWidth(desiredLen uint) BitArray

ToWidth resizes the bit array to the specified size. If the specified width is shorter, bits on the right are truncated away. If the specified width is larger, zero bits are added on the right.

type Uint128

type Uint128 struct {
	Hi, Lo uint64
}

Uint128 is a big-endian 128 bit unsigned integer which wraps two uint64s.

func FromBytes

func FromBytes(b []byte) Uint128

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 FromInts

func FromInts(hi uint64, lo uint64) Uint128

FromInts takes in two unsigned 64-bit integers and constructs a Uint128.

func FromString

func FromString(s string) (Uint128, error)

FromString parses a hexadecimal string as a 128-bit big-endian unsigned integer.

func (Uint128) Add

func (u Uint128) Add(n uint64) Uint128

Add returns a new Uint128 incremented by n.

func (Uint128) And

func (u Uint128) And(o Uint128) Uint128

And returns a new Uint128 that is the bitwise AND of two Uint128 values.

func (Uint128) Compare

func (u Uint128) Compare(o Uint128) int

Compare compares the two Uint128.

func (Uint128) Equal

func (u Uint128) Equal(o Uint128) bool

Equal returns whether or not the Uint128 are equivalent.

func (Uint128) GetBytes

func (u Uint128) GetBytes() []byte

GetBytes returns a big-endian byte representation.

func (Uint128) Or

func (u Uint128) Or(o Uint128) Uint128

Or returns a new Uint128 that is the bitwise OR of two Uint128 values.

func (Uint128) String

func (u Uint128) String() string

String returns a hexadecimal string representation.

func (Uint128) Sub

func (u Uint128) Sub(n uint64) Uint128

Sub returns a new Uint128 decremented by n.

func (Uint128) Xor

func (u Uint128) Xor(o Uint128) Uint128

Xor returns a new Uint128 that is the bitwise XOR of two Uint128 values.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL