-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.go
49 lines (41 loc) · 928 Bytes
/
queue.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
package queue
import "time"
type Iterator[T any] interface {
Begin()
End()
Next() bool
Prev() bool
Index() int
Value() T
}
// LessFn is a function that returns whether 'a' is less than 'b'.
//
// Returns true if 'a' is less than 'b'.
type LessFn[T any] func(a, b T) bool
// Comparator is a function that compares two elements.
//
// Returns -1 if x < y, 0 if x == y, 1 if x > y.
type Comparator[T any] func(x, y T) int
// Less returns true if a < b.
func (c Comparator[T]) Less(a, b T) bool {
return c(a, b) < 0
}
// Greater returns true if a > b.
func (c Comparator[T]) Greater(a, b T) bool {
return c(a, b) > 0
}
// Equal returns true if a == b.
func (c Comparator[T]) Equal(a, b T) bool {
return c(a, b) == 0
}
// TimeComparator provides a basic comparison on time.Time
func TimeComparator(a, b time.Time) int {
switch {
case a.After(b):
return 1
case a.Before(b):
return -1
default:
return 0
}
}