Skip to content

Commit ab9df5d

Browse files
committed
Update Algorithm\Tree to use Set\Vertices
1 parent d1a743b commit ab9df5d

File tree

5 files changed

+57
-38
lines changed

5 files changed

+57
-38
lines changed

src/Tree/Base.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Fhaculty\Graph\Algorithm\BaseGraph;
66
use Fhaculty\Graph\Graph;
77
use Fhaculty\Graph\Vertex;
8+
use Fhaculty\Graph\Set\Vertices;
89
use Fhaculty\Graph\Exception\UnderflowException;
910
use Fhaculty\Graph\Exception\UnexpectedValueException;
1011
use Fhaculty\Graph\Algorithm\Search\StrictDepthFirst;
@@ -65,24 +66,24 @@ abstract public function isVertexInternal(Vertex $vertex);
6566
/**
6667
* get array of leaf vertices (outermost vertices with no children)
6768
*
68-
* @return Vertex[]
69+
* @return Vertices
6970
* @uses Graph::getVertices()
7071
* @uses self::isVertexLeaf()
7172
*/
7273
public function getVerticesLeaf()
7374
{
74-
return array_filter($this->graph->getVertices(), array($this, 'isVertexLeaf'));
75+
return $this->graph->getVertices()->getVerticesMatch(array($this, 'isVertexLeaf'));
7576
}
7677

7778
/**
7879
* get array of internal vertices
7980
*
80-
* @return Vertex[]
81+
* @return Vertices
8182
* @uses Graph::getVertices()
8283
* @uses self::isVertexInternal()
8384
*/
8485
public function getVerticesInternal()
8586
{
86-
return array_filter($this->graph->getVertices(), array($this, 'isVertexInternal'));
87+
return $this->graph->getVertices()->getVerticesMatch(array($this, 'isVertexInternal'));
8788
}
8889
}

src/Tree/BaseDirected.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Fhaculty\Graph\Exception\UnderflowException;
77
use Fhaculty\Graph\Exception\UnexpectedValueException;
88
use Fhaculty\Graph\Vertex;
9+
use Fhaculty\Graph\Set\Vertices;
910

1011
/**
1112
* Abstract algorithm base class for working with directed, rooted trees
@@ -116,20 +117,20 @@ public function getVertexParent(Vertex $vertex)
116117
{
117118
$parents = $this->getVerticesParent($vertex);
118119
if (count($parents) !== 1) {
119-
if (!$parents) {
120+
if ($parents->isEmpty()) {
120121
throw new UnderflowException('No parents for given vertex found');
121122
} else {
122123
throw new UnexpectedValueException('More than one parent');
123124
}
124125
}
125-
return current($parents);
126+
return $parents->getVertexFirst();
126127
}
127128

128129
/**
129130
* get array of child vertices for given $vertex
130131
*
131132
* @param Vertex $vertex
132-
* @return Vertex[]
133+
* @return Vertices
133134
* @throws UnexpectedValueException if the given $vertex contains invalid / parallel edges (check isTree()!)
134135
*/
135136
abstract public function getVerticesChildren(Vertex $vertex);
@@ -141,7 +142,7 @@ abstract public function getVerticesChildren(Vertex $vertex);
141142
* which has none.
142143
*
143144
* @param Vertex $vertex
144-
* @return Vertex[]
145+
* @return Vertices
145146
* @throws UnexpectedValueException if the given $vertex contains invalid / parallel edges (check isTree()!)
146147
*/
147148
abstract protected function getVerticesParent(Vertex $vertex);
@@ -181,7 +182,7 @@ public function isVertexLeaf(Vertex $vertex)
181182
*/
182183
public function isVertexInternal(Vertex $vertex)
183184
{
184-
return ($this->getVerticesParent($vertex) && $this->getVerticesChildren($vertex));
185+
return (!$this->getVerticesParent($vertex)->isEmpty() && !$this->getVerticesChildren($vertex)->isEmpty());
185186
}
186187

187188
/**
@@ -275,7 +276,7 @@ public function getHeightVertex(Vertex $vertex)
275276
*
276277
* @param Vertex $vertex
277278
* @throws UnexpectedValueException if there are invalid edges (check isTree()!)
278-
* @return Vertex[]
279+
* @return Vertices
279280
* @uses self::getVerticesSubtreeRecursive()
280281
* @uses self::getVerticesSubtree()
281282
*/
@@ -284,7 +285,7 @@ public function getVerticesSubtree(Vertex $vertex)
284285
$vertices = array();
285286
$this->getVerticesSubtreeRecursive($vertex, $vertices);
286287

287-
return $vertices;
288+
return new Vertices($vertices);
288289
}
289290

290291
/**
@@ -296,7 +297,7 @@ public function getVerticesSubtree(Vertex $vertex)
296297
* @uses self::getVerticesChildren()
297298
* @uses self::getVerticesSubtreeRecursive() to recurse into subtrees
298299
*/
299-
private function getVerticesSubtreeRecursive(Vertex $vertex, &$vertices)
300+
private function getVerticesSubtreeRecursive(Vertex $vertex, array &$vertices)
300301
{
301302
$vid = $vertex->getId();
302303
if (isset($vertices[$vid])) {
@@ -315,15 +316,15 @@ private function getVerticesSubtreeRecursive(Vertex $vertex, &$vertices)
315316
* think of this as the recursive version of getVerticesChildren()
316317
*
317318
* @param Vertex $vertex
318-
* @return Vertex[]
319+
* @return Vertices
319320
* @throws UnexpectedValueException if there are invalid edges (check isTree()!)
320321
* @uses self::getVerticesSubtree()
321322
*/
322323
public function getVerticesDescendant(Vertex $vertex)
323324
{
324-
$vertices = $this->getVerticesSubtree($vertex);
325+
$vertices = $this->getVerticesSubtree($vertex)->getMap();
325326
unset($vertices[$vertex->getId()]);
326327

327-
return $vertices;
328+
return new Vertices($vertices);
328329
}
329330
}

src/Tree/Undirected.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Fhaculty\Graph\Algorithm\Search\Base as Search;
99
use Fhaculty\Graph\Algorithm\Search\StrictDepthFirst;
1010
use Fhaculty\Graph\Vertex;
11+
use Fhaculty\Graph\Set\Vertices;
1112
use Fhaculty\Graph\Edge\Base as Edge;
1213
use Fhaculty\Graph\Edge\UndirectedId as UndirectedEdge;
1314

@@ -105,7 +106,7 @@ public function isVertexInternal(Vertex $vertex)
105106
* @uses self::getVerticesNeighbor()
106107
* @uses self::getVerticesSubtreeRecursive() to recurse into sub-subtrees
107108
*/
108-
private function getVerticesSubtreeRecursive(Vertex $vertex, &$vertices, Vertex $ignore = null)
109+
private function getVerticesSubtreeRecursive(Vertex $vertex, array &$vertices, Vertex $ignore = null)
109110
{
110111
if (isset($vertices[$vertex->getId()])) {
111112
// vertex already visited => must be a cycle
@@ -128,7 +129,7 @@ private function getVerticesSubtreeRecursive(Vertex $vertex, &$vertices, Vertex
128129
*
129130
* @param Vertex $vertex
130131
* @throws UnexpectedValueException for directed edges
131-
* @return Vertex[] (might include possible duplicates)
132+
* @return Vertices (might include possible duplicates)
132133
* @uses Vertex::getEdges()
133134
* @uses Edge::getVertexToFrom()
134135
* @see Vertex::getVerticesEdge()
@@ -143,6 +144,6 @@ private function getVerticesNeighbor(Vertex $vertex)
143144
}
144145
$vertices[] = $edge->getVertexToFrom($vertex);
145146
}
146-
return $vertices;
147+
return new Vertices($vertices);
147148
}
148149
}

tests/Tree/BaseDirectedTest.php

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,30 @@
44
use Fhaculty\Graph\Exception\UnderflowException;
55
use Fhaculty\Graph\Exception\UnexpectedValueException;
66
use Fhaculty\Graph\Graph;
7+
use Fhaculty\Graph\Set\Vertices;
78

89
abstract class BaseDirectedTest extends TestCase
910
{
11+
/**
12+
*
13+
* @param Graph $graph
14+
* @return BaseDirected
15+
*/
1016
abstract protected function createTreeAlg(Graph $graph);
1117

18+
/**
19+
* @return Graph
20+
*/
1221
abstract protected function createGraphNonTree();
1322

23+
/**
24+
* @return Graph
25+
*/
1426
abstract protected function createGraphTree();
1527

28+
/**
29+
* @return Graph
30+
*/
1631
abstract protected function createGraphParallelEdge();
1732

1833
public function testEmptyGraph()
@@ -21,8 +36,8 @@ public function testEmptyGraph()
2136

2237
$tree = $this->createTreeAlg($graph);
2338
$this->assertTrue($tree->isTree());
24-
$this->assertEquals(array(), $tree->getVerticesLeaf());
25-
$this->assertEquals(array(), $tree->getVerticesInternal());
39+
$this->assertTrue($tree->getVerticesLeaf()->isEmpty());
40+
$this->assertTrue($tree->getVerticesInternal()->isEmpty());
2641

2742
return $tree;
2843
}
@@ -62,24 +77,25 @@ public function testGraphTree()
6277
$graph = $this->createGraphTree();
6378
$root = $graph->getVertexFirst();
6479

65-
$nonRoot = $graph->getVertices();
80+
$nonRoot = $graph->getVertices()->getMap();
6681
unset($nonRoot[$root->getId()]);
82+
$nonRoot = new Vertices($nonRoot);
6783

68-
$c1 = current($nonRoot);
84+
$c1 = $nonRoot->getVertexFirst();
6985

7086
$tree = $this->createTreeAlg($graph);
7187

7288
$this->assertTrue($tree->isTree());
7389
$this->assertSame($root, $tree->getVertexRoot());
74-
$this->assertSame(array_values($graph->getVertices()), array_values($tree->getVerticesSubtree($root)));
75-
$this->assertSame(array_values($nonRoot), array_values($tree->getVerticesChildren($root)));
76-
$this->assertSame(array_values($nonRoot), array_values($tree->getVerticesDescendant($root)));
77-
$this->assertSame(array_values($nonRoot), array_values($tree->getVerticesLeaf()));
78-
$this->assertSame(array(), array_values($tree->getVerticesInternal()));
90+
$this->assertSame($graph->getVertices()->getVector(), $tree->getVerticesSubtree($root)->getVector());
91+
$this->assertSame($nonRoot->getVector(), $tree->getVerticesChildren($root)->getVector());
92+
$this->assertSame($nonRoot->getVector(), $tree->getVerticesDescendant($root)->getVector());
93+
$this->assertSame($nonRoot->getVector(), $tree->getVerticesLeaf()->getVector());
94+
$this->assertSame(array(), $tree->getVerticesInternal()->getVector());
7995
$this->assertSame($root, $tree->getVertexParent($c1));
80-
$this->assertSame(array(), $tree->getVerticesChildren($c1));
81-
$this->assertSame(array(), $tree->getVerticesDescendant($c1));
82-
$this->assertSame(array($c1), array_values($tree->getVerticesSubtree($c1)));
96+
$this->assertSame(array(), $tree->getVerticesChildren($c1)->getVector());
97+
$this->assertSame(array(), $tree->getVerticesDescendant($c1)->getVector());
98+
$this->assertSame(array($c1), $tree->getVerticesSubtree($c1)->getVector());
8399
$this->assertEquals(2, $tree->getDegree());
84100
$this->assertEquals(0, $tree->getDepthVertex($root));
85101
$this->assertEquals(1, $tree->getDepthVertex($c1));

tests/Tree/UndirectedTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public function testGraphEmpty()
1818
$tree = $this->createTree($graph);
1919

2020
$this->assertTrue($tree->isTree());
21-
$this->assertSame(array(), $tree->getVerticesInternal());
22-
$this->assertSame(array(), $tree->getVerticesLeaf());
21+
$this->assertSame(array(), $tree->getVerticesInternal()->getVector());
22+
$this->assertSame(array(), $tree->getVerticesLeaf()->getVector());
2323
}
2424

2525
public function testGraphTrivial()
@@ -29,8 +29,8 @@ public function testGraphTrivial()
2929

3030
$tree = $this->createTree($graph);
3131
$this->assertTrue($tree->isTree());
32-
$this->assertSame(array(), $tree->getVerticesInternal());
33-
$this->assertSame(array(), $tree->getVerticesLeaf());
32+
$this->assertSame(array(), $tree->getVerticesInternal()->getVector());
33+
$this->assertSame(array(), $tree->getVerticesLeaf()->getVector());
3434
}
3535

3636
public function testGraphSimplePair()
@@ -41,8 +41,8 @@ public function testGraphSimplePair()
4141

4242
$tree = $this->createTree($graph);
4343
$this->assertTrue($tree->isTree());
44-
$this->assertSame(array(), $tree->getVerticesInternal());
45-
$this->assertSame($graph->getVertices(), $tree->getVerticesLeaf());
44+
$this->assertSame(array(), $tree->getVerticesInternal()->getVector());
45+
$this->assertSame($graph->getVertices()->getVector(), $tree->getVerticesLeaf()->getVector());
4646
}
4747

4848
public function testGraphSimpleLine()
@@ -54,8 +54,8 @@ public function testGraphSimpleLine()
5454

5555
$tree = $this->createTree($graph);
5656
$this->assertTrue($tree->isTree());
57-
$this->assertSame(array($graph->getVertex('v2')), array_values($tree->getVerticesInternal()));
58-
$this->assertSame(array($graph->getVertex('v1'), $graph->getVertex('v3')), array_values($tree->getVerticesLeaf()));
57+
$this->assertSame(array($graph->getVertex('v2')), $tree->getVerticesInternal()->getVector());
58+
$this->assertSame(array($graph->getVertex('v1'), $graph->getVertex('v3')), $tree->getVerticesLeaf()->getVector());
5959
}
6060

6161
public function testGraphPairParallelIsNotTree()

0 commit comments

Comments
 (0)