add iterators for tree

This commit is contained in:
2025-08-08 14:19:31 +08:00
parent 6787c1817f
commit c3b5b255ad

33
tree_iter.go Normal file
View File

@@ -0,0 +1,33 @@
package stl
import "iter"
func (t *Tree[K, V, C]) Keys() iter.Seq[K] {
return func(yield func(K) bool) {
for cur := t.FirstNode(); cur != nil; cur = cur.Next() {
if !yield(cur.key) {
return
}
}
}
}
func (t *Tree[K, V, C]) Values() iter.Seq[V] {
return func(yield func(V) bool) {
for cur := t.FirstNode(); cur != nil; cur = cur.Next() {
if !yield(cur.Value) {
return
}
}
}
}
func (t *Tree[K, V, C]) All() iter.Seq2[K, V] {
return func(yield func(K, V) bool) {
for cur := t.FirstNode(); cur != nil; cur = cur.Next() {
if !yield(cur.key, cur.Value) {
return
}
}
}
}