Skip to content

Commit 72b6404

Browse files
committed
Each getVertices*() method should return a Set\Vertices instance
1 parent 3bebe1c commit 72b6404

File tree

13 files changed

+41
-44
lines changed

13 files changed

+41
-44
lines changed

src/Bipartit.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function getColors()
5151
$colors = array();
5252

5353
// get color for each vertex
54-
foreach ($this->graph->getVertices() as $vid => $startVertex) {
54+
foreach ($this->graph->getVertices()->getMap() as $vid => $startVertex) {
5555
if (!isset($colors[$vid])) {
5656
$queue = array($startVertex);
5757
// initialize each components color
@@ -92,7 +92,7 @@ public function getColorVertices()
9292
$colors = $this->getColors();
9393
$ret = array(0 => array(), 1 => array());
9494

95-
foreach ($this->graph->getVertices() as $vid => $vertex) {
95+
foreach ($this->graph->getVertices()->getMap() as $vid => $vertex) {
9696
$ret[$colors[$vid]][$vid] = $vertex;
9797
}
9898

@@ -113,7 +113,7 @@ public function createGraphGroups()
113113
$colors = $this->getColors();
114114

115115
$graph = $this->graph->createGraphClone();
116-
foreach ($graph->getVertices() as $vid => $vertex) {
116+
foreach ($graph->getVertices()->getMap() as $vid => $vertex) {
117117
$vertex->setGroup($colors[$vid]);
118118
}
119119

src/Complete.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Complete extends BaseGraph
2626
public function isComplete()
2727
{
2828
// copy of array (separate iterator but same vertices)
29-
$c = $vertices = $this->graph->getVertices();
29+
$c = $vertices = $this->graph->getVertices()->getVector();
3030
// from each vertex
3131
foreach ($vertices as $vertex) {
3232
// to each vertex

src/ConnectedComponents.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function getNumberOfComponents()
6666
$components = 0;
6767

6868
// for each vertices
69-
foreach ($this->graph->getVertices() as $vid => $vertex) {
69+
foreach ($this->graph->getVertices()->getMap() as $vid => $vertex) {
7070
// did I visit this vertex before?
7171
if (!isset($visitedVertices[$vid])) {
7272

@@ -99,7 +99,7 @@ public function createGraphsComponents()
9999
$graphs = array();
100100

101101
// for each vertices
102-
foreach ($this->graph->getVertices() as $vid => $vertex) {
102+
foreach ($this->graph->getVertices()->getMap() as $vid => $vertex) {
103103
// did I visit this vertex before?
104104
if (!isset($visitedVertices[$vid])) {
105105

@@ -108,7 +108,7 @@ public function createGraphsComponents()
108108
$newVertices = $alg->getVertices();
109109

110110
// mark the vertices of this component as visited
111-
foreach ($newVertices as $vid => $unusedVertex) {
111+
foreach ($newVertices->getIds() as $vid) {
112112
$visitedVertices[$vid] = true;
113113
}
114114

src/DetectNegativeCycle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function getCycleNegative()
4343
// remember vertices already visited, as they can not lead to a new cycle
4444
$verticesVisited = array();
4545
// check for all vertices
46-
foreach ($this->graph->getVertices() as $vid => $vertex) {
46+
foreach ($this->graph->getVertices()->getMap() as $vid => $vertex) {
4747
// skip vertices already visited
4848
if (!isset($verticesVisited[$vid])) {
4949
// start MBF algorithm on current vertex

src/Groups.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Fhaculty\Graph\Algorithm\BaseGraph;
66
use Fhaculty\Graph\Graph;
7+
use Fhaculty\Graph\Set\Vertices;
78

89
class Groups extends BaseGraph
910
{
@@ -66,21 +67,21 @@ public function getGroups()
6667
}
6768

6869
/**
69-
* get array of all vertices in the given group
70+
* get set of all Vertices in the given group
7071
*
7172
* @param int $group
72-
* @return Vertex[]
73+
* @return Vertices
7374
* @uses Vertex::getGroup()
7475
*/
7576
public function getVerticesGroup($group)
7677
{
7778
$vertices = array();
78-
foreach ($this->graph->getVertices() as $vid => $vertex) {
79+
foreach ($this->graph->getVertices()->getMap() as $vid => $vertex) {
7980
if ($vertex->getGroup() === $group) {
8081
$vertices[$vid] = $vertex;
8182
}
8283
}
8384

84-
return $vertices;
85+
return new Vertices($vertices);
8586
}
8687
}

src/MinimumCostFlow/SuccessiveShortestPath.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ private function isBalanceReached(Graph $graph)
133133
if ($graph->getNumberOfVertices() !== $this->graph->getNumberOfVertices()) {
134134
throw new DomainException('Given graph does not appear to be a clone of input graph');
135135
}
136-
foreach ($this->graph->getVertices() as $vid => $vertex) {
136+
foreach ($this->graph->getVertices()->getMap() as $vid => $vertex) {
137137
if ($vertex->getBalance() !== $graph->getVertex($vid)->getBalance()) {
138138
return false;
139139
}
@@ -152,7 +152,7 @@ private function isBalanceReached(Graph $graph)
152152
*/
153153
private function getVertexSource(Graph $graph)
154154
{
155-
foreach ($graph->getVertices() as $vid => $vertex) {
155+
foreach ($graph->getVertices()->getMap() as $vid => $vertex) {
156156
if ($this->graph->getVertex($vid)->getBalance() - $vertex->getBalance() > 0) {
157157
return $vertex;
158158
}
@@ -174,7 +174,7 @@ private function getVertexSink(Vertex $source)
174174
// search for reachable Vertices
175175
$algBFS = new SearchBreadthFirst($source);
176176

177-
foreach ($algBFS->getVertices() as $vid => $vertex) {
177+
foreach ($algBFS->getVertices()->getMap() as $vid => $vertex) {
178178
if ($this->graph->getVertex($vid)->getBalance() - $vertex->getBalance() < 0) {
179179
return $vertex;
180180
}

src/Search/Base.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
namespace Fhaculty\Graph\Algorithm\Search;
44

55
use Fhaculty\Graph\Algorithm\BaseVertex;
6-
76
use Fhaculty\Graph\Exception\DomainException;
8-
97
use Fhaculty\Graph\Exception\InvalidArgumentException;
108
use Fhaculty\Graph\Vertex;
9+
use Fhaculty\Graph\Set\Vertices;
1110

1211
abstract class Base extends BaseVertex
1312
{
@@ -60,9 +59,9 @@ public function getNumberOfVertices()
6059
}
6160

6261
/**
63-
* get array of all vertices that can be reached from start vertex
62+
* get set of all Vertices that can be reached from start vertex
6463
*
65-
* @return Vertex[]
64+
* @return Vertices
6665
*/
6766
abstract public function getVertices();
6867

@@ -74,6 +73,6 @@ abstract public function getVertices();
7473
*/
7574
public function getVerticesIds()
7675
{
77-
return array_keys($this->getVertices());
76+
return $this->getVertices()->getIds();
7877
}
7978
}

src/Search/BreadthFirst.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
use Fhaculty\Graph\Vertex;
66
use Fhaculty\Graph\Graph;
7+
use Fhaculty\Graph\Set\Vertices;
78

89
class BreadthFirst extends Base
910
{
1011
/**
1112
*
12-
* @return Vertex[]
13+
* @return Vertices
1314
*/
1415
public function getVertices()
1516
{
@@ -40,6 +41,6 @@ public function getVertices()
4041
// untill queue is empty
4142
} while ($queue);
4243

43-
return $visited;
44+
return new Vertices($visited);
4445
}
4546
}

src/Search/DepthFirst.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Fhaculty\Graph\Algorithm\Search;
44

55
use Fhaculty\Graph\Vertex;
6+
use Fhaculty\Graph\Set\Vertices;
67

78
class DepthFirst extends Base
89
{
@@ -45,14 +46,13 @@ private function iterativeDepthFirstSearch(Vertex $vertex)
4546
}
4647
}
4748

48-
return $visited;
49+
return new Vertices($visited);
4950
}
5051

5152
/**
52-
*
5353
* calculates a recursive depth-first search
5454
*
55-
* @return Vertex[]
55+
* @return Vertices
5656
*/
5757
public function getVertices()
5858
{

src/ShortestPath/Base.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44

55
use Fhaculty\Graph\Algorithm\BaseVertex;
66
use Fhaculty\Graph\Walk;
7-
87
use Fhaculty\Graph\Exception\UnderflowException;
9-
108
use Fhaculty\Graph\Exception\InvalidArgumentException;
11-
129
use Fhaculty\Graph\Vertex;
1310
use Fhaculty\Graph\Edge\Base as Edge;
11+
use Fhaculty\Graph\Set\Vertices;
1412

1513
abstract class Base extends BaseVertex
1614
{
@@ -94,22 +92,22 @@ private function sumEdges(array $edges)
9492
}
9593

9694
/**
97-
* get array of all vertices the given start vertex has a path to
95+
* get set of all Vertices the given start vertex has a path to
9896
*
99-
* @return Vertex[]
97+
* @return Vertices
10098
* @uses AlgorithmSp::getDistanceMap()
10199
*/
102100
public function getVertices()
103101
{
104102
$vertices = array();
105103
$map = $this->getDistanceMap();
106-
foreach ($this->vertex->getGraph()->getVertices() as $vid => $vertex) {
104+
foreach ($this->vertex->getGraph()->getVertices()->getMap() as $vid => $vertex) {
107105
if (isset($map[$vid])) {
108106
$vertices[$vid] = $vertex;
109107
}
110108
}
111109

112-
return $vertices;
110+
return new Vertices($vertices);
113111
}
114112

115113
/**
@@ -135,7 +133,7 @@ public function getDistanceMap()
135133
{
136134
$edges = $this->getEdges();
137135
$ret = array();
138-
foreach ($this->vertex->getGraph()->getVertices() as $vid => $vertex) {
136+
foreach ($this->vertex->getGraph()->getVertices()->getMap() as $vid => $vertex) {
139137
try {
140138
$ret[$vid] = $this->sumEdges($this->getEdgesToInternal($vertex, $edges));
141139
} catch (UnderflowException $ignore) {
@@ -182,7 +180,7 @@ public function createGraph()
182180
*/
183181
protected function getEdgesCheapestPredecesor(array $predecessor)
184182
{
185-
$vertices = $this->vertex->getGraph()->getVertices();
183+
$vertices = $this->vertex->getGraph()->getVertices()->getMap();
186184
// start vertex doesn't have a predecessor
187185
unset($vertices[$this->vertex->getId()]);
188186

0 commit comments

Comments
 (0)