mapx

package
v0.0.0-...-ffc4fba Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2026 License: Apache-2.0, Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package mapx provides generic map operations: clone, merge, and sorted-key extraction.

Index

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

func SortAndLimit[T any](items []T, less func(a, b T) bool, limit int) []T

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

func SortedKeys[K cmp.Ordered, V any](m map[K]V) []K

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.

Types

type Numeric

type Numeric interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 |
		~float32 | ~float64
}

Numeric is the constraint for types that support the += operator.

Jump to

Keyboard shortcuts

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