-
Notifications
You must be signed in to change notification settings - Fork 4
/
ballot.go
49 lines (42 loc) · 892 Bytes
/
ballot.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 scp
import (
"fmt"
)
// Ballot is an SCP ballot.
type Ballot struct {
N int
X Value
}
// ZeroBallot is the zero ballot.
var ZeroBallot Ballot
// IsZero tells whether b is the zero ballot.
func (b Ballot) IsZero() bool {
return b.N == 0 && isNilVal(b.X)
}
// Less tells whether a ballot is less than another.
func (b Ballot) Less(other Ballot) bool {
if b.N < other.N {
return true
}
if b.N > other.N {
return false
}
if isNilVal(b.X) {
return !isNilVal(other.X)
}
if isNilVal(other.X) {
return false
}
return b.X.Less(other.X)
}
// Equal tells whether a ballot is equal to another.
func (b Ballot) Equal(other Ballot) bool {
return b.N == other.N && ValueEqual(b.X, other.X)
}
// String produces a readable representation of a ballot.
func (b Ballot) String() string {
if b.IsZero() {
return "<>"
}
return fmt.Sprintf("<%d,%s>", b.N, VString(b.X))
}