1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Letournel\PathFinder\Core; |
4
|
|
|
|
5
|
|
|
class NodeGraph |
6
|
|
|
{ |
7
|
|
|
private |
8
|
|
|
$edges, |
|
|
|
|
9
|
|
|
$vertexes, |
10
|
|
|
$symetric; |
11
|
|
|
|
12
|
|
|
public function __construct(array $nodes) |
13
|
|
|
{ |
14
|
|
|
$this->edges = array(); |
15
|
|
|
$this->vertexes = array(); |
16
|
|
|
$this->symetric = true; |
17
|
|
|
foreach($nodes as $node) |
18
|
|
|
{ |
19
|
|
|
if($node instanceof Node) |
20
|
|
|
{ |
21
|
|
|
$this->vertexes[$node->getId()] = $node; |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function setSymetricMode($symetric) |
27
|
|
|
{ |
28
|
|
|
$this->symetric = $symetric; |
29
|
|
|
|
30
|
|
|
return $this; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function createEdgeBetween(Node $a, Node $b, $value) |
34
|
|
|
{ |
35
|
|
|
$aId = $a->getId(); |
36
|
|
|
$bId = $b->getId(); |
37
|
|
|
if(! array_key_exists($aId, $this->vertexes) || ! array_key_exists($bId, $this->vertexes)) |
38
|
|
|
{ |
39
|
|
|
return; |
40
|
|
|
} |
41
|
|
|
if($aId === $bId) |
42
|
|
|
{ |
43
|
|
|
return; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if($this->symetric === true) |
47
|
|
|
{ |
48
|
|
|
$this->edges[$bId][$aId] = (float) $value; |
49
|
|
|
} |
50
|
|
|
$this->edges[$aId][$bId] = (float) $value; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getEdgeBetween(Node $a, Node $b) |
54
|
|
|
{ |
55
|
|
|
$aId = $a->getId(); |
56
|
|
|
$bId = $b->getId(); |
57
|
|
View Code Duplication |
if(! array_key_exists($aId, $this->vertexes) || ! array_key_exists($bId, $this->vertexes)) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
return null; |
60
|
|
|
} |
61
|
|
|
if($aId === $bId) |
62
|
|
|
{ |
63
|
|
|
return 0; |
64
|
|
|
} |
65
|
|
|
if(! array_key_exists($aId, $this->edges)) |
66
|
|
|
{ |
67
|
|
|
return INF; |
68
|
|
|
} |
69
|
|
|
if(! array_key_exists($bId, $this->edges[$aId])) |
70
|
|
|
{ |
71
|
|
|
return INF; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->edges[$aId][$bId]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function existsEdgeBetween(Node $a, Node $b) |
78
|
|
|
{ |
79
|
|
|
$aId = $a->getId(); |
80
|
|
|
$bId = $b->getId(); |
81
|
|
View Code Duplication |
if(! array_key_exists($aId, $this->vertexes) || ! array_key_exists($bId, $this->vertexes)) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
if($aId === $bId) |
86
|
|
|
{ |
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
if(! array_key_exists($aId, $this->edges)) |
90
|
|
|
{ |
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
if(! array_key_exists($bId, $this->edges[$aId])) |
94
|
|
|
{ |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function containsEdges(Node $a, Node $b) |
102
|
|
|
{ |
103
|
|
|
$aId = $a->getId(); |
104
|
|
|
$bId = $b->getId(); |
105
|
|
|
|
106
|
|
|
return array_key_exists($aId, $this->vertexes) && array_key_exists($bId, $this->vertexes); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function removeEdgeBetween(Node $a, Node $b) |
110
|
|
|
{ |
111
|
|
|
$aId = $a->getId(); |
112
|
|
|
$bId = $b->getId(); |
113
|
|
|
if(! array_key_exists($aId, $this->vertexes) || ! array_key_exists($bId, $this->vertexes)) |
114
|
|
|
{ |
115
|
|
|
return; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
unset($this->edges[$aId][$bId]); |
119
|
|
|
unset($this->edges[$bId][$aId]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function getVertexes() |
123
|
|
|
{ |
124
|
|
|
return $this->vertexes; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function getVertex($id) |
128
|
|
|
{ |
129
|
|
|
if(array_key_exists($id, $this->vertexes)) |
130
|
|
|
{ |
131
|
|
|
return $this->vertexes[$id]; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return null; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
View Code Duplication |
public function getEdgesFrom(Node $vertexFrom) |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$edges = array(); |
140
|
|
|
$vertexFromId = $vertexFrom->getId(); |
141
|
|
|
foreach($this->vertexes as $vertexToId => $vertexTo) |
142
|
|
|
{ |
143
|
|
|
if(isset($this->edges[$vertexFromId][$vertexToId])) |
144
|
|
|
{ |
145
|
|
|
$edges[$vertexToId] = $this->edges[$vertexFromId][$vertexToId]; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $edges; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
View Code Duplication |
public function getEdgesTo(Node $vertexTo) |
|
|
|
|
153
|
|
|
{ |
154
|
|
|
$edges = array(); |
155
|
|
|
$vertexToId = $vertexTo->getId(); |
156
|
|
|
foreach($this->vertexes as $vertexFromId => $vertexFrom) |
157
|
|
|
{ |
158
|
|
|
if(isset($this->edges[$vertexFromId][$vertexToId])) |
159
|
|
|
{ |
160
|
|
|
$edges[$vertexToId] = $this->edges[$vertexFromId][$vertexToId]; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $edges; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
View Code Duplication |
public function computeLength(NodePath $route) |
|
|
|
|
168
|
|
|
{ |
169
|
|
|
$length = 0; |
170
|
|
|
$prevNode = null; |
171
|
|
|
foreach($route as $node) |
172
|
|
|
{ |
173
|
|
|
if($prevNode === null) |
174
|
|
|
{ |
175
|
|
|
$prevNode = $node; |
176
|
|
|
continue; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$length += $this->getEdgeBetween($prevNode, $node); |
180
|
|
|
$prevNode = $node; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $length; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.