Skip to content

Commit a964876

Browse files
ianlancetaylorgopherbot
authored andcommitted
maps: new package
This copies x/exp/maps into the standard library (except for the Clear function which is now available as the clear builtin.) Fixes #57436 Change-Id: I30dd470c2f7ae34c7c82b4c1025a7582d61fabaa Reviewed-on: https://go-review.googlesource.com/c/go/+/464343 TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> Reviewed-by: Eli Bendersky <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 10643b7 commit a964876

File tree

5 files changed

+277
-1
lines changed

5 files changed

+277
-1
lines changed

api/next/57436.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
pkg maps, func Clone[$0 interface{ ~map[$1]$2 }, $1 comparable, $2 interface{}]($0) $0 #57436
2+
pkg maps, func Copy[$0 interface{ ~map[$2]$3 }, $1 interface{ ~map[$2]$3 }, $2 comparable, $3 interface{}]($0, $1) #57436
3+
pkg maps, func DeleteFunc[$0 interface{ ~map[$1]$2 }, $1 comparable, $2 interface{}]($0, func($1, $2) bool) #57436
4+
pkg maps, func EqualFunc[$0 interface{ ~map[$2]$3 }, $1 interface{ ~map[$2]$4 }, $2 comparable, $3 interface{}, $4 interface{}]($0, $1, func($3, $4) bool) bool #57436
5+
pkg maps, func Equal[$0 interface{ ~map[$2]$3 }, $1 interface{ ~map[$2]$3 }, $2 comparable, $3 comparable]($0, $1) bool #57436
6+
pkg maps, func Keys[$0 interface{ ~map[$1]$2 }, $1 comparable, $2 interface{}]($0) []$1 #57436
7+
pkg maps, func Values[$0 interface{ ~map[$1]$2 }, $1 comparable, $2 interface{}]($0) []$2 #57436

src/go/build/deps_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ var depsRules = `
4545
internal/cpu, internal/goarch,
4646
internal/goexperiment, internal/goos,
4747
internal/goversion, internal/nettrace, internal/platform,
48-
unicode/utf8, unicode/utf16, unicode,
48+
maps, unicode/utf8, unicode/utf16, unicode,
4949
unsafe;
5050
5151
# These packages depend only on internal/goarch and unsafe.

src/go/doc/comment/std.go

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/maps/maps.go

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
// Keys returns the keys of the map m.
9+
// The keys will be in an indeterminate order.
10+
func Keys[M ~map[K]V, K comparable, V any](m M) []K {
11+
r := make([]K, 0, len(m))
12+
for k := range m {
13+
r = append(r, k)
14+
}
15+
return r
16+
}
17+
18+
// Values returns the values of the map m.
19+
// The values will be in an indeterminate order.
20+
func Values[M ~map[K]V, K comparable, V any](m M) []V {
21+
r := make([]V, 0, len(m))
22+
for _, v := range m {
23+
r = append(r, v)
24+
}
25+
return r
26+
}
27+
28+
// Equal reports whether two maps contain the same key/value pairs.
29+
// Values are compared using ==.
30+
func Equal[M1, M2 ~map[K]V, K, V comparable](m1 M1, m2 M2) bool {
31+
if len(m1) != len(m2) {
32+
return false
33+
}
34+
for k, v1 := range m1 {
35+
if v2, ok := m2[k]; !ok || v1 != v2 {
36+
return false
37+
}
38+
}
39+
return true
40+
}
41+
42+
// EqualFunc is like Equal, but compares values using eq.
43+
// Keys are still compared with ==.
44+
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 {
45+
if len(m1) != len(m2) {
46+
return false
47+
}
48+
for k, v1 := range m1 {
49+
if v2, ok := m2[k]; !ok || !eq(v1, v2) {
50+
return false
51+
}
52+
}
53+
return true
54+
}
55+
56+
// Clone returns a copy of m. This is a shallow clone:
57+
// the new keys and values are set using ordinary assignment.
58+
func Clone[M ~map[K]V, K comparable, V any](m M) M {
59+
// Preserve nil in case it matters.
60+
if m == nil {
61+
return nil
62+
}
63+
r := make(M, len(m))
64+
for k, v := range m {
65+
r[k] = v
66+
}
67+
return r
68+
}
69+
70+
// Copy copies all key/value pairs in src adding them to dst.
71+
// When a key in src is already present in dst,
72+
// the value in dst will be overwritten by the value associated
73+
// with the key in src.
74+
func Copy[M1 ~map[K]V, M2 ~map[K]V, K comparable, V any](dst M1, src M2) {
75+
for k, v := range src {
76+
dst[k] = v
77+
}
78+
}
79+
80+
// DeleteFunc deletes any key/value pairs from m for which del returns true.
81+
func DeleteFunc[M ~map[K]V, K comparable, V any](m M, del func(K, V) bool) {
82+
for k, v := range m {
83+
if del(k, v) {
84+
delete(m, k)
85+
}
86+
}
87+
}

src/maps/maps_test.go

+181
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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
6+
7+
import (
8+
"math"
9+
"sort"
10+
"strconv"
11+
"testing"
12+
)
13+
14+
// TODO: replace with slices.Equal when slices is in GOROOT.
15+
func slicesEqual[E comparable](s1, s2 []E) bool {
16+
if len(s1) != len(s2) {
17+
return false
18+
}
19+
for i := range s1 {
20+
if s1[i] != s2[i] {
21+
return false
22+
}
23+
}
24+
return true
25+
}
26+
27+
var m1 = map[int]int{1: 2, 2: 4, 4: 8, 8: 16}
28+
var m2 = map[int]string{1: "2", 2: "4", 4: "8", 8: "16"}
29+
30+
func TestKeys(t *testing.T) {
31+
want := []int{1, 2, 4, 8}
32+
33+
got1 := Keys(m1)
34+
sort.Ints(got1)
35+
if !slicesEqual(got1, want) {
36+
t.Errorf("Keys(%v) = %v, want %v", m1, got1, want)
37+
}
38+
39+
got2 := Keys(m2)
40+
sort.Ints(got2)
41+
if !slicesEqual(got2, want) {
42+
t.Errorf("Keys(%v) = %v, want %v", m2, got2, want)
43+
}
44+
}
45+
46+
func TestValues(t *testing.T) {
47+
got1 := Values(m1)
48+
want1 := []int{2, 4, 8, 16}
49+
sort.Ints(got1)
50+
if !slicesEqual(got1, want1) {
51+
t.Errorf("Values(%v) = %v, want %v", m1, got1, want1)
52+
}
53+
54+
got2 := Values(m2)
55+
want2 := []string{"16", "2", "4", "8"}
56+
sort.Strings(got2)
57+
if !slicesEqual(got2, want2) {
58+
t.Errorf("Values(%v) = %v, want %v", m2, got2, want2)
59+
}
60+
}
61+
62+
func TestEqual(t *testing.T) {
63+
if !Equal(m1, m1) {
64+
t.Errorf("Equal(%v, %v) = false, want true", m1, m1)
65+
}
66+
if Equal(m1, (map[int]int)(nil)) {
67+
t.Errorf("Equal(%v, nil) = true, want false", m1)
68+
}
69+
if Equal((map[int]int)(nil), m1) {
70+
t.Errorf("Equal(nil, %v) = true, want false", m1)
71+
}
72+
if !Equal[map[int]int, map[int]int](nil, nil) {
73+
t.Error("Equal(nil, nil) = false, want true")
74+
}
75+
if ms := map[int]int{1: 2}; Equal(m1, ms) {
76+
t.Errorf("Equal(%v, %v) = true, want false", m1, ms)
77+
}
78+
79+
// Comparing NaN for equality is expected to fail.
80+
mf := map[int]float64{1: 0, 2: math.NaN()}
81+
if Equal(mf, mf) {
82+
t.Errorf("Equal(%v, %v) = true, want false", mf, mf)
83+
}
84+
}
85+
86+
// equal is simply ==.
87+
func equal[T comparable](v1, v2 T) bool {
88+
return v1 == v2
89+
}
90+
91+
// equalNaN is like == except that all NaNs are equal.
92+
func equalNaN[T comparable](v1, v2 T) bool {
93+
isNaN := func(f T) bool { return f != f }
94+
return v1 == v2 || (isNaN(v1) && isNaN(v2))
95+
}
96+
97+
// equalStr compares ints and strings.
98+
func equalIntStr(v1 int, v2 string) bool {
99+
return strconv.Itoa(v1) == v2
100+
}
101+
102+
func TestEqualFunc(t *testing.T) {
103+
if !EqualFunc(m1, m1, equal[int]) {
104+
t.Errorf("EqualFunc(%v, %v, equal) = false, want true", m1, m1)
105+
}
106+
if EqualFunc(m1, (map[int]int)(nil), equal[int]) {
107+
t.Errorf("EqualFunc(%v, nil, equal) = true, want false", m1)
108+
}
109+
if EqualFunc((map[int]int)(nil), m1, equal[int]) {
110+
t.Errorf("EqualFunc(nil, %v, equal) = true, want false", m1)
111+
}
112+
if !EqualFunc[map[int]int, map[int]int](nil, nil, equal[int]) {
113+
t.Error("EqualFunc(nil, nil, equal) = false, want true")
114+
}
115+
if ms := map[int]int{1: 2}; EqualFunc(m1, ms, equal[int]) {
116+
t.Errorf("EqualFunc(%v, %v, equal) = true, want false", m1, ms)
117+
}
118+
119+
// Comparing NaN for equality is expected to fail.
120+
mf := map[int]float64{1: 0, 2: math.NaN()}
121+
if EqualFunc(mf, mf, equal[float64]) {
122+
t.Errorf("EqualFunc(%v, %v, equal) = true, want false", mf, mf)
123+
}
124+
// But it should succeed using equalNaN.
125+
if !EqualFunc(mf, mf, equalNaN[float64]) {
126+
t.Errorf("EqualFunc(%v, %v, equalNaN) = false, want true", mf, mf)
127+
}
128+
129+
if !EqualFunc(m1, m2, equalIntStr) {
130+
t.Errorf("EqualFunc(%v, %v, equalIntStr) = false, want true", m1, m2)
131+
}
132+
}
133+
134+
func TestClone(t *testing.T) {
135+
mc := Clone(m1)
136+
if !Equal(mc, m1) {
137+
t.Errorf("Clone(%v) = %v, want %v", m1, mc, m1)
138+
}
139+
mc[16] = 32
140+
if Equal(mc, m1) {
141+
t.Errorf("Equal(%v, %v) = true, want false", mc, m1)
142+
}
143+
}
144+
145+
func TestCloneNil(t *testing.T) {
146+
var m1 map[string]int
147+
mc := Clone(m1)
148+
if mc != nil {
149+
t.Errorf("Clone(%v) = %v, want %v", m1, mc, m1)
150+
}
151+
}
152+
153+
func TestCopy(t *testing.T) {
154+
mc := Clone(m1)
155+
Copy(mc, mc)
156+
if !Equal(mc, m1) {
157+
t.Errorf("Copy(%v, %v) = %v, want %v", m1, m1, mc, m1)
158+
}
159+
Copy(mc, map[int]int{16: 32})
160+
want := map[int]int{1: 2, 2: 4, 4: 8, 8: 16, 16: 32}
161+
if !Equal(mc, want) {
162+
t.Errorf("Copy result = %v, want %v", mc, want)
163+
}
164+
165+
type M1 map[int]bool
166+
type M2 map[int]bool
167+
Copy(make(M1), make(M2))
168+
}
169+
170+
func TestDeleteFunc(t *testing.T) {
171+
mc := Clone(m1)
172+
DeleteFunc(mc, func(int, int) bool { return false })
173+
if !Equal(mc, m1) {
174+
t.Errorf("DeleteFunc(%v, true) = %v, want %v", m1, mc, m1)
175+
}
176+
DeleteFunc(mc, func(k, v int) bool { return k > 3 })
177+
want := map[int]int{1: 2, 2: 4}
178+
if !Equal(mc, want) {
179+
t.Errorf("DeleteFunc result = %v, want %v", mc, want)
180+
}
181+
}

0 commit comments

Comments
 (0)