]> Cypherpunks repositories - gostls13.git/commitdiff
internal/concurrent: make HashTrieMap iteration more idiomatic
authorMichael Anthony Knyszek <mknyszek@google.com>
Sat, 18 May 2024 17:02:54 +0000 (17:02 +0000)
committerMichael Knyszek <mknyszek@google.com>
Wed, 22 May 2024 16:01:55 +0000 (16:01 +0000)
Currently a HashTrieMap has a method called Enumerate whose method
closure is an iter.Seq2, but the current convention is to name the
method All and return an iter.Seq2. This is an easy transformation, so
do it now.

Change-Id: I323e505008b7df3a9e20fe8c223b281a8c290006
Reviewed-on: https://go-review.googlesource.com/c/go/+/586995
Reviewed-by: Michael Pratt <mpratt@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/internal/concurrent/hashtriemap.go
src/internal/concurrent/hashtriemap_test.go
src/unique/handle.go

index 5e3102349469d1cffafcbbefea98b14eb20ec137..4f7e730d4fca2cc590c28a819bf079d3b83d2ac3 100644 (file)
@@ -270,13 +270,15 @@ func (ht *HashTrieMap[K, V]) CompareAndDelete(key K, old V) (deleted bool) {
        return true
 }
 
-// Enumerate produces all key-value pairs in the map. The enumeration does
-// not represent any consistent snapshot of the map, but is guaranteed
-// to visit each unique key-value pair only once. It is safe to operate
-// on the tree during iteration. No particular enumeration order is
-// guaranteed.
-func (ht *HashTrieMap[K, V]) Enumerate(yield func(key K, value V) bool) {
-       ht.iter(ht.root, yield)
+// All returns an iter.Seq2 that produces all key-value pairs in the map.
+// The enumeration does not represent any consistent snapshot of the map,
+// but is guaranteed to visit each unique key-value pair only once. It is
+// safe to operate on the tree during iteration. No particular enumeration
+// order is guaranteed.
+func (ht *HashTrieMap[K, V]) All() func(yield func(K, V) bool) {
+       return func(yield func(key K, value V) bool) {
+               ht.iter(ht.root, yield)
+       }
 }
 
 func (ht *HashTrieMap[K, V]) iter(i *indirect[K, V], yield func(key K, value V) bool) bool {
index 96f134c2930fab577122dd1afc2f1e7695050ea2..e233824c0f08fe4a2c56c26456c7a74101f0224d 100644 (file)
@@ -119,17 +119,17 @@ func testHashTrieMap(t *testing.T, newMap func() *HashTrieMap[string, int]) {
                        }
                }
        })
-       t.Run("Enumerate", func(t *testing.T) {
+       t.Run("All", func(t *testing.T) {
                m := newMap()
 
-               testEnumerate(t, m, testDataMap(testData[:]), func(_ string, _ int) bool {
+               testAll(t, m, testDataMap(testData[:]), func(_ string, _ int) bool {
                        return true
                })
        })
-       t.Run("EnumerateDelete", func(t *testing.T) {
+       t.Run("AllDelete", func(t *testing.T) {
                m := newMap()
 
-               testEnumerate(t, m, testDataMap(testData[:]), func(s string, i int) bool {
+               testAll(t, m, testDataMap(testData[:]), func(s string, i int) bool {
                        expectDeleted(t, s, i)(m.CompareAndDelete(s, i))
                        return true
                })
@@ -200,12 +200,12 @@ func testHashTrieMap(t *testing.T, newMap func() *HashTrieMap[string, int]) {
        })
 }
 
-func testEnumerate[K, V comparable](t *testing.T, m *HashTrieMap[K, V], testData map[K]V, yield func(K, V) bool) {
+func testAll[K, V comparable](t *testing.T, m *HashTrieMap[K, V], testData map[K]V, yield func(K, V) bool) {
        for k, v := range testData {
                expectStored(t, k, v)(m.LoadOrStore(k, v))
        }
        visited := make(map[K]int)
-       m.Enumerate(func(key K, got V) bool {
+       m.All()(func(key K, got V) bool {
                want, ok := testData[key]
                if !ok {
                        t.Errorf("unexpected key %v in map", key)
index 4d9669162f8b761c45476f0c0bc07c2a4828c005..0842ae3185f2ccef5619eb79b6bae9af461f3d2f 100644 (file)
@@ -126,7 +126,7 @@ func addUniqueMap[T comparable](typ *abi.Type) *uniqueMap[T] {
                cleanupFuncs = append(cleanupFuncs, func() {
                        // Delete all the entries whose weak references are nil and clean up
                        // deleted entries.
-                       m.Enumerate(func(key T, wp weak.Pointer[T]) bool {
+                       m.All()(func(key T, wp weak.Pointer[T]) bool {
                                if wp.Strong() == nil {
                                        m.CompareAndDelete(key, wp)
                                }