summaryrefslogtreecommitdiff
path: root/lib/containers/ordered.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2022-07-13 20:41:10 -0600
committerLuke Shumaker <lukeshu@lukeshu.com>2022-07-13 21:39:09 -0600
commit952b677bf7f10da93673e3671f764c54c454bbfe (patch)
tree57b31c8f72f76d60b89e0307069c9d15c6794326 /lib/containers/ordered.go
parent4e29bb393ec774f0a79c70d9d69c54fe4e8ecb72 (diff)
Move ordered.go to lib/containers
Something about this trips a stack overflow in golangci-lint, so upgrade golangci-lint to a commit that fixes this.
Diffstat (limited to 'lib/containers/ordered.go')
-rw-r--r--lib/containers/ordered.go41
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/containers/ordered.go b/lib/containers/ordered.go
new file mode 100644
index 0000000..c5a3ec5
--- /dev/null
+++ b/lib/containers/ordered.go
@@ -0,0 +1,41 @@
+// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+package containers
+
+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]{}