maps

package
v0.22.7 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply[K comparable, V any, M ~map[K]V](m1 M, m2 M) M

func FlattenValues

func FlattenValues[KO comparable, KI comparable, V any, M ~map[KO]map[KI]V](m M) []V

Takes a "map of maps", extracts all the values from the inner maps, and then flattens them into a single slice.

func HasExactValue

func HasExactValue[K comparable, V comparable, M ~map[K]V](m M, key K, expected V) bool

func Insert

func Insert[K comparable, V comparable, M ~map[K]V](m M, seq iter.Seq2[K, V]) bool

Adds key-value pairs from seq to m. If a key in seq already exists in m, its value will be overwritten. Returns true if the map was modified, otherwise false.

func Keys

func Keys[K comparable, V any, M ~map[K]V](m M) []K

func Map

func Map[K comparable, V any, T any, MF MapFunc[K, V, T], MM ~map[K]V](m MM, mapping MF) map[K]T

Transforms a map[K]V into a map[K]T using given mapping function. Keys are preserved, but values are replaced with the result of the mapping

func MapToSlice

func MapToSlice[T any, K comparable, V any, MF MapFunc[K, V, T], MM ~map[K]V](m MM, mapping MF) []T

Maps a map to a slice. Note that iteration order over a map is not defined, so make no assumptions about the order of the items in the resulting slice.

func Select

func Select[K comparable, V any, SF SelectFunc[K, V], MM ~map[K]V](m MM, selector SF) MM

func SliceToMap

func SliceToMap[T any, K comparable, V any, KVF KeyValFunc[T, K, V], S ~[]T](s S, f KVF) map[K]V

Maps a slice to a map. The slice is processed in order, and entries produced by later elements may override entries produced by earlier elements (if keys are equal). So in general case it should NOT be assumed that the resulting map will contain as many elements as the original slice. SliceToMap(nil,..) returns an empty map, not nil. This is to make the code using this function less error-prone. Nil slices are quite usable (e.g. appending to nil slice works fine). Not so with nil maps.

func TryGetValidValue

func TryGetValidValue[K comparable, V any, M ~map[K]V](m M, key K, validate func(V) bool) (V, bool)

func Values

func Values[K comparable, V any, M ~map[K]V](m M) []V

Types

type DualKeyMap

type DualKeyMap[K1 comparable, K2 comparable, V any] struct {
	// contains filtered or unexported fields
}

func NewDualKeyMap

func NewDualKeyMap[K1 comparable, K2 comparable, V any]() *DualKeyMap[K1, K2, V]

func (*DualKeyMap[K1, K2, V]) Clear

func (m *DualKeyMap[K1, K2, V]) Clear()

func (*DualKeyMap[K1, K2, V]) DebugDump

func (m *DualKeyMap[K1, K2, V]) DebugDump(w io.Writer)

func (*DualKeyMap[K1, K2, V]) DeleteByFirstKey

func (m *DualKeyMap[K1, K2, V]) DeleteByFirstKey(k1 K1)

func (*DualKeyMap[K1, K2, V]) DeleteBySecondKey

func (m *DualKeyMap[K1, K2, V]) DeleteBySecondKey(k2 K2)

func (*DualKeyMap[K1, K2, V]) FindByFirstKey

func (m *DualKeyMap[K1, K2, V]) FindByFirstKey(k1 K1) (K2, V, bool)

func (*DualKeyMap[K1, K2, V]) FindBySecondKey

func (m *DualKeyMap[K1, K2, V]) FindBySecondKey(k2 K2) (K1, V, bool)

func (*DualKeyMap[K1, K2, V]) Range

func (m *DualKeyMap[K1, K2, V]) Range(f func(k1 K1, k2 K2, val V) bool)

func (*DualKeyMap[K1, K2, V]) Store

func (m *DualKeyMap[K1, K2, V]) Store(k1 K1, k2 K2, val V)

func (*DualKeyMap[K1, K2, V]) Update

func (m *DualKeyMap[K1, K2, V]) Update(k1 K1, k2 K2, val V) bool

Update() is like Store(), except it fails if the entry is not already present (for both keys).

func (*DualKeyMap[K1, K2, V]) UpdateChangingFirstKey

func (m *DualKeyMap[K1, K2, V]) UpdateChangingFirstKey(k1 K1, newk1 K1, k2 K2, val V) bool

UpdateChangingFirstKey() is like Update(), except it will make the entry available using a new first key value.

func (*DualKeyMap[K1, K2, V]) UpdateChangingSecondKey

func (m *DualKeyMap[K1, K2, V]) UpdateChangingSecondKey(k1 K1, k2 K2, newk2 K2, val V) bool

UpdateChangingSecondKey() is like Update(), except it will make the entry available using a new second key value.

type KeyValFunc

type KeyValFunc[T any, K comparable, V any] interface {
	~func(T) (K, V)
}

type MapFunc

type MapFunc[K comparable, V any, T any] interface {
	~func(K, V) T | ~func(V) T
}

type SelectFunc

type SelectFunc[K comparable, V any] interface {
	~func(K) bool | ~func(K, V) bool
}

type StringKeyMap

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

StringKeyMap is a map-like data structure with string keys. The keys can be treated in case-sensitive or case-insensitive manner. StringKeyMap is not goroutine-safe.

func NewStringKeyMap

func NewStringKeyMap[T any](mode StringMapMode) StringKeyMap[T]

func (StringKeyMap[T]) Apply

func (skm StringKeyMap[T]) Apply(m map[string]T)

Apply will add all the key-value pairs from the given map to this StringKeyMap. If there are existing keys in the StringKeyMap, they will be overridden.

func (StringKeyMap[T]) Data

func (skm StringKeyMap[T]) Data() map[string]T

Data returns the undelying map, with desired keys. It should not be modified, but it can be used for iteration.

func (StringKeyMap[T]) Delete

func (skm StringKeyMap[T]) Delete(key string)

Delete deletes data that is associated with the given key. It is a no-op if the key does not exist.

func (StringKeyMap[T]) DeletePrefix

func (skm StringKeyMap[T]) DeletePrefix(prefix string)

DeletePrefix deletes all keys that have the given prefix.

func (StringKeyMap[T]) Get

func (skm StringKeyMap[T]) Get(key string) (T, bool)

Gets the data associated with the given key.

func (StringKeyMap[T]) Len

func (skm StringKeyMap[T]) Len() int

func (StringKeyMap[T]) Override

func (skm StringKeyMap[T]) Override(key string, value T)

Override sets the value for the given key. If the key already exists, it is deleted first (making the passed key the new "desired" key).

func (StringKeyMap[T]) Set

func (skm StringKeyMap[T]) Set(key string, value T)

Set sets the value for the given key, preserving desired case of the key if it already exists.

type StringMapMode

type StringMapMode uint32
const (
	StringMapModeCaseSensitive   StringMapMode = 0
	StringMapModeCaseInsensitive StringMapMode = 1
)

Jump to

Keyboard shortcuts

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