Skip to content

Commit 8b0f555

Browse files
ianlancetayloreric
authored andcommitted
maps: remove Keys and Values
Preserve the names in case we want them to return an iterator. Keep the efficient runtime implementations for now, as we will probably want them under some name, perhaps KeysSlice and ValuesSlice. Fixes golang#61538 Change-Id: I6b03010bf071fb4531cb2f967dad46425962fcb8 Reviewed-on: https://go-review.googlesource.com/c/go/+/513476 Run-TryBot: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Russ Cox <[email protected]>
1 parent 69cce21 commit 8b0f555

File tree

2 files changed

+0
-128
lines changed

2 files changed

+0
-128
lines changed

src/maps/maps.go

-28
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,6 @@
55
// Package maps defines various functions useful with maps of any type.
66
package maps
77

8-
import "unsafe"
9-
10-
// keys is implemented in the runtime package.
11-
//
12-
//go:noescape
13-
func keys(m any, slice unsafe.Pointer)
14-
15-
// Keys returns the keys of the map m.
16-
// The keys will be in an indeterminate order.
17-
func Keys[M ~map[K]V, K comparable, V any](m M) []K {
18-
r := make([]K, 0, len(m))
19-
keys(m, unsafe.Pointer(&r))
20-
return r
21-
}
22-
23-
// values is implemented in the runtime package.
24-
//
25-
//go:noescape
26-
func values(m any, slice unsafe.Pointer)
27-
28-
// Values returns the values of the map m.
29-
// The values will be in an indeterminate order.
30-
func Values[M ~map[K]V, K comparable, V any](m M) []V {
31-
r := make([]V, 0, len(m))
32-
values(m, unsafe.Pointer(&r))
33-
return r
34-
}
35-
368
// Equal reports whether two maps contain the same key/value pairs.
379
// Values are compared using ==.
3810
func Equal[M1, M2 ~map[K]V, K, V comparable](m1 M1, m2 M2) bool {

src/maps/maps_test.go

-100
Original file line numberDiff line numberDiff line change
@@ -6,87 +6,13 @@ package maps
66

77
import (
88
"math"
9-
"slices"
10-
"sort"
119
"strconv"
1210
"testing"
13-
"unsafe"
1411
)
1512

1613
var m1 = map[int]int{1: 2, 2: 4, 4: 8, 8: 16}
1714
var m2 = map[int]string{1: "2", 2: "4", 4: "8", 8: "16"}
1815

19-
func keysForBenchmarking[M ~map[K]V, K comparable, V any](m M, s []K) {
20-
keys(m, unsafe.Pointer(&s))
21-
}
22-
23-
func TestKeys(t *testing.T) {
24-
want := []int{1, 2, 4, 8}
25-
26-
got1 := Keys(m1)
27-
sort.Ints(got1)
28-
if !slices.Equal(got1, want) {
29-
t.Errorf("Keys(%v) = %v, want %v", m1, got1, want)
30-
}
31-
32-
got2 := Keys(m2)
33-
sort.Ints(got2)
34-
if !slices.Equal(got2, want) {
35-
t.Errorf("Keys(%v) = %v, want %v", m2, got2, want)
36-
}
37-
38-
// test for oldbucket code path
39-
// We grow from 128 to 256 buckets at size 832 (6.5 * 128).
40-
// Then we have to evacuate 128 buckets, which means we'll be done evacuation at 832+128=960 elements inserted.
41-
// so 840 is a good number to test for oldbucket code path.
42-
var want3 []int
43-
var m = make(map[int]int)
44-
for i := 0; i < 840; i++ {
45-
want3 = append(want3, i)
46-
m[i] = i * i
47-
}
48-
49-
got3 := Keys(m)
50-
sort.Ints(got3)
51-
if !slices.Equal(got3, want3) {
52-
t.Errorf("Keys(%v) = %v, want %v", m, got3, want3)
53-
}
54-
}
55-
56-
func valuesForBenchmarking[M ~map[K]V, K comparable, V any](m M, s []V) {
57-
values(m, unsafe.Pointer(&s))
58-
}
59-
60-
func TestValues(t *testing.T) {
61-
got1 := Values(m1)
62-
want1 := []int{2, 4, 8, 16}
63-
sort.Ints(got1)
64-
if !slices.Equal(got1, want1) {
65-
t.Errorf("Values(%v) = %v, want %v", m1, got1, want1)
66-
}
67-
68-
got2 := Values(m2)
69-
want2 := []string{"16", "2", "4", "8"}
70-
sort.Strings(got2)
71-
if !slices.Equal(got2, want2) {
72-
t.Errorf("Values(%v) = %v, want %v", m2, got2, want2)
73-
}
74-
75-
//test for oldbucket code path
76-
var want3 []int
77-
var m = make(map[int]int)
78-
for i := 0; i < 840; i++ {
79-
want3 = append(want3, i*i)
80-
m[i] = i * i
81-
}
82-
83-
got3 := Values(m)
84-
sort.Ints(got3)
85-
if !slices.Equal(got3, want3) {
86-
t.Errorf("Values(%v) = %v, want %v", m, got3, want3)
87-
}
88-
}
89-
9016
func TestEqual(t *testing.T) {
9117
if !Equal(m1, m1) {
9218
t.Errorf("Equal(%v, %v) = false, want true", m1, m1)
@@ -256,29 +182,3 @@ func TestCloneWithMapAssign(t *testing.T) {
256182
}
257183
}
258184
}
259-
260-
func BenchmarkKeys(b *testing.B) {
261-
m := make(map[int]int, 1000000)
262-
for i := 0; i < 1000000; i++ {
263-
m[i] = i
264-
}
265-
b.ResetTimer()
266-
267-
slice := make([]int, 0, len(m))
268-
for i := 0; i < b.N; i++ {
269-
keysForBenchmarking(m, slice)
270-
}
271-
}
272-
273-
func BenchmarkValues(b *testing.B) {
274-
m := make(map[int]int, 1000000)
275-
for i := 0; i < 1000000; i++ {
276-
m[i] = i
277-
}
278-
b.ResetTimer()
279-
280-
slice := make([]int, 0, len(m))
281-
for i := 0; i < b.N; i++ {
282-
valuesForBenchmarking(m, slice)
283-
}
284-
}

0 commit comments

Comments
 (0)