Skip to content

Commit

Permalink
stree: a better definition of Find
Browse files Browse the repository at this point in the history
  • Loading branch information
creachadair committed Jan 5, 2025
1 parent 0f17035 commit 5a8aec3
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions stree/stree.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,16 @@ func (t *Tree[T]) Get(key T) (_ T, ok bool) {
// Find returns a cursor to the smallest key in the tree greater than or equal
// to key. If no such key exists, Find returns nil.
func (t *Tree[T]) Find(key T) *Cursor[T] {
path := t.root.pathTo(key, t.compare)
if len(path) == 0 || t.compare(path[len(path)-1].X, key) < 0 {
var next T
var found bool
t.root.inorderAfter(key, t.compare, func(k T) bool {
next, found = k, true
return false
})
if !found {
return nil
}
return &Cursor[T]{path: path}
return t.Cursor(next)
}

// Inorder is a range function that visits each key of t in order.
Expand Down

0 comments on commit 5a8aec3

Please sign in to comment.