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