-
Notifications
You must be signed in to change notification settings - Fork 6
/
comparison.go
97 lines (79 loc) · 3.1 KB
/
comparison.go
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package tuple
import (
"golang.org/x/exp/constraints"
)
// OrderedComparisonResult represents the result of a tuple ordered comparison.
// OrderedComparisonResult == 0 represents that the tuples are equal.
// OrderedComparisonResult < 0 represent that the host tuple is less than the guest tuple.
// OrderedComparisonResult > 0 represent that the host tuple is greater than the guest tuple.
type OrderedComparisonResult int
// Comparable is a constraint interface for complex tuple elements that can be compared to other instances.
// In order to compare tuples, either all of their elements must be Ordered, or Comparable.
type Comparable[T any] interface {
CompareTo(guest T) OrderedComparisonResult
}
// Equalable is a constraint interface for complex tuple elements whose equality to other instances can be tested.
type Equalable[T any] interface {
Equal(guest T) bool
}
// Equal returns whether the compared values are equal.
func (result OrderedComparisonResult) Equal() bool {
return result == 0
}
// LessThan returns whether the host is less than the guest.
func (result OrderedComparisonResult) LessThan() bool {
return result < 0
}
// LessOrEqual returns whether the host is less than or equal to the guest.
func (result OrderedComparisonResult) LessOrEqual() bool {
return result <= 0
}
// GreaterThan returns whether the host is greater than the guest.
func (result OrderedComparisonResult) GreaterThan() bool {
return result > 0
}
// GreaterOrEqual returns whether the host is greater than or equal to the guest.
func (result OrderedComparisonResult) GreaterOrEqual() bool {
return result >= 0
}
// EQ is short for Equal and returns whether the compared values are equal.
func (result OrderedComparisonResult) EQ() bool {
return result.Equal()
}
// LT is short for LessThan and returns whether the host is less than the guest.
func (result OrderedComparisonResult) LT() bool {
return result.LessThan()
}
// LE is short for LessOrEqual and returns whether the host is less than or equal to the guest.
func (result OrderedComparisonResult) LE() bool {
return result.LessOrEqual()
}
// GT is short for GreaterThan and returns whether the host is greater than the guest.
func (result OrderedComparisonResult) GT() bool {
return result.GreaterThan()
}
// GE is short for GreaterOrEqual and returns whether the host is greater than or equal to the guest.
func (result OrderedComparisonResult) GE() bool {
return result.GreaterOrEqual()
}
// multiCompare calls and compares the predicates by order.
// multiCompare will short-circuit once one of the predicates returns a non-equal result, and the rest
// of the predicates will not be called.
func multiCompare(predicates ...func() OrderedComparisonResult) OrderedComparisonResult {
for _, pred := range predicates {
if result := pred(); !result.Equal() {
return result
}
}
return 0
}
// compareOrdered returns the comparison result between the host and guest values provided they match the Ordered constraint.
func compareOrdered[T constraints.Ordered](host, guest T) OrderedComparisonResult {
if host < guest {
return -1
}
if host > guest {
return 1
}
return 0
}