summaryrefslogtreecommitdiff
path: root/lib/containers
diff options
context:
space:
mode:
Diffstat (limited to 'lib/containers')
-rw-r--r--lib/containers/intervaltree.go16
-rw-r--r--lib/containers/ordered.go10
-rw-r--r--lib/containers/ordered_test.go13
-rw-r--r--lib/containers/rbtree.go4
-rw-r--r--lib/containers/set.go2
5 files changed, 29 insertions, 16 deletions
diff --git a/lib/containers/intervaltree.go b/lib/containers/intervaltree.go
index 16bc916..b7ff866 100644
--- a/lib/containers/intervaltree.go
+++ b/lib/containers/intervaltree.go
@@ -12,11 +12,11 @@ func (ival intervalKey[K]) ContainsFn(fn func(K) int) bool {
return fn(ival.Min) >= 0 && fn(ival.Max) <= 0
}
-func (a intervalKey[K]) Cmp(b intervalKey[K]) int {
- if d := a.Min.Cmp(b.Min); d != 0 {
+func (a intervalKey[K]) Compare(b intervalKey[K]) int {
+ if d := a.Min.Compare(b.Min); d != 0 {
return d
}
- return a.Max.Cmp(b.Max)
+ return a.Max.Compare(b.Max)
}
type intervalValue[K Ordered[K], V any] struct {
@@ -39,19 +39,19 @@ func (t *IntervalTree[K, V]) keyFn(v intervalValue[K, V]) intervalKey[K] {
func (t *IntervalTree[K, V]) attrFn(node *RBNode[intervalValue[K, V]]) {
max := t.MaxFn(node.Value.Val)
- if node.Left != nil && node.Left.Value.SpanOfChildren.Max.Cmp(max) > 0 {
+ if node.Left != nil && node.Left.Value.SpanOfChildren.Max.Compare(max) > 0 {
max = node.Left.Value.SpanOfChildren.Max
}
- if node.Right != nil && node.Right.Value.SpanOfChildren.Max.Cmp(max) > 0 {
+ if node.Right != nil && node.Right.Value.SpanOfChildren.Max.Compare(max) > 0 {
max = node.Right.Value.SpanOfChildren.Max
}
node.Value.SpanOfChildren.Max = max
min := t.MinFn(node.Value.Val)
- if node.Left != nil && node.Left.Value.SpanOfChildren.Min.Cmp(min) < 0 {
+ if node.Left != nil && node.Left.Value.SpanOfChildren.Min.Compare(min) < 0 {
min = node.Left.Value.SpanOfChildren.Min
}
- if node.Right != nil && node.Right.Value.SpanOfChildren.Min.Cmp(min) < 0 {
+ if node.Right != nil && node.Right.Value.SpanOfChildren.Min.Compare(min) < 0 {
min = node.Right.Value.SpanOfChildren.Min
}
node.Value.SpanOfChildren.Min = min
@@ -98,7 +98,7 @@ func (t *IntervalTree[K, V]) Max() (K, bool) {
}
func (t *IntervalTree[K, V]) Lookup(k K) (V, bool) {
- return t.Search(k.Cmp)
+ return t.Search(k.Compare)
}
func (t *IntervalTree[K, V]) Search(fn func(K) int) (V, bool) {
diff --git a/lib/containers/ordered.go b/lib/containers/ordered.go
index d918149..aa2c94d 100644
--- a/lib/containers/ordered.go
+++ b/lib/containers/ordered.go
@@ -1,4 +1,4 @@
-// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
@@ -9,7 +9,7 @@ import (
)
type _Ordered[T any] interface {
- Cmp(T) int
+ Compare(T) int
}
type Ordered[T _Ordered[T]] _Ordered[T]
@@ -18,7 +18,7 @@ type NativeOrdered[T constraints.Ordered] struct {
Val T
}
-func NativeCmp[T constraints.Ordered](a, b T) int {
+func NativeCompare[T constraints.Ordered](a, b T) int {
switch {
case a < b:
return -1
@@ -29,8 +29,8 @@ func NativeCmp[T constraints.Ordered](a, b T) int {
}
}
-func (a NativeOrdered[T]) Cmp(b NativeOrdered[T]) int {
- return NativeCmp(a.Val, b.Val)
+func (a NativeOrdered[T]) Compare(b NativeOrdered[T]) int {
+ return NativeCompare(a.Val, b.Val)
}
var _ Ordered[NativeOrdered[int]] = NativeOrdered[int]{}
diff --git a/lib/containers/ordered_test.go b/lib/containers/ordered_test.go
new file mode 100644
index 0000000..4f0b2e6
--- /dev/null
+++ b/lib/containers/ordered_test.go
@@ -0,0 +1,13 @@
+// Copyright (C) 2023 Luke Shumaker <lukeshu@lukeshu.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+package containers_test
+
+import (
+ "net/netip"
+
+ "git.lukeshu.com/btrfs-progs-ng/lib/containers"
+)
+
+var _ containers.Ordered[netip.Addr] = netip.Addr{}
diff --git a/lib/containers/rbtree.go b/lib/containers/rbtree.go
index 0430123..1fdb799 100644
--- a/lib/containers/rbtree.go
+++ b/lib/containers/rbtree.go
@@ -111,7 +111,7 @@ func (node *RBNode[V]) search(fn func(V) int) (exact, nearest *RBNode[V]) {
func (t *RBTree[K, V]) exactKey(key K) func(V) int {
return func(val V) int {
valKey := t.KeyFn(val)
- return key.Cmp(valKey)
+ return key.Compare(valKey)
}
}
@@ -341,7 +341,7 @@ func (t *RBTree[K, V]) Insert(val V) {
switch {
case parent == nil:
t.root = node
- case key.Cmp(t.KeyFn(parent.Value)) < 0:
+ case key.Compare(t.KeyFn(parent.Value)) < 0:
parent.Left = node
default:
parent.Right = node
diff --git a/lib/containers/set.go b/lib/containers/set.go
index b2af494..0d9202c 100644
--- a/lib/containers/set.go
+++ b/lib/containers/set.go
@@ -32,7 +32,7 @@ func (o Set[T]) EncodeJSON(w io.Writer) error {
var zero T
switch (any(zero)).(type) {
case _Ordered[T]:
- less = func(a, b T) bool { return cast[_Ordered[T]](a).Cmp(b) < 0 }
+ less = func(a, b T) bool { return cast[_Ordered[T]](a).Compare(b) < 0 }
// This is the constraints.Ordered list
case string:
less = func(a, b T) bool { return cast[string](a) < cast[string](b) }