diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2022-06-30 02:46:33 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2022-06-30 02:46:33 -0600 |
commit | 74f15aa6a5c04bab0615262d005eace4fc2baedb (patch) | |
tree | 600c5c5278e7c44c19396c65f79611e3a6d9f464 /pkg/rbtree/range.go | |
parent | 2f3acf6ffb4e9f2eef496e1c67d156f4f8e049db (diff) |
add rbtree.SearchRange
Diffstat (limited to 'pkg/rbtree/range.go')
-rw-r--r-- | pkg/rbtree/range.go | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/pkg/rbtree/range.go b/pkg/rbtree/range.go new file mode 100644 index 0000000..d7325ee --- /dev/null +++ b/pkg/rbtree/range.go @@ -0,0 +1,23 @@ +package rbtree + +import ( + "lukeshu.com/btrfs-tools/pkg/util" +) + +// SearchRange is like Search, but returns all nodes that match the +// function; assuming that they are contiguous. +func (t *Tree[K, V]) SearchRange(fn func(V) int) []*Node[V] { + middle := t.Search(fn) + if middle == nil { + return nil + } + ret := []*Node[V]{middle} + for node := t.Prev(middle); node != nil && fn(node.Value) == 0; node = t.Prev(node) { + ret = append(ret, node) + } + util.ReverseSlice(ret) + for node := t.Next(middle); node != nil && fn(node.Value) == 0; node = t.Next(node) { + ret = append(ret, node) + } + return ret +} |