3 // Copyright 2021 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
15 // _Equal reports whether two slices are equal: the same length and all
16 // elements equal. All floating point NaNs are considered equal.
17 func _SliceEqual[Elem comparable](s1, s2 []Elem) bool {
18 if len(s1) != len(s2) {
21 for i, v1 := range s1 {
24 isNaN := func(f Elem) bool { return f != f }
25 if !isNaN(v1) || !isNaN(v2) {
33 // _Keys returns the keys of the map m.
34 // The keys will be an indeterminate order.
35 func _Keys[K comparable, V any](m map[K]V) []K {
36 r := make([]K, 0, len(m))
43 // _Values returns the values of the map m.
44 // The values will be in an indeterminate order.
45 func _Values[K comparable, V any](m map[K]V) []V {
46 r := make([]V, 0, len(m))
53 // _Equal reports whether two maps contain the same key/value pairs.
54 // _Values are compared using ==.
55 func _Equal[K, V comparable](m1, m2 map[K]V) bool {
56 if len(m1) != len(m2) {
59 for k, v1 := range m1 {
60 if v2, ok := m2[k]; !ok || v1 != v2 {
67 // _Copy returns a copy of m.
68 func _Copy[K comparable, V any](m map[K]V) map[K]V {
69 r := make(map[K]V, len(m))
76 // _Add adds all key/value pairs in m2 to m1. _Keys in m2 that are already
77 // present in m1 will be overwritten with the value in m2.
78 func _Add[K comparable, V any](m1, m2 map[K]V) {
79 for k, v := range m2 {
84 // _Sub removes all keys in m2 from m1. _Keys in m2 that are not present
85 // in m1 are ignored. The values in m2 are ignored.
86 func _Sub[K comparable, V any](m1, m2 map[K]V) {
92 // _Intersect removes all keys from m1 that are not present in m2.
93 // _Keys in m2 that are not in m1 are ignored. The values in m2 are ignored.
94 func _Intersect[K comparable, V any](m1, m2 map[K]V) {
96 if _, ok := m2[k]; !ok {
102 // _Filter deletes any key/value pairs from m for which f returns false.
103 func _Filter[K comparable, V any](m map[K]V, f func(K, V) bool) {
104 for k, v := range m {
111 // _TransformValues applies f to each value in m. The keys remain unchanged.
112 func _TransformValues[K comparable, V any](m map[K]V, f func(V) V) {
113 for k, v := range m {
118 var m1 = map[int]int{1: 2, 2: 4, 4: 8, 8: 16}
119 var m2 = map[int]string{1: "2", 2: "4", 4: "8", 8: "16"}
122 want := []int{1, 2, 4, 8}
126 if !_SliceEqual(got1, want) {
127 panic(fmt.Sprintf("_Keys(%v) = %v, want %v", m1, got1, want))
132 if !_SliceEqual(got2, want) {
133 panic(fmt.Sprintf("_Keys(%v) = %v, want %v", m2, got2, want))
139 want1 := []int{2, 4, 8, 16}
141 if !_SliceEqual(got1, want1) {
142 panic(fmt.Sprintf("_Values(%v) = %v, want %v", m1, got1, want1))
146 want2 := []string{"16", "2", "4", "8"}
148 if !_SliceEqual(got2, want2) {
149 panic(fmt.Sprintf("_Values(%v) = %v, want %v", m2, got2, want2))
155 panic(fmt.Sprintf("_Equal(%v, %v) = false, want true", m1, m1))
158 panic(fmt.Sprintf("_Equal(%v, nil) = true, want false", m1))
161 panic(fmt.Sprintf("_Equal(nil, %v) = true, want false", m1))
163 if !_Equal[int, int](nil, nil) {
164 panic("_Equal(nil, nil) = false, want true")
166 if ms := map[int]int{1: 2}; _Equal(m1, ms) {
167 panic(fmt.Sprintf("_Equal(%v, %v) = true, want false", m1, ms))
170 // Comparing NaN for equality is expected to fail.
171 mf := map[int]float64{1: 0, 2: math.NaN()}
173 panic(fmt.Sprintf("_Equal(%v, %v) = true, want false", mf, mf))
180 panic(fmt.Sprintf("_Copy(%v) = %v, want %v", m1, m2, m1))
184 panic(fmt.Sprintf("_Equal(%v, %v) = true, want false", m1, m2))
192 panic(fmt.Sprintf("_Add(%v, %v) = %v, want %v", m1, m1, mc, m1))
194 _Add(mc, map[int]int{16: 32})
195 want := map[int]int{1: 2, 2: 4, 4: 8, 8: 16, 16: 32}
196 if !_Equal(mc, want) {
197 panic(fmt.Sprintf("_Add result = %v, want %v", mc, want))
205 panic(fmt.Sprintf("_Sub(%v, %v) = %v, want empty map", m1, m1, mc))
208 _Sub(mc, map[int]int{1: 0})
209 want := map[int]int{2: 4, 4: 8, 8: 16}
210 if !_Equal(mc, want) {
211 panic(fmt.Sprintf("_Sub result = %v, want %v", mc, want))
215 func TestIntersect() {
219 panic(fmt.Sprintf("_Intersect(%v, %v) = %v, want %v", m1, m1, mc, m1))
221 _Intersect(mc, map[int]int{1: 0, 2: 0})
222 want := map[int]int{1: 2, 2: 4}
223 if !_Equal(mc, want) {
224 panic(fmt.Sprintf("_Intersect result = %v, want %v", mc, want))
230 _Filter(mc, func(int, int) bool { return true })
232 panic(fmt.Sprintf("_Filter(%v, true) = %v, want %v", m1, mc, m1))
234 _Filter(mc, func(k, v int) bool { return k < 3 })
235 want := map[int]int{1: 2, 2: 4}
236 if !_Equal(mc, want) {
237 panic(fmt.Sprintf("_Filter result = %v, want %v", mc, want))
241 func TestTransformValues() {
243 _TransformValues(mc, func(i int) int { return i / 2 })
244 want := map[int]int{1: 1, 2: 2, 4: 4, 8: 8}
245 if !_Equal(mc, want) {
246 panic(fmt.Sprintf("_TransformValues result = %v, want %v", mc, want))
259 TestTransformValues()