Documentation
¶
Overview ¶
Package mapx provides generic map operations: clone, merge, and sorted-key extraction.
Index ¶
- func BuildLookupSet[T comparable](items []T) map[T]struct{}
- func CloneFunc[K comparable, V any](m map[K]V, cloneV func(V) V) map[K]V
- func CloneNested[K1, K2 comparable, V any](m map[K1]map[K2]V) map[K1]map[K2]V
- func EstimateMapSize[K comparable, V any](m map[K]V, entryBytes int) int64
- func MergeAdditive[K comparable, V Numeric](dst, src map[K]V)
- func MergeNestedAdditive[K1, K2 comparable, V Numeric](dst, src map[K1]map[K2]V)
- func SortAndLimit[T any](items []T, less func(a, b T) bool, limit int) []T
- func SortedKeys[K cmp.Ordered, V any](m map[K]V) []K
- func Unique[T comparable](s []T) []T
- type Numeric
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildLookupSet ¶
func BuildLookupSet[T comparable](items []T) map[T]struct{}
BuildLookupSet converts a slice into a lookup set (map[T]struct{}). Duplicate items are silently deduplicated. Returns nil for a nil slice.
func CloneFunc ¶
func CloneFunc[K comparable, V any](m map[K]V, cloneV func(V) V) map[K]V
CloneFunc returns a deep copy of m, applying cloneV to each value. Returns nil for a nil map.
func CloneNested ¶
func CloneNested[K1, K2 comparable, V any](m map[K1]map[K2]V) map[K1]map[K2]V
CloneNested returns a deep copy of a two-level nested map. Outer and inner maps are independently allocated. Returns nil for a nil map. Nil inner maps are preserved as nil.
func EstimateMapSize ¶
func EstimateMapSize[K comparable, V any](m map[K]V, entryBytes int) int64
EstimateMapSize estimates memory usage of m assuming entryBytes per entry.
func MergeAdditive ¶
func MergeAdditive[K comparable, V Numeric](dst, src map[K]V)
MergeAdditive additively merges src into dst: dst[k] += src[k] for every key in src. If dst is nil, this is a no-op.
func MergeNestedAdditive ¶
func MergeNestedAdditive[K1, K2 comparable, V Numeric](dst, src map[K1]map[K2]V)
MergeNestedAdditive merges src into dst for two-level maps. For each key k1 in src with a non-empty inner map, the inner map is merged additively into dst[k1] via MergeAdditive. If dst[k1] is nil it is initialized. Empty inner maps in src are skipped. If dst is nil this is a no-op.
func SortAndLimit ¶
SortAndLimit copies items, sorts the copy using less, and returns at most limit elements. Returns nil for a nil slice. If limit <= 0, returns an empty slice.
func SortedKeys ¶
SortedKeys returns the keys of m in sorted order. Returns nil for a nil map.
func Unique ¶
func Unique[T comparable](s []T) []T
Unique returns a new slice containing only the first occurrence of each element. Insertion order is preserved. Returns nil for a nil slice.