summaryrefslogtreecommitdiff
path: root/lib/util/ordered.go
diff options
context:
space:
mode:
Diffstat (limited to 'lib/util/ordered.go')
-rw-r--r--lib/util/ordered.go41
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/util/ordered.go b/lib/util/ordered.go
new file mode 100644
index 0000000..fc5b5ee
--- /dev/null
+++ b/lib/util/ordered.go
@@ -0,0 +1,41 @@
+// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+package util
+
+import (
+ "golang.org/x/exp/constraints"
+)
+
+func CmpUint[T constraints.Unsigned](a, b T) int {
+ switch {
+ case a < b:
+ return -1
+ case a == b:
+ return 0
+ default:
+ return 1
+ }
+}
+
+type Ordered[T interface{ Cmp(T) int }] interface {
+ Cmp(T) int
+}
+
+type NativeOrdered[T constraints.Ordered] struct {
+ Val T
+}
+
+func (a NativeOrdered[T]) Cmp(b NativeOrdered[T]) int {
+ switch {
+ case a.Val < b.Val:
+ return -1
+ case a.Val > b.Val:
+ return 1
+ default:
+ return 0
+ }
+}
+
+var _ Ordered[NativeOrdered[int]] = NativeOrdered[int]{}