-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomparator.go
180 lines (158 loc) · 4.7 KB
/
comparator.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package streams
// Comparator is the interface used to compare elements of a Stream
//
// This interface is used when sorting, when finding min/max of a stream
// and is also used to determine equality during set operations
// (Stream.Difference, Stream.Intersection, Stream.SymmetricDifference and Stream.Union)
type Comparator[T any] interface {
// Compare compares the two values lexicographically, i.e.:
//
// * the result should be 0 if v1 == v2
//
// * the result should be -1 if v1 < v2
//
// * the result should be 1 if v1 > v2
Compare(v1, v2 T) int
// Less returns true if v1 < v2, otherwise false
Less(v1, v2 T) bool
// LessOrEqual returns true if v1 <= v2, otherwise false
LessOrEqual(v1, v2 T) bool
// Greater returns true if v1 > v2, otherwise false
Greater(v1, v2 T) bool
// GreaterOrEqual returns true if v1 >= v2, otherwise false
GreaterOrEqual(v1, v2 T) bool
// Equal returns true if v1 == v2, otherwise false
Equal(v1, v2 T) bool
// NotEqual returns true if v1 != v2, otherwise false
NotEqual(v1, v2 T) bool
// Reversed creates a new comparator that imposes the reverse ordering to this comparator
//
// the reversal is against less/greater as well as against equality/non-equality
Reversed() Comparator[T]
// Then creates a new comparator from this comparator, with a following then comparator
// that is used when the initial comparison yields equal
Then(other Comparator[T]) Comparator[T]
}
// NewComparator creates a new Comparator from the function provided
func NewComparator[T any](f ComparatorFunc[T]) Comparator[T] {
if f == nil {
return nil
}
return comparator[T]{
f: f,
}
}
type comparator[T any] struct {
f ComparatorFunc[T]
inner Comparator[T]
then Comparator[T]
reversed bool
}
// Compare compares the two values lexicographically, i.e.:
//
// * the result should be 0 if v1 == v2
//
// * the result should be -1 if v1 < v2
//
// * the result should be 1 if v1 > v2
func (c comparator[T]) Compare(v1, v2 T) int {
result := 0
if c.f != nil {
result = c.f(v1, v2)
} else {
result = c.inner.Compare(v1, v2)
}
if c.reversed {
result = 0 - result
}
if result == 0 && c.then != nil {
result = c.then.Compare(v1, v2)
}
return result
}
// Less returns true if v1 < v2, otherwise false
func (c comparator[T]) Less(v1, v2 T) bool {
return c.Compare(v1, v2) < 0
}
// LessOrEqual returns true if v1 <= v2, otherwise false
func (c comparator[T]) LessOrEqual(v1, v2 T) bool {
return c.Compare(v1, v2) <= 0
}
// Greater returns true if v1 > v2, otherwise false
func (c comparator[T]) Greater(v1, v2 T) bool {
return c.Compare(v1, v2) > 0
}
// GreaterOrEqual returns true if v1 >= v2, otherwise false
func (c comparator[T]) GreaterOrEqual(v1, v2 T) bool {
return c.Compare(v1, v2) >= 0
}
// Equal returns true if v1 == v2, otherwise false
func (c comparator[T]) Equal(v1, v2 T) bool {
if r := c.Compare(v1, v2); c.reversed {
return r != 0
} else {
return r == 0
}
}
// NotEqual returns true if v1 != v2, otherwise false
func (c comparator[T]) NotEqual(v1, v2 T) bool {
if r := c.Compare(v1, v2); c.reversed {
return r == 0
} else {
return r != 0
}
}
// Reversed creates a new comparator that imposes the reverse ordering to this comparator
//
// the reversal is against less/greater as well as against equality/non-equality
func (c comparator[T]) Reversed() Comparator[T] {
return comparator[T]{
inner: c,
reversed: !c.reversed,
}
}
// Then creates a new comparator from this comparator, with a following then comparator
// that is used when the initial comparison yields equal
func (c comparator[T]) Then(other Comparator[T]) Comparator[T] {
return comparator[T]{
inner: c,
then: other,
}
}
// ComparatorFunc is the function signature used to create a new Comparator
//
// the function should compare the two values provided lexicographically, i.e.:
//
// * the result should be 0 if v1 == v2
//
// * the result should be -1 if v1 < v2
//
// * the result should be 1 if v1 > v2
type ComparatorFunc[T any] func(v1, v2 T) int
func (f ComparatorFunc[T]) Compare(v1, v2 T) int {
return f(v1, v2)
}
func (f ComparatorFunc[T]) Less(v1, v2 T) bool {
return f(v1, v2) < 0
}
func (f ComparatorFunc[T]) LessOrEqual(v1, v2 T) bool {
return f(v1, v2) <= 0
}
func (f ComparatorFunc[T]) Greater(v1, v2 T) bool {
return f(v1, v2) > 0
}
func (f ComparatorFunc[T]) GreaterOrEqual(v1, v2 T) bool {
return f(v1, v2) >= 0
}
func (f ComparatorFunc[T]) Equal(v1, v2 T) bool {
return f(v1, v2) == 0
}
func (f ComparatorFunc[T]) NotEqual(v1, v2 T) bool {
return f(v1, v2) != 0
}
func (f ComparatorFunc[T]) Reversed() Comparator[T] {
return NewComparator[T](f).Reversed()
}
func (f ComparatorFunc[T]) Then(other Comparator[T]) Comparator[T] {
return NewComparator[T](f).Then(other)
}