summaryrefslogtreecommitdiff
path: root/lib/rbtree/rbtree.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2022-07-12 23:52:37 -0600
committerLuke Shumaker <lukeshu@lukeshu.com>2022-07-12 23:52:37 -0600
commitf1e8040bc33e9057bd7a756a09c431c3f0d86226 (patch)
treec58814ec322b8f2a6a891e721ad9cc662f8d2cef /lib/rbtree/rbtree.go
parentfb589cdfc5acd6bd629c3fcfbe42f94700e78899 (diff)
Add an `Ordered` interface for the Cmp method, have rbtree use it
Diffstat (limited to 'lib/rbtree/rbtree.go')
-rw-r--r--lib/rbtree/rbtree.go15
1 files changed, 4 insertions, 11 deletions
diff --git a/lib/rbtree/rbtree.go b/lib/rbtree/rbtree.go
index 0353e75..575a9ca 100644
--- a/lib/rbtree/rbtree.go
+++ b/lib/rbtree/rbtree.go
@@ -7,7 +7,7 @@ package rbtree
import (
"fmt"
- "golang.org/x/exp/constraints"
+ "git.lukeshu.com/btrfs-progs-ng/lib/util"
)
type Color bool
@@ -32,7 +32,7 @@ func (node *Node[V]) getColor() Color {
return node.Color
}
-type Tree[K constraints.Ordered, V any] struct {
+type Tree[K util.Ordered[K], V any] struct {
KeyFn func(V) K
root *Node[V]
}
@@ -104,14 +104,7 @@ func (node *Node[V]) search(fn func(V) int) (exact, nearest *Node[V]) {
func (t *Tree[K, V]) exactKey(key K) func(V) int {
return func(val V) int {
valKey := t.KeyFn(val)
- switch {
- case key < valKey:
- return -1
- case key > valKey:
- return 1
- default: // key == valKey:
- return 0
- }
+ return key.Cmp(valKey)
}
}
@@ -282,7 +275,7 @@ func (t *Tree[K, V]) Insert(val V) {
}
if parent == nil {
t.root = node
- } else if key < t.KeyFn(parent.Value) {
+ } else if key.Cmp(t.KeyFn(parent.Value)) < 0 {
parent.Left = node
} else {
parent.Right = node