summaryrefslogtreecommitdiff
path: root/lib/containers/ordered.go
blob: 038edf8d320e23abf5463360216557ebf1c51e75 (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
// 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 interface{ Cmp(T) int }] interface {
	Cmp(T) int
}

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]{}