/
vector_ops.go
96 lines (83 loc) · 2.31 KB
/
vector_ops.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
package vecfc
import (
"github.com/EktaMind/Thank_Sirus/inter/dag"
"github.com/EktaMind/Thank_Sirus/inter/idx"
"github.com/EktaMind/Thank_Sirus/vecengine"
)
func (b *LowestAfterSeq) InitWithEvent(i idx.Validator, e dag.Event) {
b.Set(i, e.Seq())
}
func (b *LowestAfterSeq) Visit(i idx.Validator, e dag.Event) bool {
if b.Get(i) != 0 {
return false
}
b.Set(i, e.Seq())
return true
}
func (b *HighestBeforeSeq) InitWithEvent(i idx.Validator, e dag.Event) {
b.Set(i, BranchSeq{Seq: e.Seq(), MinSeq: e.Seq()})
}
func (b *HighestBeforeSeq) IsEmpty(i idx.Validator) bool {
seq := b.Get(i)
return !seq.IsForkDetected() && seq.Seq == 0
}
func (b *HighestBeforeSeq) IsForkDetected(i idx.Validator) bool {
return b.Get(i).IsForkDetected()
}
func (b *HighestBeforeSeq) Seq(i idx.Validator) idx.Event {
val := b.Get(i)
return val.Seq
}
func (b *HighestBeforeSeq) MinSeq(i idx.Validator) idx.Event {
val := b.Get(i)
return val.MinSeq
}
func (b *HighestBeforeSeq) SetForkDetected(i idx.Validator) {
b.Set(i, forkDetectedSeq)
}
func (self *HighestBeforeSeq) CollectFrom(_other vecengine.HighestBeforeI, num idx.Validator) {
other := _other.(*HighestBeforeSeq)
for branchID := idx.Validator(0); branchID < num; branchID++ {
hisSeq := other.Get(branchID)
if hisSeq.Seq == 0 && !hisSeq.IsForkDetected() {
// hisSeq doesn't observe anything about this branchID
continue
}
mySeq := self.Get(branchID)
if mySeq.IsForkDetected() {
// mySeq observes the maximum already
continue
}
if hisSeq.IsForkDetected() {
// set fork detected
self.SetForkDetected(branchID)
} else {
if mySeq.Seq == 0 || mySeq.MinSeq > hisSeq.MinSeq {
// take hisSeq.MinSeq
mySeq.MinSeq = hisSeq.MinSeq
self.Set(branchID, mySeq)
}
if mySeq.Seq < hisSeq.Seq {
// take hisSeq.Seq
mySeq.Seq = hisSeq.Seq
self.Set(branchID, mySeq)
}
}
}
}
func (self *HighestBeforeSeq) GatherFrom(to idx.Validator, _other vecengine.HighestBeforeI, from []idx.Validator) {
other := _other.(*HighestBeforeSeq)
// read all branches to find highest event
highestBranchSeq := BranchSeq{}
for _, branchID := range from {
branch := other.Get(branchID)
if branch.IsForkDetected() {
highestBranchSeq = branch
break
}
if branch.Seq > highestBranchSeq.Seq {
highestBranchSeq = branch
}
}
self.Set(to, highestBranchSeq)
}