1
0
mirror of https://github.com/golang/go synced 2024-11-14 19:50:21 -07:00

internal/concurrent: make HashTrieMap iteration more idiomatic

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>
This commit is contained in:
Michael Anthony Knyszek 2024-05-18 17:02:54 +00:00 committed by Michael Knyszek
parent d0edd9acc8
commit 3a3837f422
3 changed files with 16 additions and 14 deletions

View 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 {

View 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)

View 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)
}