diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2022-07-13 20:31:51 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2022-07-13 20:31:51 -0600 |
commit | 09cc146211148a3b2568261c41a804a802c31d4c (patch) | |
tree | e64dc42bb557664333e313e89ddaafa975126e9a /lib/util/ordered.go | |
parent | a0fc940be0ea888c7b380cd95723da65bbd32bcb (diff) |
re-organize some things between files
Diffstat (limited to 'lib/util/ordered.go')
-rw-r--r-- | lib/util/ordered.go | 41 |
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]{} |