diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2022-06-30 03:14:05 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2022-06-30 03:31:43 -0600 |
commit | 27d2f3a0efe6de94c7720907557e640e8a2f1428 (patch) | |
tree | 5cf5fd8f3ca247198fdb978b017c9c72ab581f27 /pkg/rbtree/rbtree_util.go | |
parent | 428d86a1816d13ea6969793a42893c739487cf3d (diff) |
btrfsvol: use rbtree
Diffstat (limited to 'pkg/rbtree/rbtree_util.go')
-rw-r--r-- | pkg/rbtree/rbtree_util.go | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/pkg/rbtree/rbtree_util.go b/pkg/rbtree/rbtree_util.go new file mode 100644 index 0000000..db7fd32 --- /dev/null +++ b/pkg/rbtree/rbtree_util.go @@ -0,0 +1,48 @@ +package rbtree + +import ( + "reflect" + + "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) []V { + middle := t.Search(fn) + if middle == nil { + return nil + } + ret := []V{middle.Value} + for node := t.Prev(middle); node != nil && fn(node.Value) == 0; node = t.Prev(node) { + ret = append(ret, node.Value) + } + util.ReverseSlice(ret) + for node := t.Next(middle); node != nil && fn(node.Value) == 0; node = t.Next(node) { + ret = append(ret, node.Value) + } + return ret +} + +func (t *Tree[K, V]) Equal(u *Tree[K, V]) bool { + if (t == nil) != (u == nil) { + return false + } + if t == nil { + return true + } + + var tSlice []V + t.Walk(func(node *Node[V]) error { + tSlice = append(tSlice, node.Value) + return nil + }) + + var uSlice []V + u.Walk(func(node *Node[V]) error { + uSlice = append(uSlice, node.Value) + return nil + }) + + return reflect.DeepEqual(tSlice, uSlice) +} |