utils

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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 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 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.

Jump to

Keyboard shortcuts

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