summaryrefslogtreecommitdiff
path: root/lib/containers/ordered.go
blob: d9181494974669c0de2674d168a510c0053900d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Copyright (C) 2022  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package containers

import (
	"golang.org/x/exp/constraints"
)

type _Ordered[T any] interface {
	Cmp(T) int
}

type Ordered[T _Ordered[T]] _Ordered[T]

type NativeOrdered[T constraints.Ordered] struct {
	Val T
}

func NativeCmp[T constraints.Ordered](a, b T) int {
	switch {
	case a < b:
		return -1
	case a > b:
		return 1
	default:
		return 0
	}
}

func (a NativeOrdered[T]) Cmp(b NativeOrdered[T]) int {
	return NativeCmp(a.Val, b.Val)
}

var _ Ordered[NativeOrdered[int]] = NativeOrdered[int]{}