blob: cee55087dfe1dfcd11a9a5b2b262313c441d07a8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
package rbtree
import (
"reflect"
"git.lukeshu.com/btrfs-progs-ng/lib/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)
}
|