utils

package
v0.0.0-...-56c637b Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const BitsPerByte = 8

Variables

This section is empty.

Functions

func Accumulate

func Accumulate[T any, U constraints.Ordered](input []T, value func(T) U) U

Reduces a sequence by adding up the value returned by a function applied to each item

func AllOnes

func AllOnes[T constraints.Unsigned](bits int) T

Returns an all ones bitmask of n bits of the given unsigned integer type

func AsciiFrame

func AsciiFrame(fields []AsciiFrameField, frameWidth int, unit string, layout AsciiFrameUnitLayout, leftpad int) string

Prints an ascii diagram of a binary frame composed of contiguous fields of different unit lenghts

func BitCast

func BitCast[To constraints.Integer, From constraints.Integer](from From) To

func Bits

func Bits(bytes int) int

Returns the size in bits of n bytes

func ConcatMap

func ConcatMap[T any, U any](input []T, mapFunction func(T) []U) []U

Generates a sequence composed by the concatenation of all the secuences generated by applying a map function to each element of the input sequence

func ConditionallyReversedRefs

func ConditionallyReversedRefs[T any](input []T, reversed bool) []*T

Returns a sequence of references to the items on an slice, reversed or not based on a condition

func Filter

func Filter[T any](input []T, cond func(T) bool) []T

Returns the subset sequence of all items of a sequence passing a given boolean condition

func FormatSlice

func FormatSlice[T any](input []T, separator string) string

Returns an string containing all formatted sequence items separated by a given separator

func FormatUintBinary

func FormatUintBinary(value uint64, bits int) string

Formats an uint value into a fixed width binary string of n bits

func FormatUintHex

func FormatUintHex(value uint64, bits int) string

Formats an uint value into an fixed width hex string of n characters

func GenMap

func GenMap[T any, Key comparable](input []T, keyFunc func(T) Key) map[Key]T

Generates a map from a sequence of items and a function that generates a key from an item

func GenMapFromKeys

func GenMapFromKeys[T any, Key comparable](keys []Key, itemFunc func(Key) T) map[Key]T

Generates a map from a sequence of keys and a function that generates an item from a key

func Indices

func Indices(n int) []int

Returns a sequence of n indices

func InvertedMap

func InvertedMap[Key comparable, Value comparable](input map[Key]Value) map[Value]Key

Converts a Key -> Value map into a Value -> Key map

func Iota

func Iota[T any](n int, gen func(int) T) []T

Generates a sequence of n elements given a generation function

func Keys

func Keys[Key comparable, Value comparable](input map[Key]Value) []Key

Returns an array with all the keys of a map

func MakeError

func MakeError(err error, detailsBody string, args ...any) error

func Map

func Map[T any, U any](input []T, mapFunction func(T) U) []U

Generates a sequence constructed by applying a function to all elements of a given input sequence

func MapMap

func MapMap[Key comparable, Value comparable, NewKey comparable, NewValue comparable](input map[Key]Value, mapFunction func(Key, Value) (NewKey, NewValue)) map[NewKey]NewValue

Generates a new Map NewKey -> NewValue from a given map Key -> Value and a transformation function (Key, Value) -> (NewKey, NewValue)

func MapMember

func MapMember(name string, items []any) ([]any, error)

Maps a sequence of objects into a sequence of values of a given member of each item of the input sequence

func Max

func Max[T constraints.Ordered](input []T) T

Returns the biggest item of a sequence

func Member

func Member(name string, object any) (any, error)

Returns the value of an object member by name. If the member is a method it is assumed that it has no paramters and gets called to return the value.

func Min

func Min[T constraints.Ordered](input []T) T

Returns the smaller item of a sequence

func Reduce

func Reduce[T any, U any](input []T, foldFunc func(T, U) U) U

Reduces a sequence to a value given an accumulation function

func Refs

func Refs[T any](input []T) []*T

Returns a sequence of references to the items of an slice

func ReversedRefs

func ReversedRefs[T any](input []T) []*T

Returns a sequence of references to the items of an slice in reverse order

func Sizeof

func Sizeof[T any]() int

Returns the size in bytes of values of a type

func SizeofBits

func SizeofBits[T any]() int

Returns the size in bits of values of a type

func Values

func Values[Key comparable, Value comparable](input map[Key]Value) []Value

Returns an array with all the values of a map

Types

type AsciiFrameField

type AsciiFrameField struct {
	// Name of the field
	Name string

	// Units within the frame the field begins from
	Begin int

	// Field width
	Width int
}

func (*AsciiFrameField) PastTopUnit

func (f *AsciiFrameField) PastTopUnit() int

The first unit within the frame used by the next field

func (*AsciiFrameField) TopUnit

func (f *AsciiFrameField) TopUnit() int

The last unit within the frame used by this field

type AsciiFrameUnitLayout

type AsciiFrameUnitLayout uint
const (
	// Units increase left to right
	AsciiFrameUnitLayout_LeftToRight AsciiFrameUnitLayout = iota
	// Units increase right to left
	AsciiFrameUnitLayout_RightToLeft
)

type BitView

type BitView[T constraints.Unsigned] struct {
	Bits *T
}

Implements a read/write view over an unsigned interger, allowing manipullating individual bits easily

func CreateBitView

func CreateBitView[T constraints.Unsigned](value *T) BitView[T]

Creates a bit view out of an unsigned int

func (BitView[T]) ClearBit

func (v BitView[T]) ClearBit(bit int)

Sets bit to 0

func (BitView[T]) ClearBits

func (v BitView[T]) ClearBits(bit int, width int)

Sets all bits in a range to 0

func (BitView[T]) Read

func (v BitView[T]) Read(bit int, width int) T

Extracts a range of bits given a first bit and a width

func (BitView[T]) SetBit

func (v BitView[T]) SetBit(bit int)

Sets bit to 1

func (BitView[T]) SetBits

func (v BitView[T]) SetBits(bit int, width int)

Sets all bits in a range to 1

func (BitView[T]) SizeofBits

func (v BitView[T]) SizeofBits() int

Returns the size in bits of the viewed value

func (BitView[T]) Value

func (v BitView[T]) Value() T

Returns the viewed unsigned int value

func (BitView[T]) Write

func (v BitView[T]) Write(value T, bit int, width int)

Copies a value into a range of bits, given the start and width of the range. All most significant bits of the value not fitting into the destination range are ignored.

type Pair

type Pair[First any, Second any] struct {
	First  First
	Second Second
}

func MakePair

func MakePair[First any, Second any](first First, second Second) Pair[First, Second]

func ZipMap

func ZipMap[Key comparable, Value comparable](input map[Key]Value) []Pair[Key, Value]

Returns an array of pairs (Key, Value) from a given map Key -> Value

func (*Pair[First, Second]) Decompose

func (p *Pair[First, Second]) Decompose() (First, Second)

func (*Pair[First, Second]) String

func (p *Pair[First, Second]) String() string

Jump to

Keyboard shortcuts

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