util

package
v0.0.0-...-aca9518 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2024 License: Apache-2.0 Imports: 4 Imported by: 17

Documentation

Overview

Utils to complement the standard library.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendSliceCopy

func AppendSliceCopy[T any](immutable []T, suffix ...T) []T

Similar to append(), but always reallocates exactly one new slice, and never mutates the buffer behind `immutable` even if there is remaining capacity.

func AtomicExtrema

func AtomicExtrema[Base any](
	ptr Atomic[Base],
	other Base,
	rightIsBetter func(Base, Base) bool,
) (_stored bool)

func AtomicMax

func AtomicMax[Base constraints.Ordered](ptr Atomic[Base], other Base) (_stored bool)

func AtomicMin

func AtomicMin[Base constraints.Ordered](ptr Atomic[Base], other Base) (_stored bool)

func ClearSlice

func ClearSlice[T any](slice *[]T)

Clears the slice without deallocating its capacity, freeing any possible underlying pointers for GC.

func ClosedChan

func ClosedChan[T any]() <-chan T

func ContextWithStopCh

func ContextWithStopCh(ctx context.Context, ch <-chan Empty) context.Context

func CountSlice

func CountSlice[T any](input []T, predicate func(T) bool) int

func DrainSliceOrdered

func DrainSliceOrdered[T any](slice *[]T, predicate func(T) bool)

Removes items in the slice that do not match the predicate, preserving order of other items.

func DrainSliceUnordered

func DrainSliceUnordered[T any](slice *[]T, predicate func(T) bool)

Swap-removes items in the slice that do not match the predicate.

func FindInSlice

func FindInSlice[T comparable](input []T, needle T) int

Returns the first occurrence of needle in input, or -1 if not found.

func FindInSliceWith

func FindInSliceWith[T any](input []T, predicate func(T) bool) int

Returns the first occurrence in input where predicate returns true, or -1 if not found.

func ForEachRef

func ForEachRef[T any](slice []T, eachFn func(*T))

Execute `eachFn` on each item of `slice` by reference.

func GetArbitraryMapEntry

func GetArbitraryMapEntry[K comparable, V any](map_ map[K]V) (K, V, bool)

Returns an arbitrary map entry and true, or zero KV and false if the map is empty.

func GetOrAppend

func GetOrAppend[T any](slice *[]T, predicate func(*T) bool) *T

Returns a pointer to a slice entry satisfying the condition, appending a new zero entry if none satisfies.

func GetOrAppendSliceWith

func GetOrAppendSliceWith[T any](slice *[]T, predicate func(*T) bool, ctor func() T) *T

Similar to GetOrAppend, but appends an entry from the ctor function.

func Identity

func Identity[T any](t T) T

func IsZero

func IsZero[T comparable](value T) bool

func MapSlice

func MapSlice[T any, U any](input []T, fn func(T) U) []U

func NewLateInit

func NewLateInit[T any]() (_reader LateInitReader[T], _writer LateInitWriter[T])

func NoOp

func NoOp()

func NotifyOnBlock

func NotifyOnBlock[T any](blockNotifier BlockNotifier, ch <-chan T) (T, bool)

Receives a value from `ch`. If this was a blocking operation, also calls the OnBlock/OnUnblock hooks on `blockNotifier`.

func RoundedIntDiv

func RoundedIntDiv[T constraints.Integer](numerator, divisor T) T

Returns the nearest integer to {numerator / divisor} with half-positive rounding.

func SelectOrNotifyRR

func SelectOrNotifyRR[T1, T2 any, R any](
	blockNotifier BlockNotifier,
	ch1 <-chan T1, then1 func(T1, bool) R,
	ch2 <-chan T2, then2 func(T2, bool) R,
) R

Select-recvs from one of the channels, and call the specified function if it blocks (all channels have no data).

func SelectOrNotifyRRR

func SelectOrNotifyRRR[T1, T2, T3 any, R any](
	blockNotifier BlockNotifier,
	ch1 <-chan T1, then1 func(T1, bool) R,
	ch2 <-chan T2, then2 func(T2, bool) R,
	ch3 <-chan T3, then3 func(T3, bool) R,
) R

Select-recvs from one of the channels, and call the specified function if it blocks (all channels have no data).

func SliceContains

func SliceContains[T comparable](input []T, needle T) bool

func SliceToMap

func SliceToMap[K comparable, V any](input []V, keyFn func(V) K) map[K]V

func SwapRemove

func SwapRemove[T any](input *[]T, i int)

Removes the item at index i from input, moving the last item to its original position.

func Type

func Type[T any]() reflect.Type

func TypeName

func TypeName[T any]() string

func Zero

func Zero[T any]() T

Returns a zero value to be initialized later.

Types

type Atomic

type Atomic[Base any] interface {
	CompareAndSwap(oldValue, newValue Base) (swapped bool)
	Load() Base
	Store(newValue Base)
	Swap(newValue Base) (oldValue Base)
}

type BlockNotifier

type BlockNotifier interface {
	OnBlock()
	OnUnblock()
}

type Empty

type Empty = struct{}

type LateInitReader

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

func (LateInitReader[T]) Get

func (reader LateInitReader[T]) Get() T

type LateInitWriter

type LateInitWriter[T any] func(T)

type TryRecvResult

type TryRecvResult[T any] struct {
	Received     bool
	ReceivedData T

	ChanClosed bool
	NoData     bool
}

func TryRecv

func TryRecv[T any](ch <-chan T) TryRecvResult[T]

Jump to

Keyboard shortcuts

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