-
Notifications
You must be signed in to change notification settings - Fork 1
/
m3event.go
137 lines (115 loc) · 3.27 KB
/
m3event.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
package m3space
import (
"github.com/freddy33/qsm-go/m3path"
"github.com/freddy33/qsm-go/m3point"
)
type EventID int
const (
NilEvent = EventID(-1)
)
type DistAndTime int
type EventColor uint8
const (
RedEvent EventColor = 1 << iota
GreenEvent
BlueEvent
YellowEvent
)
// TODO: This should be in the space data entry of the environment
var AllColors = [4]EventColor{RedEvent, GreenEvent, BlueEvent, YellowEvent}
type Event struct {
id EventID
space *Space
pathNodeMap m3path.PathNodeMap
node Node
created DistAndTime
color EventColor
pathContext m3path.PathContext
}
type SpacePathNodeMap struct {
space *Space
id EventID
size int
}
func (spnm *SpacePathNodeMap) Size() int {
panic("implement me")
}
func (spnm *SpacePathNodeMap) Clear() {
panic("implement me")
}
func (spnm *SpacePathNodeMap) Range(f func(point m3point.Point, pn m3path.PathNode) bool, nbProc int) {
panic("implement me")
}
/***************************************************************/
// SpacePathNodeMap Functions
/***************************************************************/
func (spnm *SpacePathNodeMap) GetSize() int {
return spnm.size
}
func (spnm *SpacePathNodeMap) GetPathNode(p m3point.Point) m3path.PathNode {
res, ok := spnm.space.nodesMap.Load(p)
if ok {
pathNode := res.(Node).GetPathNode(spnm.id)
if pathNode != nil {
return pathNode
}
}
return nil
}
func (spnm *SpacePathNodeMap) AddPathNode(pathNode m3path.PathNode) (m3path.PathNode, bool) {
n := spnm.space.getOrCreateNode(pathNode.P())
nbLatest := n.GetNbLatestEvents()
n.addPathNode(spnm.id, pathNode, spnm.space)
spnm.size++
// New latest node
if nbLatest == 0 {
spnm.space.latestNodes = append(spnm.space.latestNodes, n)
}
return pathNode, true
}
func (spnm *SpacePathNodeMap) IsActive(pathNode m3path.PathNode) bool {
n := spnm.space.GetNode(pathNode.P())
if n != nil {
return n.IsPathNodeActive(pathNode, spnm.space)
}
return false
}
/***************************************************************/
// Event Functions
/***************************************************************/
func (space *Space) CreateEvent(ctxType m3point.GrowthType, idx int, offset int, p m3point.Point, k EventColor) *Event {
pnm := &SpacePathNodeMap{space, space.lastIdCounter, 0}
space.lastIdCounter++
ppd := m3point.GetPointPackData(space.env)
ctx := m3path.MakePathContextDBFromGrowthContext(space.env, ppd.GetGrowthContextByTypeAndIndex(ctxType, idx), offset)
e := Event{pnm.id, space, pnm,nil, space.currentTime, k, ctx}
space.events[pnm.id] = &e
ctx.InitRootNode(p)
// TODO: Remove PathNodeMap need. Use DB
pnm.AddPathNode(ctx.GetRootPathNode())
e.node = space.GetNode(p)
space.activeNodes.addNode(e.node)
return &e
}
func (space *Space) CreateEventFromColor(p m3point.Point, k EventColor) *Event {
idx, offset := getIndexAndOffsetForColor(k)
return space.CreateEvent(8, idx, offset, p, k)
}
func getIndexAndOffsetForColor(k EventColor) (int, int) {
switch k {
case RedEvent:
return 0, 0
case GreenEvent:
return 4, 0
case BlueEvent:
return 8, 0
case YellowEvent:
return 10, 4
}
Log.Errorf("Event color unknown %v", k)
return -1, -1
}
func (evt *Event) LatestDistance() DistAndTime {
// DistAndTime and time are the same...
return DistAndTime(evt.space.currentTime - evt.created)
}