Documentation
¶
Overview ¶
Package gmap Author: hyphen Copyright 2023 hyphen. All rights reserved. Create-time: 2023/12/8
Index ¶
- func Clone[K comparable, V any, M ~map[K]V](m M) M
- func CollectKey[K comparable, V any](m map[K]V) []K
- func CollectValue[K comparable, V any](m map[K]V) []V
- func ContainsAll[K comparable, V any](m map[K]V, ks ...K) bool
- func ContainsAny[K comparable, V any](m map[K]V, ks ...K) bool
- func ContainsMapAll[K, V comparable, M ~map[K]V](parent, child M) bool
- func ContainsMapAny[K, V comparable, M ~map[K]V](parent, child M) bool
- func ForEach[K comparable, V any](m map[K]V, fc func(K, V))
- func Map[K1, K2 comparable, V1, V2 any](m map[K1]V1, fc func(K1, V1) (K2, V2)) map[K2]V2
- func Reverse[K, V comparable](m map[K]V) map[V]K
- func SafeStore[K comparable, V any, M ~map[K]V](m M, k K, v V) M
- func ToSlice[K comparable, V, T any](m map[K]V, fc KVTrans[K, V, T]) []T
- func Union[K comparable, V any](ms ...map[K]V) map[K]V
- func UnionOnConflict[K comparable, V any, M ~map[K]V](ms []M, fc OnConflict[K, V]) map[K]V
- func UseKey[K any, V any](k K, v V) K
- func UseNew[K any, V any](k K, old, new V) V
- func UseOld[K any, V any](k K, old, new V) V
- func UsePair[K any, V any](k K, v V) *gutils.Pair[K, V]
- func UseValue[K any, V any](k K, v V) V
- func UseZero[K any, V any](k K, old, new V) (v V)
- type KVTrans
- type OnConflict
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Clone ¶ added in v0.7.0
func Clone[K comparable, V any, M ~map[K]V](m M) M
Clone creates a shallow copy of map m.
EXAMPLE:
m := map[int]string{1: "a", 2: "b"} Clone(m) => map[int]string{1: "a", 2: "b"}
func CollectKey ¶
func CollectKey[K comparable, V any](m map[K]V) []K
CollectKey collects all keys from map m into a slice.
EXAMPLE:
m := map[int]string{1: "a", 2: "b"} CollectKey(m) => []int{1, 2}
func CollectValue ¶
func CollectValue[K comparable, V any](m map[K]V) []V
CollectValue collects all values from map m into a slice.
EXAMPLE:
m := map[int]string{1: "a", 2: "b"} CollectValue(m) => []string{"a", "b"}
func ContainsAll ¶ added in v0.3.0
func ContainsAll[K comparable, V any](m map[K]V, ks ...K) bool
ContainsAll checks if all keys ks exist in map m.
EXAMPLE:
m := map[int]string{1: "a", 2: "b"} ContainsAll(m, 1, 2) => true ContainsAll(m, 1, 3) => false
func ContainsAny ¶ added in v0.3.0
func ContainsAny[K comparable, V any](m map[K]V, ks ...K) bool
ContainsAny checks if any key in ks exists in map m.
EXAMPLE:
m := map[int]string{1: "a", 2: "b"} ContainsAny(m, 1, 3) => true ContainsAny(m, 3, 4) => false
func ContainsMapAll ¶ added in v0.3.0
func ContainsMapAll[K, V comparable, M ~map[K]V](parent, child M) bool
ContainsMapAll checks if all key-value pairs in child exist in parent.
EXAMPLE:
parent := map[int]string{1: "a", 2: "b"} child := map[int]string{1: "a"} ContainsMapAll(parent, child) => true ContainsMapAll(parent, map[int]string{1: "c"}) => false
func ContainsMapAny ¶ added in v0.3.0
func ContainsMapAny[K, V comparable, M ~map[K]V](parent, child M) bool
ContainsMapAny checks if any key-value pair in child exists in parent.
EXAMPLE:
parent := map[int]string{1: "a", 2: "b"} child := map[int]string{1: "a"} ContainsMapAny(parent, child) => true ContainsMapAny(parent, map[int]string{1: "c"}) => false
func ForEach ¶
func ForEach[K comparable, V any](m map[K]V, fc func(K, V))
ForEach applies function fc to each key and value of map m.
EXAMPLE:
m := map[int]string{1: "a", 2: "b"} ForEach(m, func(k int, v string) { fmt.Printf("%d:%s ", k, v) }) => Output: "1:a 2:b "
func Map ¶
func Map[K1, K2 comparable, V1, V2 any](m map[K1]V1, fc func(K1, V1) (K2, V2)) map[K2]V2
Map applies function fc to each key and value of map m. Results of fc are returned as a new map.
EXAMPLE:
f := func(k, v int) (string, string) { return strconv.Itoa(k), strconv.Itoa(v) } Map(map[int]int{1: 1}, f) => map[string]string{"1": "1"} Map(map[int]int{}, f) => map[string]string{}
HINT:
- Use [MapKeys] if you only need to map the keys.
- Use [MapValues] if you only need to map the values.
- Use [FilterMap] if you also want to ignore keys/values during mapping.
- Use ToSlice if you want to "map" both key and value to single element
func Reverse ¶
func Reverse[K, V comparable](m map[K]V) map[V]K
Reverse swaps keys and values in map m and returns a new map.
EXAMPLE:
m := map[int]string{1: "a", 2: "b"} Reverse(m) => map[string]int{"a": 1, "b": 2}
func SafeStore ¶
func SafeStore[K comparable, V any, M ~map[K]V](m M, k K, v V) M
SafeStore safely stores a key-value pair in map m. If m is nil, it initializes the map.
EXAMPLE:
var m map[int]string SafeStore(m, 1, "a") => m = map[int]string{1: "a"}
func ToSlice ¶
func ToSlice[K comparable, V, T any](m map[K]V, fc KVTrans[K, V, T]) []T
ToSlice applies function fc to each key and value of map m and returns a slice of results.
EXAMPLE:
m := map[int]string{1: "a", 2: "b"} ToSlice(m, func(k int, v string) string { return fmt.Sprintf("%d:%s", k, v) }) => []string{"1:a", "2:b"}
func Union ¶
func Union[K comparable, V any](ms ...map[K]V) map[K]V
Union merges multiple maps into a single map. If the same key exists in multiple maps, the value from the last map is used.
EXAMPLE:
m1 := map[int]string{1: "a", 2: "b"} m2 := map[int]string{2: "c", 3: "d"} Union(m1, m2) => map[int]string{1: "a", 2: "c", 3: "d"}
HINT:
- Use UnionOnConflict if you need custom conflict resolution.
func UnionOnConflict ¶
func UnionOnConflict[K comparable, V any, M ~map[K]V](ms []M, fc OnConflict[K, V]) map[K]V
UnionOnConflict merges multiple maps into a single map with custom conflict resolution. If the same key exists in multiple maps, the provided function fc is used to resolve the conflict.
EXAMPLE:
m1 := map[int]string{1: "a", 2: "b"} m2 := map[int]string{2: "c", 3: "d"} fc := func(k int, old, new string) string { return old + new } UnionOnConflict([]map[int]string{m1, m2}, fc) => map[int]string{1: "a", 2: "bc", 3: "d"}
HINT:
func UseKey ¶
UseKey returns the key as the result of the transformation.
EXAMPLE:
UseKey(1, "a") => 1
func UseNew ¶
UseNew resolves conflicts by always using the new value.
EXAMPLE:
UseNew(1, "old", "new") => "new"
func UseOld ¶
UseOld resolves conflicts by always using the old value.
EXAMPLE:
UseOld(1, "old", "new") => "old"
func UsePair ¶
UsePair returns a pair of key and value as the result of the transformation.
EXAMPLE:
UsePair(1, "a") => &gutils.Pair{Key: 1, Value: "a"}
Types ¶
type OnConflict ¶
OnConflict defines a function type for resolving key conflicts during map merging.
EXAMPLE:
fc := func(k int, old, new string) string { return old + new } UnionOnConflict([]map[int]string{m1, m2}, fc)