utils

package
v0.0.0-...-ff61ee7 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2020 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Overview

Errors and random helper functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs(a int) int

Abs returns the absolute value.

func AppendCopy

func AppendCopy(nxt int, arr []int) []int

func AppendUvarint

func AppendUvarint(v uint64, byt []byte) (int, []byte)

func CheckOverflow

func CheckOverflow(items ...uint64) bool

CheckOverflow returns true if the sum of items would overflow.

func CheckUnique

func CheckUnique(items ...Any) bool

CheckUnique returns true if all items input are unique. Compares items using == (so won't follow pointers, etc)

func CheckUniqueInt

func CheckUniqueInt(items ...Any) bool

func CheckUniqueSorted

func CheckUniqueSorted(equalFunc func(a, b Any) bool, items ...Any) bool

//////////////////////////////////////////////////// Check unique items /////////////////////////////////////////////////// CheckUniqueSorted takes as input a sorted list of items, and a function that returns true if 2 items are equal. It returns true if all items are unique.

func ContainsInt

func ContainsInt(l sort.IntSlice, v int) bool

func ContainsSlice

func ContainsSlice(s1 sort.IntSlice, s2 sort.IntSlice) bool

ContainsSlice returns true if s2 is contained in s1

func ConvertInt64

func ConvertInt64(n interface{}) int64

func CopyBuf

func CopyBuf(buf []byte) []byte

func CreateIntSlice

func CreateIntSlice(n int) (ret []int)

CreateIntSlice returns a slice of length n, where ret[i] == i.

func DecodeHelper

func DecodeHelper(reader io.Reader) (n int, buff []byte, err error)

func EncodeHelper

func EncodeHelper(arr []byte, writer io.Writer) (n int, err error)

EncodeHelper writes the size of the bytes followed by the bytes to the writer.

func EncodeUint16

func EncodeUint16(v uint16, writer io.Writer) (int, error)

func EncodeUint32

func EncodeUint32(v uint32, writer io.Writer) (int, error)

func EncodeUint64

func EncodeUint64(v uint64, writer io.Writer) (int, error)

func EncodeUvarint

func EncodeUvarint(v uint64, writer io.Writer) (int, error)

func Exp

func Exp(x, y int) (ret int)

Exp computes x to the power of y

func GenList

func GenList(count int) []int

GenList creates a list of integers upto count, i.e. if count = 3 it returns []int{0,1,2}.

func GenListUint64

func GenListUint64(start, end int) []uint64

GenListUint64 is the same as GenList, except creates a list of uint64s.

func GenRandPerm

func GenRandPerm(count int, max int, rnd *rand.Rand) []int

func GetBitAt

func GetBitAt(idx uint, v uint64) types.BinVal

func GetDuplicates

func GetDuplicates(items sort.IntSlice) (nonDuplicates sort.IntSlice, duplicates sort.IntSlice)

GetDuplicates takes as input a sorted slice of integers, then returns a sorted slice containing all the values, and a sorted slice containing any remaining duplicates. More specifically: "sort(nonDuplicates + duplicates) == items".

func GetNonDuplicateCount

func GetNonDuplicateCount(s1 sort.IntSlice, s2 sort.IntSlice) int

GetNonDuplicateCount returns "| set(s1 + s2) |".

func GetOneThirdBottom

func GetOneThirdBottom(n int) int

GetOneThridBottom returns \bot n/3

func GetUniqueCount

func GetUniqueCount(s1 sort.IntSlice) int

GetUnique count returns the number of unique items in s1

func IncreaseCap

func IncreaseCap(arr []byte, newCap int) []byte

IncreaseCap increases the capacitiy of the array if needed

func InsertIfNotFound

func InsertIfNotFound(arr sort.IntSlice, toInsert int, startIndex int) (sort.IntSlice, int, bool)

InsertIfNotFound inserts toInsert into arr, only if it does not already exist in arr after startIndex. arr must be sorted.

func InsertInSortedSlice

func InsertInSortedSlice(arr sort.IntSlice, toInsert int, startIndex int) (sort.IntSlice, int)

InsertInSortedSlice inserts toInsert into arr returning the sorted arr and the index where toInsert was inserted, startIndex is where to start searching for the insert location, (i.e arr[startIndex] must be less than toInsert). It allows duplcates.

func JoinBytes

func JoinBytes(items ...[]byte) []byte

JoinBytes calls bytes.join with a nil seperator.

func Max

func Max(a, b int) int

Max returns the max of a and b.

func Max64

func Max64(a, b int64) int64

Max64 returns the max of a and b.

func MaxConsensusIndex

func MaxConsensusIndex(l ...types.ConsensusInt) types.ConsensusInt

MinConsensusIndex returns the minimum input types.ConsensusInt.

func MaxConsensusRound

func MaxConsensusRound(l ...types.ConsensusRound) types.ConsensusRound

MaxConsensusRound returns the maximum types.ConsensusRound in l.

func MaxDuration

func MaxDuration(l ...time.Duration) time.Duration

MaxDuration returns the maximum duration in l.

func MaxInt

func MaxInt(l1 int, l ...int) int

MaxInt returns the maximum input int.

func MaxIntSlice

func MaxIntSlice(l ...int) int

func MaxU64

func MaxU64(a, b uint64) uint64

MaxU64 returns the max of a and b.

func MaxU64Slice

func MaxU64Slice(l ...uint64) (ret uint64)

MaxU64Slice returns the max value in the slice.

func MaxUint32

func MaxUint32(l []uint32) uint32

MaxUint32 returns the maximum uint32 in l.

func Min

func Min(a, b int) int

Min returns the minimum of a and b.

func MinConsensusIndex

func MinConsensusIndex(l ...types.ConsensusInt) types.ConsensusInt

MinConsensusIndex returns the minimum input types.ConsensusInt.

func MinConsensusRound

func MinConsensusRound(l ...types.ConsensusRound) types.ConsensusRound

MinConsensusRound returns the minimum input types.ConsensusRound .

func MinDuration

func MinDuration(l ...time.Duration) time.Duration

MinDuration returns the minimum duration in l.

func MinIntSlice

func MinIntSlice(l ...int) (ret int)

MinIntSlice returns the max value in the slice.

func MinU64Slice

func MinU64Slice(l ...uint64) (ret uint64)

MinU64Slice returns the max value in the slice.

func MinUint32

func MinUint32(l ...uint32) uint32

MinUint32 returns the minimum input uint32.

func NonZeroCount

func NonZeroCount(v [][][]byte) (count int)

func PanicNonNil

func PanicNonNil(err error)

func ReadBytes

func ReadBytes(n int, reader io.Reader) (read int, buff []byte, err error)

ReadBytes reads the given number of bytes into a new slice. An error is returned if less than n bytes are read.

func ReadTCPIPFile

func ReadTCPIPFile(filePath string, includePort bool) ([]string, error)

ReadTCPIPFile reads a file with one ip address per line, checks if the IPs are valid, and returns them as a slice of strings. If includePort is true, then the port is not removed when the address is read.

func ReadUint16

func ReadUint16(reader io.Reader) (v uint16, n int, err error)

func ReadUint32

func ReadUint32(reader io.Reader) (v uint32, n int, err error)

func ReadUint64

func ReadUint64(reader io.Reader) (v uint64, n int, err error)

func ReadUvarint

func ReadUvarint(r io.Reader) (uint64, int, error)

ReadUvarint reads an encoded unsigned integer from r and returns it as a uint64. This is modified from the golang library to use io.Reader instead of byte reader

func ReadUvarintByteReader

func ReadUvarintByteReader(r io.ByteReader) (uint64, int, error)

func RemoveDuplicatesSortCountString

func RemoveDuplicatesSortCountString(input []string) (ret []string)

RemoveDuplicatesString returns a new slice of strings with duplicates removed The results is sorted by the ones with the most duplicates.

func RemoveFromSlice

func RemoveFromSlice(n int, sli []int) (bool, []int)

RemoveFromSlice removes n from sli and returns true and the new slice. If n is not in the slice it returns false.

func RemoveFromSliceString

func RemoveFromSliceString(n string, sli []string) (bool, []string)

RemoveFromSlice removes n from sli and returns true and the new slice. If n is not in the slice it returns false.

func SortSorted

func SortSorted(s1 sort.IntSlice, s2 sort.IntSlice) sort.IntSlice

SortSorted takes two sorted lists as input and outputs the two lists sorted in a single list.

func SortSortedList

func SortSortedList(items ...sort.IntSlice) sort.IntSlice

SortSorted list takes as input several sorted lists and returns a sorted list containing all items.

func SortSortedNoDuplicates

func SortSortedNoDuplicates(s1 sort.IntSlice, s2 sort.IntSlice) sort.IntSlice

SortSortedNoDuplicates is the same as SortSorted, but returns a list with no duplicates.

func SplitBytes

func SplitBytes(buff []byte, maxPieces int) (ret [][]byte)

func SubOrOne

func SubOrOne(a, b uint64) uint64

SubOrOne runs:

if b >= a {
	return 1
}
return a - b

func SubOrZero

func SubOrZero(a, b uint64) uint64

SubOrZero runs:

if b > a {
	return 0
}
return a - b

func SubSortedSlice

func SubSortedSlice(s1 sort.IntSlice, s2 sort.IntSlice) sort.IntSlice

SubSortedSlice subtracts s2 from s1

func SumConsensusRound

func SumConsensusRound(l []types.ConsensusRound) types.ConsensusRound

SumConsensusRound sums l.

func SumDuration

func SumDuration(l []time.Duration) time.Duration

SumDuration sums the durations in l.

func SumUint32

func SumUint32(l []uint32) uint32

SumUint32 sums l.

func SumUint64

func SumUint64(l ...uint64) uint64

SumUint32 sums l.

func TrimZeros

func TrimZeros(buff []byte, minLen int) []byte

TrimZeros removes any zeros from the right end, but leaves at least minLen bytes.

func TrueCount

func TrueCount(arr []bool) (ret int)

func Uint64ToBytes

func Uint64ToBytes(v uint64) []byte

Uint64ToBytes uses uvarint to encode the uint64 to bytes.

func XORbytes

func XORbytes(a, b []byte) ([]byte, error)

Types

type Any

type Any interface{}

type MovingAvg

type MovingAvg struct {
	// contains filtered or unexported fields
}

MovingAvg tracks a cumlitive moving average.

func NewMovingAvg

func NewMovingAvg(windowSize int, initVal int) *MovingAvg

NewMovingAvg creates a object for tracking the cumlitive moving average with the given window size, initVal means the previous windowSize inputs had that initVal TODO this should keep the number of messages over a certain time, not a fixed number

func (*MovingAvg) AddElement

func (ma *MovingAvg) AddElement(v int) (newAvg int)

AddElement adds a new element to the moving average, and returns the new moving average

type SeededCTRSource

type SeededCTRSource struct {
	// contains filtered or unexported fields
}

SeededCTRSource is intended to be used as a source for the golang math.Rand objects. It uses an key and a nonce as the seed for the randoms using golang's AES CTR stream.

func NewSeededCTRSource

func NewSeededCTRSource(key [32]byte, nonce [16]byte) (*SeededCTRSource, error)

NewSeededCTRSource create a need random source using key and nonce as the seed.

func (*SeededCTRSource) Int63

func (src *SeededCTRSource) Int63() int64

Int63 returns a non-negative pseudo-random 63-bit integer as an int64.

func (*SeededCTRSource) Seed

func (src *SeededCTRSource) Seed(seed int64)

Seed is not supported, the source instead should be created with NewSeededCTRSource.

func (*SeededCTRSource) Uint64

func (src *SeededCTRSource) Uint64() uint64

Uint64 returns the next pseudo-random uint64.

type SortedChildren

type SortedChildren []*StringNode

func (SortedChildren) Len

func (sc SortedChildren) Len() int

func (SortedChildren) Less

func (sc SortedChildren) Less(i, j int) bool

func (SortedChildren) Swap

func (sc SortedChildren) Swap(i, j int)

type StringNode

type StringNode struct {
	Value    string
	Children SortedChildren
	Other    interface{}
}

Jump to

Keyboard shortcuts

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