-
Notifications
You must be signed in to change notification settings - Fork 211
/
haretypes.go
283 lines (235 loc) · 5.35 KB
/
haretypes.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package hare
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/spacemeshos/go-spacemesh/common/types"
"hash/fnv"
"sort"
)
type instanceID types.LayerID
type messageType byte
// declare all known message types
const (
status messageType = 0
proposal messageType = 1
commit messageType = 2
notify messageType = 3
pre messageType = 10
)
// declare round identifiers
const (
preRound = -1
statusRound = 0
proposalRound = 1
commitRound = 2
notifyRound = 3
)
const defaultSetSize = 200
func (mType messageType) String() string {
switch mType {
case status:
return "Status"
case proposal:
return "Proposal"
case commit:
return "Commit"
case notify:
return "Notify"
case pre:
return "PreRound"
default:
return "Unknown message type"
}
}
func (id instanceID) Bytes() []byte {
idInBytes := make([]byte, 4)
binary.LittleEndian.PutUint32(idInBytes, uint32(id))
return idInBytes
}
// Set represents a unique set of values.
type Set struct {
values map[types.BlockID]struct{}
id uint32
isIDValid bool
}
// NewDefaultEmptySet creates an empty set with the default size.
func NewDefaultEmptySet() *Set {
return NewEmptySet(defaultSetSize)
}
// NewEmptySet creates an empty set with the provided size.
func NewEmptySet(size int) *Set {
s := &Set{}
s.values = make(map[types.BlockID]struct{}, size)
s.id = 0
s.isIDValid = false
return s
}
// NewSetFromValues creates a set of the provided values.
// Note: duplicated values are ignored.
func NewSetFromValues(values ...types.BlockID) *Set {
s := &Set{}
s.values = make(map[types.BlockID]struct{}, len(values))
for _, v := range values {
s.Add(v)
}
s.id = 0
s.isIDValid = false
return s
}
// NewSet creates a set from the provided array of values.
// Note: duplicated values are ignored.
func NewSet(data []types.BlockID) *Set {
s := &Set{}
s.isIDValid = false
s.values = make(map[types.BlockID]struct{}, len(data))
for _, bid := range data {
s.values[bid] = struct{}{}
}
return s
}
// Clone creates a copy of the set.
func (s *Set) Clone() *Set {
clone := NewEmptySet(len(s.values))
for bid := range s.values {
clone.Add(bid)
}
return clone
}
// Contains returns true if the provided value is contained in the set, false otherwise.
func (s *Set) Contains(id types.BlockID) bool {
_, exist := s.values[id]
return exist
}
// Add a value to the set.
// It has no effect if the value already exists in the set.
func (s *Set) Add(id types.BlockID) {
if _, exist := s.values[id]; exist {
return
}
s.isIDValid = false
s.values[id] = struct{}{}
}
// Remove a value from the set.
// It has no effect if the value doesn't exist in the set.
func (s *Set) Remove(id types.BlockID) {
if _, exist := s.values[id]; !exist {
return
}
s.isIDValid = false
delete(s.values, id)
}
// Equals returns true if the provided set represents this set, false otherwise.
func (s *Set) Equals(g *Set) bool {
if len(s.values) != len(g.values) {
return false
}
for bid := range s.values {
if _, exist := g.values[bid]; !exist {
return false
}
}
return true
}
// ToSlice returns the array representation of the set.
func (s *Set) ToSlice() []types.BlockID {
// order keys
keys := make([]types.BlockID, len(s.values))
i := 0
for k := range s.values {
keys[i] = k
i++
}
sort.Slice(keys, func(i, j int) bool { return bytes.Compare(keys[i].Bytes(), keys[j].Bytes()) == -1 })
l := make([]types.BlockID, 0, len(s.values))
for i := range keys {
l = append(l, keys[i])
}
return l
}
func (s *Set) updateID() {
// order keys
keys := make([]types.BlockID, len(s.values))
i := 0
for k := range s.values {
keys[i] = k
i++
}
sort.Slice(keys, func(i, j int) bool { return bytes.Compare(keys[i].Bytes(), keys[j].Bytes()) == -1 })
// calc
h := fnv.New32()
for i := 0; i < len(keys); i++ {
h.Write(keys[i].Bytes())
}
// update
s.id = h.Sum32()
s.isIDValid = true
}
// ID returns the ObjectID of the set.
func (s *Set) ID() uint32 {
if !s.isIDValid {
s.updateID()
}
return s.id
}
func (s *Set) String() string {
// TODO: should improve
b := new(bytes.Buffer)
for v := range s.values {
fmt.Fprintf(b, "%v,", v.String())
}
if b.Len() >= 1 {
return b.String()[:b.Len()-1]
}
return b.String()
}
// IsSubSetOf returns true if s is a subset of g, false otherwise.
func (s *Set) IsSubSetOf(g *Set) bool {
for v := range s.values {
if !g.Contains(v) {
return false
}
}
return true
}
// Intersection returns the intersection a new set which represents the intersection of s and g.
func (s *Set) Intersection(g *Set) *Set {
both := NewEmptySet(len(s.values))
for v := range s.values {
if g.Contains(v) {
both.Add(v)
}
}
return both
}
// Union returns a new set which represents the union set of s and g.
func (s *Set) Union(g *Set) *Set {
union := NewEmptySet(len(s.values) + len(g.values))
for v := range s.values {
union.Add(v)
}
for v := range g.values {
union.Add(v)
}
return union
}
// Complement returns a new set that represents the complement of s relatively to the world u.
func (s *Set) Complement(u *Set) *Set {
comp := NewEmptySet(len(u.values))
for v := range u.values {
if !s.Contains(v) {
comp.Add(v)
}
}
return comp
}
// Subtract g from s.
func (s *Set) Subtract(g *Set) {
for v := range g.values {
s.Remove(v)
}
}
// Size returns the number of elements in the set.
func (s *Set) Size() int {
return len(s.values)
}