-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathcomparable.go
62 lines (48 loc) · 1.61 KB
/
comparable.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
// Copyright 2011 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.
package skiplist
// Comparable defines a comparable func.
type Comparable interface {
Compare(lhs, rhs interface{}) int
CalcScore(key interface{}) float64
}
var (
_ Comparable = GreaterThanFunc(nil)
_ Comparable = LessThanFunc(nil)
)
// GreaterThanFunc returns true if lhs greater than rhs
type GreaterThanFunc func(lhs, rhs interface{}) int
// LessThanFunc returns true if lhs less than rhs
type LessThanFunc GreaterThanFunc
// Compare compares lhs and rhs by calling `f(lhs, rhs)`.
func (f GreaterThanFunc) Compare(lhs, rhs interface{}) int {
return f(lhs, rhs)
}
// CalcScore always returns 0 as there is no way to understand how f compares keys.
func (f GreaterThanFunc) CalcScore(key interface{}) float64 {
return 0
}
// Compare compares lhs and rhs by calling `-f(lhs, rhs)`.
func (f LessThanFunc) Compare(lhs, rhs interface{}) int {
return -f(lhs, rhs)
}
// CalcScore always returns 0 as there is no way to understand how f compares keys.
func (f LessThanFunc) CalcScore(key interface{}) float64 {
return 0
}
// Reverse creates a reversed comparable.
func Reverse(comparable Comparable) Comparable {
return reversedComparable{
comparable: comparable,
}
}
type reversedComparable struct {
comparable Comparable
}
var _ Comparable = reversedComparable{}
func (reversed reversedComparable) Compare(lhs, rhs interface{}) int {
return -reversed.comparable.Compare(lhs, rhs)
}
func (reversed reversedComparable) CalcScore(key interface{}) float64 {
return -reversed.comparable.CalcScore(key)
}