utils

package
v0.16.3 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2022 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildHash

func BuildHash(msgType byte, bytes []byte) common.Hash

BuildHash converts byte array to hash. Use sha256 to generate a unique message hash.

func False

func False(atm *int32) bool

Returns whether the specified value is equal to 0.

func MaxInt64

func MaxInt64(a, b int64) int64

func MaxUInt

func MaxUInt(a, b uint32) uint32

func MergeBytes

func MergeBytes(bts ...[]byte) []byte

MergeBytes merges multiple bytes of data and returns the merged byte array.

func MinInt

func MinInt(a, b int) int

func MinInt64

func MinInt64(a, b int64) int64

func MinUInt

func MinUInt(a, b uint32) uint32

func Rand32Bytes

func Rand32Bytes(n uint32) []byte

func RandBytes

func RandBytes(n int) []byte

func RandInt31n

func RandInt31n(n int32) uint32

func RandIntn

func RandIntn(n int) int

func RandomOffset

func RandomOffset(n int) int

RandomOffset returns a random offset between 0 and n

func SetFalse

func SetFalse(atm *int32)

Set the specified variable to 0.

func SetTrue

func SetTrue(atm *int32)

Set the specified variable to 1.

func True

func True(atm *int32) bool

Returns whether the specified value is equal to 1.

Types

type BitArray

type BitArray struct {
	Bits  uint32   `json:"bits"`  // NOTE: persisted via reflect, must be exported
	Elems []uint64 `json:"elems"` // NOTE: persisted via reflect, must be exported
}

BitArray is a thread-unsafe implementation of a bit array.

func NewBitArray

func NewBitArray(bits uint32) *BitArray

NewBitArray returns a new bit array. It returns nil if the number of bits is zero.

func (*BitArray) And

func (bA *BitArray) And(o *BitArray) *BitArray

And returns a bit array resulting from a bitwise AND of the two bit arrays. If the two bit-arrys have different lengths, this truncates the larger of the two bit-arrays from the right. Thus the size of the return value is the minimum of the two provided bit arrays.

func (*BitArray) Bytes

func (bA *BitArray) Bytes() []byte

Bytes returns the byte representation of the bits within the bitarray.

func (*BitArray) Copy

func (bA *BitArray) Copy() *BitArray

Copy returns a copy of the provided bit array.

func (*BitArray) GetIndex

func (bA *BitArray) GetIndex(i uint32) bool

GetIndex returns the bit at index i within the bit array. The behavior is undefined if i >= bA.Bits

func (*BitArray) IsEmpty

func (bA *BitArray) IsEmpty() bool

IsEmpty returns true iff all bits in the bit array are 0

func (*BitArray) IsFull

func (bA *BitArray) IsFull() bool

IsFull returns true iff all bits in the bit array are 1.

func (*BitArray) MarshalJSON

func (bA *BitArray) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler interface by marshaling bit array using a custom format: a string of '-' or 'x' where 'x' denotes the 1 bit.

func (*BitArray) Not

func (bA *BitArray) Not() *BitArray

Not returns a bit array resulting from a bitwise Not of the provided bit array.

func (*BitArray) Or

func (bA *BitArray) Or(o *BitArray) *BitArray

Or returns a bit array resulting from a bitwise OR of the two bit arrays. If the two bit-arrys have different lengths, Or right-pads the smaller of the two bit-arrays with zeroes. Thus the size of the return value is the maximum of the two provided bit arrays.

func (*BitArray) PickRandom

func (bA *BitArray) PickRandom() (uint32, bool)

PickRandom returns a random index for a set bit in the bit array. If there is no such value, it returns 0, false. It uses the global randomness in `random.go` to get this index.

func (*BitArray) SetIndex

func (bA *BitArray) SetIndex(i uint32, v bool) bool

SetIndex sets the bit at index i within the bit array. The behavior is undefined if i >= bA.Bits

func (*BitArray) Size

func (bA *BitArray) Size() uint32

Size returns the number of bits in the bitarray

func (*BitArray) String

func (bA *BitArray) String() string

String returns a string representation of BitArray: BA{<bit-string>}, where <bit-string> is a sequence of 'x' (1) and '_' (0). The <bit-string> includes spaces and newlines to help people. For a simple sequence of 'x' and '_' characters with no spaces or newlines, see the MarshalJSON() method. Example: "BA{_x_}" or "nil-BitArray" for nil.

func (*BitArray) StringIndented

func (bA *BitArray) StringIndented(indent string) string

StringIndented returns the same thing as String(), but applies the indent at every 10th bit, and twice at every 50th bit.

func (*BitArray) Sub

func (bA *BitArray) Sub(o *BitArray) *BitArray

Sub subtracts the two bit-arrays bitwise, without carrying the bits. Note that carryless subtraction of a - b is (a and not b). The output is the same as bA, regardless of o's size. If bA is longer than o, o is right padded with zeroes

func (*BitArray) UnmarshalJSON

func (bA *BitArray) UnmarshalJSON(bz []byte) error

UnmarshalJSON implements json.Unmarshaler interface by unmarshaling a custom JSON description.

func (*BitArray) Update

func (bA *BitArray) Update(o *BitArray)

Update sets the bA's bits to be that of the other bit array. The copying begins from the begin of both bit arrays.

type KeyValuePair

type KeyValuePair struct {
	Key   string
	Value int64
}

Represents a k-v key-value pair.

type KeyValuePairList

type KeyValuePairList []KeyValuePair

KeyValuePairList is a slice of Pairs that implements sort.Interface to sort by Value.

func SortMap

func SortMap(m map[string]int64) KeyValuePairList

Sort the Map according to the key, return the list of sorted key-value pairs.

func (KeyValuePairList) Len

func (kvp KeyValuePairList) Len() int

func (KeyValuePairList) Less

func (kvp KeyValuePairList) Less(i, j int) bool

func (*KeyValuePairList) Pop

func (kvp *KeyValuePairList) Pop() interface{}

Pop up an element.

func (*KeyValuePairList) Push

func (kvp *KeyValuePairList) Push(x interface{})

Add an element.

func (KeyValuePairList) Swap

func (kvp KeyValuePairList) Swap(i, j int)

Jump to

Keyboard shortcuts

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