mdb/btreeiterator.go

52 lines
892 B
Go

package mdb
/*Copyright (c) 2022, John David Lee
All rights reserved.
This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree.
*/
type BTreeIterator[T any] struct {
out chan *T
close chan struct{}
current *T
}
func newBTreeIterator[T any]() *BTreeIterator[T] {
return &BTreeIterator[T]{
out: make(chan *T),
close: make(chan struct{}, 1),
}
}
func (iter *BTreeIterator[T]) each(i *T) bool {
select {
case iter.out <- i:
return true
case <-iter.close:
return false
}
}
func (iter *BTreeIterator[T]) done() {
close(iter.out)
}
func (iter *BTreeIterator[T]) Next() bool {
val, ok := <-iter.out
if ok {
iter.current = val
return true
}
return false
}
func (iter *BTreeIterator[T]) Close() {
iter.close <- struct{}{}
}
func (iter *BTreeIterator[T]) Value() T {
return *iter.current
}