common

package
v1.8.3 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2022 License: LGPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

* Copyright (C) 2021 The poly network Authors * This file is part of The poly network library. * * The poly network is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * The poly network is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * You should have received a copy of the GNU Lesser General Public License * along with The poly network . If not, see <http://www.gnu.org/licenses/>.

* Copyright (C) 2021 The poly network Authors * This file is part of The poly network library. * * The poly network is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * The poly network is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * You should have received a copy of the GNU Lesser General Public License * along with The poly network . If not, see <http://www.gnu.org/licenses/>.

* Copyright (C) 2021 The poly network Authors * This file is part of The poly network library. * * The poly network is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * The poly network is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * You should have received a copy of the GNU Lesser General Public License * along with The poly network . If not, see <http://www.gnu.org/licenses/>.

* Copyright (C) 2021 The poly network Authors * This file is part of The poly network library. * * The poly network is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * The poly network is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * You should have received a copy of the GNU Lesser General Public License * along with The poly network . If not, see <http://www.gnu.org/licenses/>.

* Copyright (C) 2021 The poly network Authors * This file is part of The poly network library. * * The poly network is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * The poly network is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * You should have received a copy of the GNU Lesser General Public License * along with The poly network . If not, see <http://www.gnu.org/licenses/>.

* Copyright (C) 2021 The poly network Authors * This file is part of The poly network library. * * The poly network is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * The poly network is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * You should have received a copy of the GNU Lesser General Public License * along with The poly network . If not, see <http://www.gnu.org/licenses/>.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fingerprint

func Fingerprint(slice []byte) []byte

Fingerprint returns the first 6 bytes of a byte slice. If the slice is less than 6 bytes, the fingerprint contains trailing zeroes.

func IsEmpty

func IsEmpty(o interface{}) bool

Returns true if it has zero length.

func MaxInt

func MaxInt(a, b int) int

func MaxInt64

func MaxInt64(a, b int64) int64

func MinInt

func MinInt(a, b int) int

func MinInt64

func MinInt64(a, b int64) int64

func RandBool

func RandBool() bool

func RandBytes

func RandBytes(n int) []byte

func RandFloat32

func RandFloat32() float32

func RandFloat64

func RandFloat64() float64

func RandInt

func RandInt() int

func RandInt16

func RandInt16() int16

func RandInt31

func RandInt31() int32

func RandInt31n

func RandInt31n(n int32) int32

func RandInt32

func RandInt32() int32

func RandInt63

func RandInt63() int64

func RandInt63n

func RandInt63n(n int64) int64

func RandInt64

func RandInt64() int64

func RandIntn

func RandIntn(n int) int

func RandPerm

func RandPerm(n int) []int

func RandStr

func RandStr(length int) string

func RandTime

func RandTime() time.Time

func RandUint

func RandUint() uint

func RandUint16

func RandUint16() uint16

func RandUint32

func RandUint32() uint32

func RandUint64

func RandUint64() uint64

func Seed

func Seed(seed int64)

Types

type BitArray

type BitArray struct {
	Bits  int      `json:"bits"`  // NOTE: persisted via reflect, must be exported
	Elems []uint64 `json:"elems"` // NOTE: persisted via reflect, must be exported
	// contains filtered or unexported fields
}

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

func NewBitArray

func NewBitArray(bits int) *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 int) 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() (int, 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 int, 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() int

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 HexBytes

type HexBytes []byte

The main purpose of HexBytes is to enable HEX-encoding for json/encoding.

func (HexBytes) Bytes

func (bz HexBytes) Bytes() []byte

Allow it to fulfill various interfaces in light-client, etc...

func (HexBytes) Format

func (bz HexBytes) Format(s fmt.State, verb rune)

func (HexBytes) Marshal

func (bz HexBytes) Marshal() ([]byte, error)

Marshal needed for protobuf compatibility

func (HexBytes) MarshalJSON

func (bz HexBytes) MarshalJSON() ([]byte, error)

This is the point of Bytes.

func (HexBytes) String

func (bz HexBytes) String() string

func (*HexBytes) Unmarshal

func (bz *HexBytes) Unmarshal(data []byte) error

Unmarshal needed for protobuf compatibility

func (*HexBytes) UnmarshalJSON

func (bz *HexBytes) UnmarshalJSON(data []byte) error

This is the point of Bytes.

type Rand

type Rand struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Rand is a prng, that is seeded with OS randomness. The OS randomness is obtained from crypto/rand, however none of the provided methods are suitable for cryptographic usage. They all utilize math/rand's prng internally.

All of the methods here are suitable for concurrent use. This is achieved by using a mutex lock on all of the provided methods.

func NewRand

func NewRand() *Rand

func (*Rand) Bool

func (r *Rand) Bool() bool

Bool returns a uniformly random boolean

func (*Rand) Bytes

func (r *Rand) Bytes(n int) []byte

Bytes returns n random bytes generated from the internal prng.

func (*Rand) Float32

func (r *Rand) Float32() float32

func (*Rand) Float64

func (r *Rand) Float64() float64

func (*Rand) Int

func (r *Rand) Int() int

func (*Rand) Int16

func (r *Rand) Int16() int16

func (*Rand) Int31

func (r *Rand) Int31() int32

func (*Rand) Int31n

func (r *Rand) Int31n(n int32) int32

func (*Rand) Int32

func (r *Rand) Int32() int32

func (*Rand) Int63

func (r *Rand) Int63() int64

func (*Rand) Int63n

func (r *Rand) Int63n(n int64) int64

func (*Rand) Int64

func (r *Rand) Int64() int64

func (*Rand) Intn

func (r *Rand) Intn(n int) int

Intn returns, as an int, a uniform pseudo-random number in the range [0, n). It panics if n <= 0.

func (*Rand) Perm

func (r *Rand) Perm(n int) []int

Perm returns a pseudo-random permutation of n integers in [0, n).

func (*Rand) Seed

func (r *Rand) Seed(seed int64)

func (*Rand) Str

func (r *Rand) Str(length int) string

Str constructs a random alphanumeric string of given length.

func (*Rand) Time

func (r *Rand) Time() time.Time

func (*Rand) Uint

func (r *Rand) Uint() uint

func (*Rand) Uint16

func (r *Rand) Uint16() uint16

func (*Rand) Uint32

func (r *Rand) Uint32() uint32

func (*Rand) Uint64

func (r *Rand) Uint64() uint64

Jump to

Keyboard shortcuts

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