Documentation ¶
Index ¶
- Variables
- func BitLen(vint VarInt) int
- func Compare(abits, bbits Bits) int
- func Decode(r io.ReadCloser, vint VarInt) error
- func Encode(vint VarInt) io.ReadCloser
- func Len(vint VarInt) int
- func Sortable(vint VarInt) sort.Interface
- type Bits
- type VarInt
- func (vint VarInt) Add(i int, bits Bits) error
- func (vint VarInt) And(i int, bits Bits) error
- func (vint VarInt) Div(i int, bits Bits) error
- func (vint VarInt) Get(i int, bits Bits) error
- func (vint VarInt) GetSet(i int, bits Bits) error
- func (vint VarInt) Lsh(i, n int) error
- func (vint VarInt) Mod(i int, bits Bits) error
- func (vint VarInt) Mul(i int, bits Bits) error
- func (vint VarInt) Not(i int) error
- func (vint VarInt) Or(i int, bits Bits) error
- func (vint VarInt) Rsh(i, n int) error
- func (vint VarInt) Set(i int, bits Bits) error
- func (vint VarInt) Sub(i int, bits Bits) error
- func (vint VarInt) Xor(i int, bits Bits) error
Constants ¶
This section is empty.
Variables ¶
var ( ErrorBitLengthIsNotPositive = errors.New("the provided bit length has to be a strictly positive number") ErrorLengthIsNotPositive = errors.New("the provided length has to be a strictly positive number") ErrorBitLengthIsNotEfficient = errors.New("the provided bit length is over the threshold, for efficiency consider decreasing it or use big.Int slice") ErrorLengthIsNotEfficient = errors.New("the provided length is under the threshold, for efficiency consider increasing it or use uint slice") ErrorVarIntIsInvalid = errors.New("the varint is not valid for this operation") ErrorIndexIsNegative = errors.New("the provided index has to be not be a negative number") ErrorIndexIsOutOfRange = errors.New("the provided index is out of the number range") ErrorUnequalBitLengthCardinality = errors.New("the provided bit length does not have equal cardinality with the number") ErrorAdditionOverflow = errors.New("the addition result overflows its max value") ErrorMultiplicationOverflow = errors.New("the multiplication result overflows its max value") ErrorSubtractionUnderflow = errors.New("the subtraction result underflow its min value") ErrorDivisionByZero = errors.New("the division result is undefined for 0 value divisor") ErrorReaderIsNotDecodable = errors.New("reader does not contain decodable bytes") ErrorShiftIsNegative = errors.New("the provided shift has to be not be a negative number") )
The register of all static errors and warns that can be returned by VarInt.
Functions ¶
func BitLen ¶
BitLen returns bit length of the VarInt instance. BitLen is standalone function by choice to make VarInt more consistent and ergonomic. It's safe to use on nil VarInt, 0 is returned.
func Compare ¶
Compare returns an integer comparing of the provided Bits. The result is 0 if Bits a == b, -1 if Bits a < b, and +1 Bits if a > b. Currently it only compare bits with the same bit len akin to VarInt operations.
func Decode ¶
func Decode(r io.ReadCloser, vint VarInt) error
Decode dencodes the io.ReadCloser result from Encode into the provided VarInt. The provided VarInt has to be already preallocated, otherwise the ErrorVarIntIsInvalid is returned. The provided io.ReadCloser has to be encoded with binary.BigEndian iside, otherwise the ErrorReaderIsNotDecodable is returned.
func Encode ¶
func Encode(vint VarInt) io.ReadCloser
Encode lazily encodes the provided VarInt into io.ReadCloser. It uses binary.BigEndian encoding for the number. It also starts a goroutine to encode the number lazily, so the returned io.ReadCloser has to be always closed otherwise goroutine leak occures.
Types ¶
type Bits ¶
type Bits []uint
Bits is immutable intermediate representation for single integer inside VarInt. It's used as data transfer object for most of VarInt operations, and provides a number of convenient methods to convert it back and forth between other numerical presentations. Bits type somewhat resembles unsigned big.Int internally and provides similar transformations. However, note that by design most of Bits operations are not fast and allocate memory therefore should be only used to bootstrap and pass data to VarIant and not as standalone type.
func NewBits ¶
NewBits allocates and returns new Bits instance with predefined bit length and optional initialization value bytes slice. In case value bytes slice doesn't fit into the provided bit length, it is truncated to fit into the provided bit len. In case the provided bit len is negative number, actual bit len is calculated from the bytes slice. In case the provided bit len is 0, empty Bits marker is returned. See Bits type for more details.
func NewBitsBigInt ¶
NewBitsBigInt allocates, copies and returns new Bits instance from the provided big.Int, it deduces bit length to exactly fit the provided number. In case nil is provided empty Bits marker is returned. See Bits type for more details.
func NewBitsBits ¶
NewBitsBits allocates, copies and returns new Bits instance from the provided bit len and Bits, effectively making a deep copy of it. See Bits type for more details.
func NewBitsRand ¶
NewBitsRand allocates and returns new Bits instance filled with random bytes from provided Rand that fits the provided bit length. See Bits type for more details.
func NewBitsString ¶
NewBitsString parses, allocates and returns new Bits instance from the provided string and base, it deduces bit length to exactly fit the provided number. Valid base values are inside [2, 62], base values below 2 are converted to 2, base values above 62 are converted to 62. Leading plus '+' sings are ignored. Separating underscore '_' signs are allowed and also ignored. In case empty or invalid string is provided a special nil Bits marker is returned. The implementation follows big.Int. See Bits type for more details.
func NewBitsUint ¶
NewBitsUint allocates and returns new Bits instance with deduced bit length to exactly fit the provided number. See Bits type for more details.
func (Bits) BigInt ¶
BigInt allocates and returns a big.Int from value bytes slice of the Bits instance. It's safe to use on nil Bits, 0 is returned.
func (Bits) BitLen ¶
BitLen returns bit length of the Bits instance. It's safe to use on nil Bits, 0 is returned.
func (Bits) Bytes ¶
Bytes returns value bytes slice of the Bits instance. It's safe to use on nil Bits, {0} is returned.
func (Bits) Empty ¶
Empty returns true on nil Bits, or if the bit length is 0 or if value bytes slice is empty, otherwise returns false.
func (Bits) Format ¶
Format formats the Bits instance accordingly to provided format, most numeric formats are supported as well as #, 0 flags and pad width flag. In case invalid format is provided nothing is returned. It's safe to use on nil Bits, empty value is returned. Implements fmt.Formatter. The implementation follows big.Int.
func (Bits) String ¶
String returns a hex '%#X' string representation of the Bits instance decorated with bit length, in format '[blen]{hex_bytes}'. It's safe to use on nil Bits, [0]{0x0} is returned. Implements fmt.Stringer.
func (Bits) To ¶
To allocates and returns []byte representation of the Bits instance using the provided base. Valid base values are inside [2, 62], base values below 2 are converted to 2, base values above 62 are converted to 62. It's safe to use on nil Bits, {'0'} is returned. The implementation follows big.Int.
type VarInt ¶
type VarInt []uint
VarInt provides fast and memory efficient arbitrary bit length unsigned integer array type.
The purpose of VarInt to provide the maximum memory compact way to use and store unsigned custom bits integers. It does so by storing all the integers adjacent to each other inside a continuous numeric byte slice. It allocates the underlying numeric bytes slice only once on creation and doesn't expect to allocate any more memory afterwards. VarInt provides all the basic arithmetic and bitwise operations. To apply any of these operations, internal bits manipulations are required which implies certain computational overhead. Thus providing a tradeoff between CPU time and memory. Overhead grows lineraly, proportionally to bit len and is comparable with overhead from big.Int operations. Unlike big.Int however, VarInt uses exact number of bits to store the integers inside. Which makes VarInt extremely memory efficient. For example, to store a slice of 100 integers 100 bit each, big.Int requires 12400 bits, while VarInt needs exactly 10000 bits. In the same fashion VarInt also provides an efficient way to store integers smaller than 64 bits. For example, to store a slice of 1000 integers 2 bit each, []uin8 requires 8000 bits, while VarInt needs exactly 2000 bits. However, note that VarInt is no way close to be optimized as well as big.Int, and provides diminishing returns as bit length grows above certain threshold.
Currently, in a conscious decision multiple operations are implemented in favour of simplicity and not computational complexity, this includes Mul that uses standard long multiplication instead of fast multiplication algorithms like Karatsuba multiplication, and Div that uses standard slow division instead of fast division algorithms. The main rationale behind this choice is the fact that VarInt has the most efficiency when used for small and medium size integers in the range of 1 to 5000 bit width, therefore asymptotic complexity should be less significant for this library. Note that VarInt carries a small fixed overhead internaly, it allocates 2 separate uint cells at the beginning of the numeric bytes slice to store length and bit length. It also collocates extra Bits variable at the end of numeric bytes slice which is used internally for many operations as a computation temporary buffer, including: Mul, Div, Mod, Sort. Currently, for simplicity and consistency most VarInt operations apply changes in place on the provided index and require the provided Bits to have exactly the same bit len, otherwise ErrorUnequalBitLengthCardinality is returned. Currently, VarInt provides only unsigned arithmetic.
func NewVarInt ¶
NewVarInt allocates and returns VarInt instance that is capable to fit the provided number of integers each of the provided bit len in width. In case the provided bit len is not positive, invalid number and ErrorBitLengthIsNotPositive is returned. In case the len is not positive, invalid number and ErrorLengthIsNotPositive is returned. In case the provided bit len is larger than predefined threshold of 4096, valid VarInt is still returned along with ErrorBitLengthIsNotEfficient warning. In case the provided len is smaller than predefined threshold of 4, valid VarInt is still returned along with ErrorLengthIsNotEfficient warning. See VarInt type for more details.
func (VarInt) Add ¶
Add adds the provided bits to the integer inside VarInt at the provided index. In case the operation is used on invalid nil VarInt, ErrorVarIntIsInvalid is returned. In case negative index is provided, ErrorIndexIsNegative is returned. In case the provided index is greater than len of VarInt, ErrorIndexIsOutOfRange is returned. In case the provided bits has different bit len, ErrorUnequalBitLengthCardinality is returned. In case the addition result overflows the bit len, the regular unsigned semantic applies and extra ErrorAdditionOverflow warning is returned.
func (VarInt) And ¶
And applies bitwise and & operation to the provided bits and the integer inside VarInt at the provided index. In case the operation is used on invalid nil VarInt, ErrorVarIntIsInvalid is returned. In case negative index is provided, ErrorIndexIsNegative is returned. In case the provided index is greater than len of VarInt, ErrorIndexIsOutOfRange is returned. In case the provided bits has different bit len, ErrorUnequalBitLengthCardinality is returned.
func (VarInt) Div ¶
Div divides the provided bits with the integer inside VarInt at the provided index. In case the operation is used on invalid nil VarInt, ErrorVarIntIsInvalid is returned. In case negative index is provided, ErrorIndexIsNegative is returned. In case the provided index is greater than len of VarInt, ErrorIndexIsOutOfRange is returned. In case the provided bits has different bit len, ErrorUnequalBitLengthCardinality is returned. In case the division by zero is attempted, ErrorDivisionByZero is returned
func (VarInt) Get ¶
Get sets the provided bits to the integer inside VarInt at the provided index. It never allocates new Bits, the provided Bits are expected to be preallocated by the caller. In case the operation is used on invalid nil VarInt, ErrorVarIntIsInvalid is returned. In case negative index is provided, ErrorIndexIsNegative is returned. In case the provided index is greater than len of VarInt, ErrorIndexIsOutOfRange is returned. In case the provided bits has different bit len, ErrorUnequalBitLengthCardinality is returned.
func (VarInt) GetSet ¶
GetSet swaps the provided bits with the integer inside VarInt at the provided index. In case the operation is used on invalid nil VarInt, ErrorVarIntIsInvalid is returned. In case negative index is provided, ErrorIndexIsNegative is returned. In case the provided index is greater than len of VarInt, ErrorIndexIsOutOfRange is returned. In case the provided bits has different bit len, ErrorUnequalBitLengthCardinality is returned.
func (VarInt) Lsh ¶
Lsh applies left shift << operation to the integer inside VarInt at the provided index. In case the operation is used on invalid nil VarInt, ErrorVarIntIsInvalid is returned. In case negative shift is provided, ErrorShiftIsNegative is returned. In case negative index is provided, ErrorIndexIsNegative is returned. In case the provided index is greater than len of VarInt, ErrorIndexIsOutOfRange is returned.
func (VarInt) Mod ¶
Mod applies modulo operation to the provided bits and the integer inside VarInt at the provided index. In case the operation is used on invalid nil VarInt, ErrorVarIntIsInvalid is returned. In case negative index is provided, ErrorIndexIsNegative is returned. In case the provided index is greater than len of VarInt, ErrorIndexIsOutOfRange is returned. In case the provided bits has different bit len, ErrorUnequalBitLengthCardinality is returned. In case the division by zero is attempted, ErrorDivisionByZero is returned
func (VarInt) Mul ¶
Mul multiplies the provided bits with the integer inside VarInt at the provided index. In case the operation is used on invalid nil VarInt, ErrorVarIntIsInvalid is returned. In case negative index is provided, ErrorIndexIsNegative is returned. In case the provided index is greater than len of VarInt, ErrorIndexIsOutOfRange is returned. In case the provided bits has different bit len, ErrorUnequalBitLengthCardinality is returned. In case the multiplication result overflows the bit len, the integer is trucated and extra ErrorMultiplicationOverflow warning is returned.
func (VarInt) Not ¶
Not applies bitwise negation ^ operation to the integer inside VarInt at the provided index. In case the operation is used on invalid nil VarInt, ErrorVarIntIsInvalid is returned. In case negative index is provided, ErrorIndexIsNegative is returned. In case the provided index is greater than len of VarInt, ErrorIndexIsOutOfRange is returned.
func (VarInt) Or ¶
Or applies bitwise and | operation to the provided bits and the integer inside VarInt at the provided index. In case the operation is used on invalid nil VarInt, ErrorVarIntIsInvalid is returned. In case negative index is provided, ErrorIndexIsNegative is returned. In case the provided index is greater than len of VarInt, ErrorIndexIsOutOfRange is returned. In case the provided bits has different bit len, ErrorUnequalBitLengthCardinality is returned.
func (VarInt) Rsh ¶
Rsh applies right shift >> operation to the integer inside VarInt at the provided index. In case the operation is used on invalid nil VarInt, ErrorVarIntIsInvalid is returned. In case negative shift is provided, ErrorShiftIsNegative is returned. In case negative index is provided, ErrorIndexIsNegative is returned. In case the provided index is greater than len of VarInt, ErrorIndexIsOutOfRange is returned.
func (VarInt) Set ¶
Set sets the provided bits into the integer inside VarInt at the provided index. In case the operation is used on invalid nil VarInt, ErrorVarIntIsInvalid is returned. In case negative index is provided, ErrorIndexIsNegative is returned. In case the provided index is greater than len of VarInt, ErrorIndexIsOutOfRange is returned. In case the provided bits has different bit len, ErrorUnequalBitLengthCardinality is returned.
func (VarInt) Sub ¶
Sub subtracts the provided bits from the integer inside VarInt at the provided index. In case the operation is used on invalid nil VarInt, ErrorVarIntIsInvalid is returned. In case negative index is provided, ErrorIndexIsNegative is returned. In case the provided index is greater than len of VarInt, ErrorIndexIsOutOfRange is returned. In case the provided bits has different bit len, ErrorUnequalBitLengthCardinality is returned. In case the subtraction result underflows the integer, the regular unsigned semantic applies and extra ErrorSubtractionUnderflow warning is returned.
func (VarInt) Xor ¶
Xor applies bitwise and ^ operation to the provided bits and the integer inside VarInt at the provided index. In case the operation is used on invalid nil VarInt, ErrorVarIntIsInvalid is returned. In case negative index is provided, ErrorIndexIsNegative is returned. In case the provided index is greater than len of VarInt, ErrorIndexIsOutOfRange is returned. In case the provided bits has different bit len, ErrorUnequalBitLengthCardinality is returned.