utils

package
v0.7.5 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs added in v0.4.0

func Abs[T constraints.Integer | constraints.Float](val T) T

Abs returns the absolute value of the given number.

func GenerateUniqueAlias added in v0.2.0

func GenerateUniqueAlias() string

GenerateUniqueAlias generates a unique alias. This is thread-safe.

func GetMapKeys added in v0.2.0

func GetMapKeys[K comparable, V any](m map[K]V) []K

GetMapKeys returns the map's keys as an unsorted slice.

func GetMapKeysSorted added in v0.2.0

func GetMapKeysSorted[K cmp.Ordered, V any](m map[K]V) []K

GetMapKeysSorted returns the map's keys as a sorted slice. The keys are sorted in ascending order. For descending order, use GetMapKeysSortedDescending.

func GetMapKeysSortedDescending added in v0.2.0

func GetMapKeysSortedDescending[K cmp.Ordered, V any](m map[K]V) []K

GetMapKeysSortedDescending returns the map's keys as a sorted slice. The keys are sorted in descending order. For ascending order, use GetMapKeysSorted.

func GetMapValues added in v0.2.0

func GetMapValues[K comparable, V any](m map[K]V) []V

GetMapValues returns the map's values as a slice. Due to Go's map iteration, the values will be in a non-deterministic order.

func IsGeneratedAlias added in v0.2.0

func IsGeneratedAlias(alias string) bool

IsGeneratedAlias returns whether the given alias was generated using GenerateUniqueAlias.

func Max added in v0.4.0

func Max[T constraints.Ordered](vals ...T) (maximum T)

Max returns the largest value of the given parameters. If no parameters are given, then returns the default value for the type.

func Min added in v0.4.0

func Min[T constraints.Ordered](vals ...T) (minimum T)

Min returns the smallest value of the given parameters.

func SliceToMapKeys added in v0.2.0

func SliceToMapKeys[T comparable](slice []T) map[T]struct{}

SliceToMapKeys converts the given slice into a map where all of the keys are represented by all of the slice's values.

func SliceToMapValues added in v0.2.0

func SliceToMapValues[K comparable, V any](slice []V, getKey func(V) K) map[K]V

SliceToMapValues converts the given slice into a map where all of the slice's values are keyed by the output of the `getKey` function, which takes a value and returns a key. It is up to the caller to return a unique key.

Types

type Reader added in v0.7.5

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

Reader handles type-safe reading from a byte slice, which was created by a Writer. This is not safe for concurrent use.

func NewReader added in v0.7.5

func NewReader(data []byte) *Reader

NewReader creates a new Reader that will read from the given data.

func (*Reader) Bool added in v0.7.5

func (reader *Reader) Bool() bool

Bool reads a bool.

func (*Reader) BoolSlice added in v0.7.5

func (reader *Reader) BoolSlice() []bool

BoolSlice reads a bool slice.

func (*Reader) Byte added in v0.7.5

func (reader *Reader) Byte() byte

Byte reads a byte. This is equivalent to Uint8, but is included since it is more common to refer to a byte rather than a uint8.

func (*Reader) ByteSlice added in v0.7.5

func (reader *Reader) ByteSlice() []byte

ByteSlice reads a byte slice. This is equivalent to Uint8Slice, but is included since it is more common to refer to byte slices than uint8 slices.

func (*Reader) Float32 added in v0.7.5

func (reader *Reader) Float32() float32

Float32 reads a float32.

func (*Reader) Float32Slice added in v0.7.5

func (reader *Reader) Float32Slice() []float32

Float32Slice reads a float32 slice.

func (*Reader) Float64 added in v0.7.5

func (reader *Reader) Float64() float64

Float64 reads a float64.

func (*Reader) Float64Slice added in v0.7.5

func (reader *Reader) Float64Slice() []float64

Float64Slice reads a float64 slice.

func (*Reader) Int16 added in v0.7.5

func (reader *Reader) Int16() int16

Int16 reads an int16.

func (*Reader) Int16Slice added in v0.7.5

func (reader *Reader) Int16Slice() []int16

Int16Slice reads an int16 slice.

func (*Reader) Int32 added in v0.7.5

func (reader *Reader) Int32() int32

Int32 reads an int32.

func (*Reader) Int32Slice added in v0.7.5

func (reader *Reader) Int32Slice() []int32

Int32Slice reads an int32 slice.

func (*Reader) Int64 added in v0.7.5

func (reader *Reader) Int64() int64

Int64 reads an int64.

func (*Reader) Int64Slice added in v0.7.5

func (reader *Reader) Int64Slice() []int64

Int64Slice reads an int64 slice.

func (*Reader) Int8 added in v0.7.5

func (reader *Reader) Int8() int8

Int8 reads an int8.

func (*Reader) Int8Slice added in v0.7.5

func (reader *Reader) Int8Slice() []int8

Int8Slice reads an int8 slice.

func (*Reader) IsEmpty added in v0.7.5

func (reader *Reader) IsEmpty() bool

IsEmpty returns true when all of the data has been read.

func (*Reader) RemainingBytes added in v0.7.5

func (reader *Reader) RemainingBytes() uint64

RemainingBytes returns the number of bytes that have not yet been read.

func (*Reader) String added in v0.7.5

func (reader *Reader) String() string

String reads a string.

func (*Reader) StringSlice added in v0.7.5

func (reader *Reader) StringSlice() []string

StringSlice reads a string slice.

func (*Reader) Uint16 added in v0.7.5

func (reader *Reader) Uint16() uint16

Uint16 reads a uint16.

func (*Reader) Uint16Slice added in v0.7.5

func (reader *Reader) Uint16Slice() []uint16

Uint16Slice reads a uint16 slice.

func (*Reader) Uint32 added in v0.7.5

func (reader *Reader) Uint32() uint32

Uint32 reads a uint32.

func (*Reader) Uint32Slice added in v0.7.5

func (reader *Reader) Uint32Slice() []uint32

Uint32Slice reads a uint32 slice.

func (*Reader) Uint64 added in v0.7.5

func (reader *Reader) Uint64() uint64

Uint64 reads a uint64.

func (*Reader) Uint64Slice added in v0.7.5

func (reader *Reader) Uint64Slice() []uint64

Uint64Slice reads a uint64 slice.

func (*Reader) Uint8 added in v0.7.5

func (reader *Reader) Uint8() uint8

Uint8 reads a uint8.

func (*Reader) Uint8Slice added in v0.7.5

func (reader *Reader) Uint8Slice() []uint8

Uint8Slice reads a uint8 slice.

func (*Reader) VariableInt added in v0.7.5

func (reader *Reader) VariableInt() int64

VariableInt reads an int64 that was written using variable-length encoding.

func (*Reader) VariableIntSlice added in v0.7.5

func (reader *Reader) VariableIntSlice() []int64

VariableIntSlice reads an int64 slice that was written using variable-length encoding.

func (*Reader) VariableUint added in v0.7.5

func (reader *Reader) VariableUint() uint64

VariableUint reads a uint64 that was written using variable-length encoding.

func (*Reader) VariableUintSlice added in v0.7.5

func (reader *Reader) VariableUintSlice() []uint64

VariableUintSlice reads a uint64 slice that was written using variable-length encoding.

type Stack

type Stack[T any] struct {
	// contains filtered or unexported fields
}

Stack is a generic stack.

func NewStack

func NewStack[T any]() *Stack[T]

NewStack creates a new, empty stack.

func (*Stack[T]) Empty

func (s *Stack[T]) Empty() bool

Empty returns whether the stack is empty.

func (*Stack[T]) Len

func (s *Stack[T]) Len() int

Len returns the size of the stack.

func (*Stack[T]) Peek

func (s *Stack[T]) Peek() (value T)

Peek returns the top value on the stack without removing it.

func (*Stack[T]) PeekDepth

func (s *Stack[T]) PeekDepth(depth int) (value T)

PeekDepth returns the n-th value from the top. PeekDepth(0) is equivalent to the standard Peek().

func (*Stack[T]) PeekReference

func (s *Stack[T]) PeekReference() *T

PeekReference returns a reference to the top value on the stack without removing it.

func (*Stack[T]) Pop

func (s *Stack[T]) Pop() (value T)

Pop returns the top value on the stack while also removing it from the stack.

func (*Stack[T]) Push

func (s *Stack[T]) Push(value T)

Push adds the given value to the stack.

type Writer added in v0.7.5

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

Writer handles type-safe writing into a byte buffer, which may later be read from using Reader. The Writer will automatically grow as it is written to. The serialized forms of booleans, ints, uints, and floats are byte-comparable, meaning it is valid to use bytes.Compare() without needing to deserialize them. Variable-length encoded values, strings, and slices are not byte-comparable. This is not safe for concurrent use.

func NewWriter added in v0.7.5

func NewWriter(capacity uint64) *Writer

NewWriter creates a new Writer with the given starting capacity. A larger starting capacity reduces reallocations at the cost of potentially wasted memory.

func (*Writer) Bool added in v0.7.5

func (writer *Writer) Bool(val bool)

Bool writes a bool.

func (*Writer) BoolSlice added in v0.7.5

func (writer *Writer) BoolSlice(vals []bool)

BoolSlice writes a bool slice.

func (*Writer) Byte added in v0.7.5

func (writer *Writer) Byte(val byte)

Byte writes a byte. This is equivalent to Uint8, but is included since it is more common to refer to a byte rather than a uint8.

func (*Writer) ByteSlice added in v0.7.5

func (writer *Writer) ByteSlice(vals []byte)

ByteSlice writes a byte slice. This is equivalent to Uint8Slice, but is included since it is more common to refer to byte slices than uint8 slices.

func (*Writer) Data added in v0.7.5

func (writer *Writer) Data() []byte

Data returns the data written to the Writer.

func (*Writer) Float32 added in v0.7.5

func (writer *Writer) Float32(val float32)

Float32 writes a float32.

func (*Writer) Float32Slice added in v0.7.5

func (writer *Writer) Float32Slice(vals []float32)

Float32Slice writes a float32 slice.

func (*Writer) Float64 added in v0.7.5

func (writer *Writer) Float64(val float64)

Float64 writes a float64.

func (*Writer) Float64Slice added in v0.7.5

func (writer *Writer) Float64Slice(vals []float64)

Float64Slice writes a float64 slice.

func (*Writer) Int16 added in v0.7.5

func (writer *Writer) Int16(val int16)

Int16 writes an int16.

func (*Writer) Int16Slice added in v0.7.5

func (writer *Writer) Int16Slice(vals []int16)

Int16Slice writes an int16 slice.

func (*Writer) Int32 added in v0.7.5

func (writer *Writer) Int32(val int32)

Int32 writes an int32.

func (*Writer) Int32Slice added in v0.7.5

func (writer *Writer) Int32Slice(vals []int32)

Int32Slice writes an int32 slice.

func (*Writer) Int64 added in v0.7.5

func (writer *Writer) Int64(val int64)

Int64 writes an int64.

func (*Writer) Int64Slice added in v0.7.5

func (writer *Writer) Int64Slice(vals []int64)

Int64Slice writes an int64 slice.

func (*Writer) Int8 added in v0.7.5

func (writer *Writer) Int8(val int8)

Int8 writes an int8.

func (*Writer) Int8Slice added in v0.7.5

func (writer *Writer) Int8Slice(vals []int8)

Int8Slice writes an int8 slice.

func (*Writer) String added in v0.7.5

func (writer *Writer) String(val string)

String writes a string.

func (*Writer) StringSlice added in v0.7.5

func (writer *Writer) StringSlice(vals []string)

StringSlice writes a string slice.

func (*Writer) Uint16 added in v0.7.5

func (writer *Writer) Uint16(val uint16)

Uint16 writes a uint16.

func (*Writer) Uint16Slice added in v0.7.5

func (writer *Writer) Uint16Slice(vals []uint16)

Uint16Slice writes a uint16 slice.

func (*Writer) Uint32 added in v0.7.5

func (writer *Writer) Uint32(val uint32)

Uint32 writes a uint32.

func (*Writer) Uint32Slice added in v0.7.5

func (writer *Writer) Uint32Slice(vals []uint32)

Uint32Slice writes a uint32 slice.

func (*Writer) Uint64 added in v0.7.5

func (writer *Writer) Uint64(val uint64)

Uint64 writes a uint64.

func (*Writer) Uint64Slice added in v0.7.5

func (writer *Writer) Uint64Slice(vals []uint64)

Uint64Slice writes a uint64 slice.

func (*Writer) Uint8 added in v0.7.5

func (writer *Writer) Uint8(val uint8)

Uint8 writes a uint8.

func (*Writer) Uint8Slice added in v0.7.5

func (writer *Writer) Uint8Slice(vals []uint8)

Uint8Slice writes a uint8 slice.

func (*Writer) VariableInt added in v0.7.5

func (writer *Writer) VariableInt(val int64)

VariableInt writes an int64 using variable-length encoding. Smaller values use less space at the cost of larger values using more bytes, but this is generally more space-efficient. This does carry a small computational hit when reading.

func (*Writer) VariableIntSlice added in v0.7.5

func (writer *Writer) VariableIntSlice(vals []int64)

VariableIntSlice writes an int64 slice using variable-length encoding. Smaller values use less space at the cost of larger values using more space, but this is generally more space-efficient. This does carry a computational hit when reading.

func (*Writer) VariableUint added in v0.7.5

func (writer *Writer) VariableUint(val uint64)

VariableUint writes a uint64 using variable-length encoding. Smaller values use less space at the cost of larger values using more bytes, but this is generally more space-efficient. This does carry a small computational hit when reading.

func (*Writer) VariableUintSlice added in v0.7.5

func (writer *Writer) VariableUintSlice(vals []uint64)

VariableUintSlice writes a uint64 slice using variable-length encoding. Smaller values use less space at the cost of larger values using more space, but this is generally more space-efficient. This does carry a computational hit when reading.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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