| 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 a |
| 6 |
|
| 7 |
// SliceEqual reports whether two slices are equal: the same length and all |
| 8 |
// elements equal. All floating point NaNs are considered equal. |
| 9 |
func SliceEqual[Elem comparable](s1, s2 []Elem) bool { |
| 10 |
if len(s1) != len(s2) { |
| 11 |
return false |
| 12 |
} |
| 13 |
for i, v1 := range s1 { |
| 14 |
v2 := s2[i] |
| 15 |
if v1 != v2 { |
| 16 |
isNaN := func(f Elem) bool { return f != f } |
| 17 |
if !isNaN(v1) || !isNaN(v2) { |
| 18 |
return false |
| 19 |
} |
| 20 |
} |
| 21 |
} |
| 22 |
return true |
| 23 |
} |
| 24 |
|
| 25 |
// Keys returns the keys of the map m. |
| 26 |
// The keys will be an indeterminate order. |
| 27 |
func Keys[K comparable, V any](m map[K]V) []K { |
| 28 |
r := make([]K, 0, len(m)) |
| 29 |
for k := range m { |
| 30 |
r = append(r, k) |
| 31 |
} |
| 32 |
return r |
| 33 |
} |
| 34 |
|
| 35 |
// Values returns the values of the map m. |
| 36 |
// The values will be in an indeterminate order. |
| 37 |
func Values[K comparable, V any](m map[K]V) []V { |
| 38 |
r := make([]V, 0, len(m)) |
| 39 |
for _, v := range m { |
| 40 |
r = append(r, v) |
| 41 |
} |
| 42 |
return r |
| 43 |
} |
| 44 |
|
| 45 |
// Equal reports whether two maps contain the same key/value pairs. |
| 46 |
// Values are compared using ==. |
| 47 |
func Equal[K, V comparable](m1, m2 map[K]V) bool { |
| 48 |
if len(m1) != len(m2) { |
| 49 |
return false |
| 50 |
} |
| 51 |
for k, v1 := range m1 { |
| 52 |
if v2, ok := m2[k]; !ok || v1 != v2 { |
| 53 |
return false |
| 54 |
} |
| 55 |
} |
| 56 |
return true |
| 57 |
} |
| 58 |
|
| 59 |
// Copy returns a copy of m. |
| 60 |
func Copy[K comparable, V any](m map[K]V) map[K]V { |
| 61 |
r := make(map[K]V, len(m)) |
| 62 |
for k, v := range m { |
| 63 |
r[k] = v |
| 64 |
} |
| 65 |
return r |
| 66 |
} |
| 67 |
|
| 68 |
// Add adds all key/value pairs in m2 to m1. Keys in m2 that are already |
| 69 |
// present in m1 will be overwritten with the value in m2. |
| 70 |
func Add[K comparable, V any](m1, m2 map[K]V) { |
| 71 |
for k, v := range m2 { |
| 72 |
m1[k] = v |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
// Sub removes all keys in m2 from m1. Keys in m2 that are not present |
| 77 |
// in m1 are ignored. The values in m2 are ignored. |
| 78 |
func Sub[K comparable, V any](m1, m2 map[K]V) { |
| 79 |
for k := range m2 { |
| 80 |
delete(m1, k) |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
// Intersect removes all keys from m1 that are not present in m2. |
| 85 |
// Keys in m2 that are not in m1 are ignored. The values in m2 are ignored. |
| 86 |
func Intersect[K comparable, V any](m1, m2 map[K]V) { |
| 87 |
for k := range m1 { |
| 88 |
if _, ok := m2[k]; !ok { |
| 89 |
delete(m1, k) |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
// Filter deletes any key/value pairs from m for which f returns false. |
| 95 |
func Filter[K comparable, V any](m map[K]V, f func(K, V) bool) { |
| 96 |
for k, v := range m { |
| 97 |
if !f(k, v) { |
| 98 |
delete(m, k) |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
// TransformValues applies f to each value in m. The keys remain unchanged. |
| 104 |
func TransformValues[K comparable, V any](m map[K]V, f func(V) V) { |
| 105 |
for k, v := range m { |
| 106 |
m[k] = f(v) |
| 107 |
} |
| 108 |
} |