mirror of
https://github.com/golang/go
synced 2024-11-14 20:10:30 -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:
parent
d0edd9acc8
commit
3a3837f422
@ -270,13 +270,15 @@ func (ht *HashTrieMap[K, V]) CompareAndDelete(key K, old V) (deleted bool) {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enumerate produces all key-value pairs in the map. The enumeration does
|
// All returns an iter.Seq2 that produces all key-value pairs in the map.
|
||||||
// not represent any consistent snapshot of the map, but is guaranteed
|
// The enumeration does not represent any consistent snapshot of the map,
|
||||||
// to visit each unique key-value pair only once. It is safe to operate
|
// but is guaranteed to visit each unique key-value pair only once. It is
|
||||||
// on the tree during iteration. No particular enumeration order is
|
// safe to operate on the tree during iteration. No particular enumeration
|
||||||
// guaranteed.
|
// order is guaranteed.
|
||||||
func (ht *HashTrieMap[K, V]) Enumerate(yield func(key K, value V) bool) {
|
func (ht *HashTrieMap[K, V]) All() func(yield func(K, V) bool) {
|
||||||
ht.iter(ht.root, yield)
|
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 {
|
func (ht *HashTrieMap[K, V]) iter(i *indirect[K, V], yield func(key K, value V) bool) bool {
|
||||||
|
@ -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()
|
m := newMap()
|
||||||
|
|
||||||
testEnumerate(t, m, testDataMap(testData[:]), func(_ string, _ int) bool {
|
testAll(t, m, testDataMap(testData[:]), func(_ string, _ int) bool {
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
t.Run("EnumerateDelete", func(t *testing.T) {
|
t.Run("AllDelete", func(t *testing.T) {
|
||||||
m := newMap()
|
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))
|
expectDeleted(t, s, i)(m.CompareAndDelete(s, i))
|
||||||
return true
|
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 {
|
for k, v := range testData {
|
||||||
expectStored(t, k, v)(m.LoadOrStore(k, v))
|
expectStored(t, k, v)(m.LoadOrStore(k, v))
|
||||||
}
|
}
|
||||||
visited := make(map[K]int)
|
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]
|
want, ok := testData[key]
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Errorf("unexpected key %v in map", key)
|
t.Errorf("unexpected key %v in map", key)
|
||||||
|
@ -126,7 +126,7 @@ func addUniqueMap[T comparable](typ *abi.Type) *uniqueMap[T] {
|
|||||||
cleanupFuncs = append(cleanupFuncs, func() {
|
cleanupFuncs = append(cleanupFuncs, func() {
|
||||||
// Delete all the entries whose weak references are nil and clean up
|
// Delete all the entries whose weak references are nil and clean up
|
||||||
// deleted entries.
|
// 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 {
|
if wp.Strong() == nil {
|
||||||
m.CompareAndDelete(key, wp)
|
m.CompareAndDelete(key, wp)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user