-
Notifications
You must be signed in to change notification settings - Fork 0
/
pair.go
78 lines (62 loc) · 1.54 KB
/
pair.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
package gost
import "fmt"
// Pair is a tuple of two values.
type Pair[K any, V any] struct {
Key K
Value V
}
// impl Display for Pair
func (p Pair[K, V]) ToString() string {
keyToString := castToToString[K](p.Key)
valueToString := castToToString[V](p.Value)
var key String
var value String
if keyToString.IsSome() {
key = keyToString.Unwrap().ToString()
} else {
key = String(fmt.Sprintf("%v", p.Key))
}
if valueToString.IsSome() {
value = valueToString.Unwrap().ToString()
} else {
value = String(fmt.Sprintf("%v", p.Value))
}
return fmt.Sprintf("Pair[%s, %s]", key, value)
}
// impl Display for Pair
func (p Pair[K, V]) Display() string {
return p.ToString()
}
// impl Debug for Pair
func (p Pair[K, V]) Debug() string {
return p.ToString()
}
// impl Clone for Pair
func (p Pair[K, V]) Clone() Pair[K, V] {
keyClone := castToClone[K](p.Key).Unwrap().Clone()
valueClone := castToClone[V](p.Value).Unwrap().Clone()
return Pair[K, V]{
Key: keyClone,
Value: valueClone,
}
}
// impl Eq for Pair
func (p Pair[K, V]) Eq(other Pair[K, V]) Bool {
keyEq := castToEq[K](p.Key).Unwrap().Eq(other.Key)
valueEq := castToEq[V](p.Value).Unwrap().Eq(other.Value)
return keyEq && valueEq
}
// impl Ord for Pair
func (p Pair[K, V]) Cmp(other Pair[K, V]) Ordering {
keyOrd := castToOrd[K](p.Key).Unwrap().Cmp(other.Key)
if keyOrd == OrderingEqual {
valueOrd := castToOrd[V](p.Value).Unwrap().Cmp(other.Value)
return valueOrd
} else {
return keyOrd
}
}
// impl AsRef for Pair
func (p Pair[K, V]) AsRef() *Pair[K, V] {
return &p
}