1 // Copyright 2021 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package maps defines various functions useful with maps of any type.
8 // Equal reports whether two maps contain the same key/value pairs.
9 // Values are compared using ==.
10 func Equal[M1, M2 ~map[K]V, K, V comparable](m1 M1, m2 M2) bool {
11 if len(m1) != len(m2) {
14 for k, v1 := range m1 {
15 if v2, ok := m2[k]; !ok || v1 != v2 {
22 // EqualFunc is like Equal, but compares values using eq.
23 // Keys are still compared with ==.
24 func EqualFunc[M1 ~map[K]V1, M2 ~map[K]V2, K comparable, V1, V2 any](m1 M1, m2 M2, eq func(V1, V2) bool) bool {
25 if len(m1) != len(m2) {
28 for k, v1 := range m1 {
29 if v2, ok := m2[k]; !ok || !eq(v1, v2) {
36 // clone is implemented in the runtime package.
39 // Clone returns a copy of m. This is a shallow clone:
40 // the new keys and values are set using ordinary assignment.
41 func Clone[M ~map[K]V, K comparable, V any](m M) M {
42 // Preserve nil in case it matters.
49 // Copy copies all key/value pairs in src adding them to dst.
50 // When a key in src is already present in dst,
51 // the value in dst will be overwritten by the value associated
52 // with the key in src.
53 func Copy[M1 ~map[K]V, M2 ~map[K]V, K comparable, V any](dst M1, src M2) {
54 for k, v := range src {
59 // DeleteFunc deletes any key/value pairs from m for which del returns true.
60 func DeleteFunc[M ~map[K]V, K comparable, V any](m M, del func(K, V) bool) {