diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2022-07-12 23:52:37 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2022-07-12 23:52:37 -0600 |
commit | 449a2aaadb0764dc542cdb2f7f029c70e60aee8f (patch) | |
tree | 3175d4a95a25d9f819eacecb910a0b30a177c810 | |
parent | 8d0bbfc6e4d29dc6a3c5969b0f58c42a7347e09d (diff) |
Add an `Ordered` interface for the Cmp method, have rbtree use it
-rw-r--r-- | lib/util/generic.go | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/lib/util/generic.go b/lib/util/generic.go index 61e045b..a63a814 100644 --- a/lib/util/generic.go +++ b/lib/util/generic.go @@ -124,3 +124,24 @@ func (m *SyncMap[K, V]) Range(f func(key K, value V) bool) { }) } func (m *SyncMap[K, V]) Store(key K, value V) { m.inner.Store(key, value) } + +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]{} |