maps

package
v0.6.8 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2022 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalcMerge added in v0.2.0

func CalcMerge[M1 ~map[K]V, M2 ~map[K]V, K comparable, V comparable](dst M1, src M2) (create, overwrite, conflicts map[K]struct{})

CalcMerge calculates statistics for merging src into dst.

  • Data in both dst and src remains unchanged
  • Statistics is returned as sets of keys `map[K]struct{}`
  • Keys in src that are not in dst are returned in the `create` set
  • Keys/value pairs that are equal in both src and dst are returned in the `overwrite` set
  • Keys that have different values in src and dst are returned in the `conflicts` set

func CalcMergeFunc added in v0.2.0

func CalcMergeFunc[M1 ~map[K]V1, M2 ~map[K]V2, K comparable, V1, V2 any](dst M1, src M2, allow func(key K, dstval V1, srcval V2) bool) (create, overwrite, conflicts map[K]struct{})

CalcMergeFunc provides the same functionality as CalcMerge, but uses the allow functor to determine if a value can be overwritten. Notice also, that both dst and src maps in CalcMergeFunc are allowed to have different value types, which can help in merging heterogeneous data.

func EqualKeys added in v0.6.7

func EqualKeys[M1 ~map[K]V1, M2 ~map[K]V2, K comparable, V1 any, V2 any](m1 M1, m2 M2) bool

Equal reports whether two maps contain the same keys.

func HasDuplicates added in v0.4.0

func HasDuplicates[M ~map[K]V, K comparable, V comparable](m M) bool

HasDuplicates checks whether m contains duplicates (multiple keys having the same value).

func Insert added in v0.5.0

func Insert[M ~map[K]V, K comparable, V any](m M, pairs ...*Pair[K, V])

Insert copies key-value pairs into m, if the map doesn't already contain an elements with equivalent keys.

Example
m := map[int]string{
	1: "one",
	2: "two",
	3: "three",
	6: "six",
}

type pair = Pair[int, string]
Insert(m, &pair{3, "THREE"}, &pair{4, "FOUR"})

m2 := map[int]string{
	1: "UNO",
	2: "DOS",
	3: "TRES",
	5: "CINCO",
}
Insert(m, Pairs(m2)...)
for _, p := range SortedByKey(m) {
	fmt.Printf("%d: %s\n", p.Key, p.Val)
}
Output:

1: one
2: two
3: three
4: FOUR
5: CINCO
6: six

func InsertOrOverwrite added in v0.5.0

func InsertOrOverwrite[M ~map[K]V, K comparable, V any](m M, pairs ...*Pair[K, V])

InsertOrOverwrite copies key-value pairs into m, overwriting existing elements with equivalent keys.

Example
m := map[int]string{
	1: "one",
	2: "two",
	3: "three",
	6: "six",
}

type pair = Pair[int, string]
InsertOrOverwrite(m, &pair{3, "THREE"}, &pair{4, "FOUR"})

m2 := map[int]string{
	1: "UNO",
	2: "DOS",
	3: "TRES",
	5: "CINCO",
}
InsertOrOverwrite(m, Pairs(m2)...)
for _, p := range SortedByKey(m) {
	fmt.Printf("%d: %s\n", p.Key, p.Val)
}
Output:

1: UNO
2: DOS
3: TRES
4: FOUR
5: CINCO
6: six

func Inverted added in v0.4.0

func Inverted[M ~map[K]V, K comparable, V comparable](m M) (inverted map[V]K, duplicates map[V]struct{})

Inverted produces inverted map from m. Entries that can not be inverted are returned as a set of duplicates, they are excluded from the inverted result:

A strategy for resolving the issues with duplicates then may include iterating over the returned set of duplicates, possibly calling the MatchValue function to discover which keys are associated to each duplicate and taking appropriate actions.

Example
m := map[string]int{
	"one":                      1,
	"two":                      2,
	"three":                    3,
	"fourty two":               42,
	"the answer to everything": 42,
}

inverted, duplicates := Inverted(m)

fmt.Printf("\nINVERTED\n")
for _, p := range SortedByKey(inverted) {
	fmt.Printf("%d: %s\n", p.Key, p.Val)
}
fmt.Printf("\nDUPLICATES\n")
for v := range duplicates {
	matching_keys := MatchValue(m, v)
	as_slice := sets.Sorted(matching_keys)
	fmt.Printf("%d: %s\n", v, strings.Join(as_slice, ", "))
}
Output:


INVERTED
1: one
2: two
3: three

DUPLICATES
42: fourty two, the answer to everything

func KeySet

func KeySet[M ~map[K]V, K comparable, V any](m M) map[K]struct{}

KeySet returns a set constructed from all the keys in m.

func MatchValue added in v0.4.0

func MatchValue[M ~map[K]V, K comparable, V comparable](m M, v V) (keys map[K]struct{})

MatchValue returns a set of keys that contain the same value v.

func Merge added in v0.1.0

func Merge[M1 ~map[K]V, M2 ~map[K]V, K comparable, V comparable](dst M1, src M2) (conflicts M2)

Merge copies key/value pairs in src adding them to dst. When a key from src is already in dst and the associated values are different, instead of overwriting, the whole key/value pair from src is copied to conflicts.

Upon completion of this routine, the caller may analyze the conflicted keys and:

  • Discard and reject the conflicts
  • Overwrite the dst, for example by calling golang.org/x/exp/maps.Copy routine
  • Implement more granular solution by merging each value individually
Example
m := map[int]string{
	1: "one",
	2: "two",
	3: "three",
	4: "four",
}
m2 := map[int]string{
	2: "TWO",
	3: "THREE",
	4: "four",
	5: "five",
}

conflicts := Merge(m, m2)

fmt.Printf("\nMERGED\n")
for _, p := range SortedByKey(m) {
	fmt.Printf("%d: %s\n", p.Key, p.Val)
}
fmt.Printf("\nCONFLICTS\n")
for _, p := range SortedByKey(conflicts) {
	fmt.Printf("%d: %s\n", p.Key, p.Val)
}
Output:


MERGED
1: one
2: two
3: three
4: four
5: five

CONFLICTS
2: TWO
3: THREE

func MergeFunc added in v0.1.0

func MergeFunc[M1 ~map[K]V, M2 ~map[K]V, K comparable, V any](dst M1, src M2, allow func(dstval, srcval V) bool) (conflicts M2)

MergeFunc provides the same functionality as Merge, but uses the allow functor to determine if a value can be overwritten.

func Sliced

func Sliced[M ~map[K]V, S ~map[K]struct{}, K comparable, V any](m M, s S) M

Sliced returns elements from m that exist in s.

Example
m := map[int]string{
	1: "one",
	2: "two",
	3: "three",
	4: "four",
}

s := map[int]struct{}{
	2: {},
	4: {},
}

for _, p := range SortedByKey(Sliced(m, s)) {
	fmt.Printf("%d: %s\n", p.Key, p.Val)
}
Output:

2: two
4: four

func SortedKeys added in v0.6.5

func SortedKeys[M ~map[K]V, K constraints.Ordered, V any](m M) []K

SortedKeys returns sorted keys of the map m. This sort is guaranteed to be stable because the keys are unique.

func SortedKeysFunc added in v0.6.6

func SortedKeysFunc[M ~map[K]V, K comparable, V any](m M, less func(a, b K) bool) []K

SortedKeysFunc returns sorted keys of the map m as determined by the less function. This sort is stable provided the less function produces stable results.

func ValueSet added in v0.4.0

func ValueSet[M ~map[K]V, K comparable, V comparable](m M) map[V]struct{}

ValueSet returns a set constructed from all the values in m.

Types

type Pair

type Pair[K any, V any] struct {
	Key K
	Val V
}

Pair is a key-value pair that can be used to flatten maps.

func Pairs

func Pairs[M ~map[K]V, K comparable, V any](m M) []*Pair[K, V]

Pairs returns a slice of key-value pairs constructed from m. The pairs will be in an indeterminate order.

func SortedByKey added in v0.4.0

func SortedByKey[M ~map[K]V, K constraints.Ordered, V any](m M) []*Pair[K, V]

SortedByKey returns a slice of key-value pairs constructed from m and sorted by key. This sort is guaranteed to be stable because the keys are unique.

Example
m := map[int]string{
	1: "one",
	2: "two",
	3: "three",
	4: "four",
}

for _, p := range SortedByKey(m) {
	fmt.Printf("%d: %s\n", p.Key, p.Val)
}
Output:

1: one
2: two
3: three
4: four

func SortedByKeyFunc added in v0.4.0

func SortedByKeyFunc[M ~map[K]V, K comparable, V any](m M, less func(a, b K) bool) []*Pair[K, V]

SortedByKeyFunc returns a slice of key-value pairs constructed from m and sorted by key as determined by the less function. This sort is stable provided the less function produces stable results.

func SortedByVal added in v0.4.0

func SortedByVal[M ~map[K]V, K comparable, V constraints.Ordered](m M) []*Pair[K, V]

SortedByVal returns a slice of key-value pairs constructed from m and sorted by value. This sort is stable only if there is no duplicates (all values are unique).

func SortedByValFunc added in v0.4.0

func SortedByValFunc[M ~map[K]V, K comparable, V any](m M, less func(a, b V) bool) []*Pair[K, V]

SortedByValFunc returns a slice of key-value pairs constructed from m and sorted by value as determined by the less function. This sort is stable provided there is no duplicates (all values are unique) and the less function produces stable results.

func SortedFunc added in v0.4.0

func SortedFunc[M ~map[K]V, K comparable, V any](m M, less func(a, b *Pair[K, V]) bool) []*Pair[K, V]

SortedFunc returns a slice of key-value pairs constructed from m and sorted as determined by the less function. The sort is stable if the less function produces stable results.

func StableSortedByVal

func StableSortedByVal[M ~map[K]V, K constraints.Ordered, V constraints.Ordered](m M) []*Pair[K, V]

StableSortedByVal returns a slice of key-value pairs constructed from m and sorted by value. For duplicate values, sorting falls back to comparing keys. This sort is guaranteed to be stable.

Example
m := map[int]string{
	1: "D",
	2: "C",
	3: "B",
	4: "A",
}

for _, p := range StableSortedByVal(m) {
	fmt.Printf("%d: %s\n", p.Key, p.Val)
}
Output:

4: A
3: B
2: C
1: D

func StableSortedByValFunc

func StableSortedByValFunc[M ~map[K]V, K constraints.Ordered, V any](m M, less func(a, b V) bool) []*Pair[K, V]

SortedByValFunc returns a slice of key-value pairs constructed from m and sorted by value as determined by the less function. For duplicate values, sorting falls back to comparing keys. This sort is stable provided the less function produces stable results.

Jump to

Keyboard shortcuts

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