-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcomparable.go
More file actions
92 lines (73 loc) · 2.81 KB
/
comparable.go
File metadata and controls
92 lines (73 loc) · 2.81 KB
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
package binarytree
import(
"bytes"
)
// Comparable is an interface for comparable types. All keys used in this implementation of
// a binary tree must implement this interface.
//
// Helper types are supplied for some primitives, please see `IntKey`, [StringKey] and [ByteSliceKey]
type Comparable interface {
LessThan(Comparable) bool
EqualTo(Comparable) bool
GreaterThan(Comparable) bool
ValueOf() interface{}
}
// IntKey is a type of base type int that implements the Comparable interface.
type IntKey int
// Return true if this key is less than the supplied IntKey.
func (me IntKey) LessThan(other Comparable) bool {
return me < other.(IntKey)
}
// Return true if this key is equal to the supplied IntKey.
func (me IntKey) EqualTo(other Comparable) bool {
return me == other.(IntKey)
}
// Return true if this key is greater than the supplied IntKey.
func (me IntKey) GreaterThan(other Comparable) bool {
return me > other.(IntKey)
}
// Return the int value as an interface
func (me IntKey) ValueOf() interface{} {
return int(me)
}
// StringKey is a type of base type String that implements the Comparable interface.
type StringKey string
// Return true if this key is less than the supplied StringKey.
func (me StringKey) LessThan(other Comparable) bool {
return me < other.(StringKey)
}
// Return true if this key is equal to the supplied StringKey.
func (me StringKey) EqualTo(other Comparable) bool {
return me == other.(StringKey)
}
// Return true if this key is greater than the supplied StringKey.
func (me StringKey) GreaterThan(other Comparable) bool {
return me > other.(StringKey)
}
// Return the string value as an interface
func (me StringKey) ValueOf() interface{} {
return string(me)
}
// StringKey is a type of base type String that implements the Comparable interface.
type ByteSliceKey []byte
// Return true if this key is less than the supplied ByteSliceKey.
func (me ByteSliceKey) LessThan(other Comparable) bool {
if len(me) > len(other.(ByteSliceKey)) { return false }
if len(me) < len(other.(ByteSliceKey)) { return true }
return bytes.Compare(me.ValueOf().([]byte), other.ValueOf().([]byte)) < 0
}
// Return true if this key is equal to the supplied ByteSliceKey.
func (me ByteSliceKey) EqualTo(other Comparable) bool {
if len(me) != len(other.(ByteSliceKey)) { return false }
return bytes.Compare(me.ValueOf().([]byte), other.ValueOf().([]byte)) == 0
}
// Return true if this key is greater than the supplied ByteSliceKey.
func (me ByteSliceKey) GreaterThan(other Comparable) bool {
if len(me) < len(other.(ByteSliceKey)) { return false }
if len(me) > len(other.(ByteSliceKey)) { return true }
return bytes.Compare(me.ValueOf().([]byte), other.ValueOf().([]byte)) > 0
}
// Return the []byte value as an interface
func (me ByteSliceKey) ValueOf() interface{} {
return []byte(me)
}