| 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. |
| 4 | |
| 5 | // Package maps defines various functions useful with maps of any type. |
| 6 | package maps |
| 7 | |
| 8 | import "maps" |
| 9 | |
| 10 | // Keys returns the keys of the map m. |
| 11 | // The keys will be in an indeterminate order. |
| 12 | // |
| 13 | // The simplest true equivalent using the standard library is: |
| 14 | // |
| 15 | // slices.AppendSeq(make([]K, 0, len(m)), maps.Keys(m)) |
| 16 | func Keys[M ~map[K]V, K comparable, V any](m M) []K { |
| 17 | |
| 18 | r := make([]K, 0, len(m)) |
| 19 | for k := range m { |
| 20 | r = append(r, k) |
| 21 | } |
| 22 | return r |
| 23 | } |
| 24 | |
| 25 | // Values returns the values of the map m. |
| 26 | // The values will be in an indeterminate order. |
| 27 | // |
| 28 | // The simplest true equivalent using the standard library is: |
| 29 | // |
| 30 | // slices.AppendSeq(make([]V, 0, len(m)), maps.Values(m)) |
| 31 | func Values[M ~map[K]V, K comparable, V any](m M) []V { |
| 32 | |
| 33 | r := make([]V, 0, len(m)) |
| 34 | for _, v := range m { |
| 35 | r = append(r, v) |
| 36 | } |
| 37 | return r |
| 38 | } |
| 39 | |
| 40 | // Equal reports whether two maps contain the same key/value pairs. |
| 41 | // Values are compared using ==. |
| 42 | // |
| 43 | //go:fix inline |
| 44 | func Equal[M1, M2 ~map[K]V, K, V comparable](m1 M1, m2 M2) bool { |
| 45 | return maps.Equal(m1, m2) |
| 46 | } |
| 47 | |
| 48 | // EqualFunc is like Equal, but compares values using eq. |
| 49 | // Keys are still compared with ==. |
| 50 | // |
| 51 | //go:fix inline |
| 52 | 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 { |
| 53 | return maps.EqualFunc(m1, m2, eq) |
| 54 | } |
| 55 | |
| 56 | // Clear removes all entries from m, leaving it empty. |
| 57 | // |
| 58 | //go:fix inline |
| 59 | func Clear[M ~map[K]V, K comparable, V any](m M) { |
| 60 | clear(m) |
| 61 | } |
| 62 | |
| 63 | // Clone returns a copy of m. This is a shallow clone: |
| 64 | // the new keys and values are set using ordinary assignment. |
| 65 | // |
| 66 | //go:fix inline |
| 67 | func Clone[M ~map[K]V, K comparable, V any](m M) M { |
| 68 | return maps.Clone(m) |
| 69 | } |
| 70 | |
| 71 | // Copy copies all key/value pairs in src adding them to dst. |
| 72 | // When a key in src is already present in dst, |
| 73 | // the value in dst will be overwritten by the value associated |
| 74 | // with the key in src. |
| 75 | // |
| 76 | //go:fix inline |
| 77 | func Copy[M1 ~map[K]V, M2 ~map[K]V, K comparable, V any](dst M1, src M2) { |
| 78 | maps.Copy(dst, src) |
| 79 | } |
| 80 | |
| 81 | // DeleteFunc deletes any key/value pairs from m for which del returns true. |
| 82 | // |
| 83 | //go:fix inline |
| 84 | func DeleteFunc[M ~map[K]V, K comparable, V any](m M, del func(K, V) bool) { |
| 85 | maps.DeleteFunc(m, del) |
| 86 | } |