-
Notifications
You must be signed in to change notification settings - Fork 1
/
pathGraph.go
93 lines (78 loc) · 1.84 KB
/
pathGraph.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
package kstar
import "container/heap"
type pathGraph struct {
hin map[int]*pathGraphHeap
ht map[int]*pathGraphHeap
r rNode
}
func newPathGraph() *pathGraph {
var pg pathGraph
pg.hin = make(map[int]*pathGraphHeap)
pg.ht = make(map[int]*pathGraphHeap)
pg.r = rNode{}
return &pg
}
func (pg *pathGraph) updateHinNodes(edges []Edge, as *astar) {
for _, e := range edges {
if _, ok := pg.hin[e.V]; !ok {
pg.hin[e.V] = newPathGraphHeap()
}
hin := pg.hin[e.V]
n := hinNode{
u: e.U,
v: e.V,
i: e.I,
d: as.dValue(e),
vHin: hin,
hts: &pg.ht,
}
currentTop := hin.Top()
exists, pos := hin.exists(n.EdgeKeys())
if exists {
hin.replace(hin.pq[pos], n)
heap.Fix(hin, pos)
} else {
heap.Push(hin, n)
}
top := hin.Top().(hinNode)
if currentTop != nil && !currentTop.(hinNode).equals(top) {
currentTopHin := currentTop.(hinNode)
pg.propagateHinTopChange(¤tTopHin, &top, as)
}
}
}
func (pg *pathGraph) propagateHinTopChange(oldNode, newNode *hinNode, as *astar) {
current := as.g.T()
currentHt := pg.ht[current]
for ok, pos := currentHt.exists(oldNode.u, oldNode.v, oldNode.i); ok; {
currentHt.replace(oldNode, newNode)
heap.Fix(currentHt, pos)
current = as.searchTreeParents[current].U
currentHt = pg.ht[current]
}
}
func (pg *pathGraph) generateHts(as *astar) {
pg.generateHt(as.g.S(), as.g.S(), as)
}
func (pg *pathGraph) generateHt(n, s int, as *astar) {
if n == s {
pg.ht[n] = newPathGraphHeap()
} else {
parent := as.searchTreeParents[n]
htParent := pg.ht[parent.U]
pg.ht[n] = new(pathGraphHeap)
copyHt(htParent, pg.ht[n])
}
ht := pg.ht[n]
hin := pg.hin[n]
if hin != nil {
hinRoot := hin.Top().(hinNode)
heap.Push(ht, htNode{
hinNode: &hinRoot,
ht: ht,
})
}
for child := range as.searchTreeChildren[n] {
pg.generateHt(child, s, as)
}
}