Skip to content

Commit 1eafe38

Browse files
authored
Merge pull request #170 from clue-labs/directed-loop
Fix returning directed loop edges & adjacent vertices from vertex twice
2 parents a5455ed + 2e62e78 commit 1eafe38

2 files changed

Lines changed: 48 additions & 4 deletions

File tree

src/Vertex.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,18 @@ public function getEdges()
229229
public function getEdgesOut()
230230
{
231231
$that = $this;
232+
$prev = null;
232233

233-
return $this->getEdges()->getEdgesMatch(function (Edge $edge) use ($that) {
234-
return $edge->hasVertexStart($that);
234+
return $this->getEdges()->getEdgesMatch(function (Edge $edge) use ($that, &$prev) {
235+
$ret = $edge->hasVertexStart($that);
236+
237+
// skip duplicate directed loop edges
238+
if ($edge === $prev && $edge instanceof EdgeDirected) {
239+
$ret = false;
240+
}
241+
$prev = $edge;
242+
243+
return $ret;
235244
});
236245
}
237246

@@ -243,9 +252,18 @@ public function getEdgesOut()
243252
public function getEdgesIn()
244253
{
245254
$that = $this;
255+
$prev = null;
256+
257+
return $this->getEdges()->getEdgesMatch(function (Edge $edge) use ($that, &$prev) {
258+
$ret = $edge->hasVertexTarget($that);
259+
260+
// skip duplicate directed loop edges
261+
if ($edge === $prev && $edge instanceof EdgeDirected) {
262+
$ret = false;
263+
}
264+
$prev = $edge;
246265

247-
return $this->getEdges()->getEdgesMatch(function (Edge $edge) use ($that) {
248-
return $edge->hasVertexTarget($that);
266+
return $ret;
249267
});
250268
}
251269

tests/VertexTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,32 @@ public function testEdges()
7575
$this->assertEquals(array($v3, $v4), $this->vertex->getVerticesEdgeFrom()->getVector());
7676
}
7777

78+
public function testUndirectedLoopEdgeReturnsEdgeTwiceInAndOut()
79+
{
80+
$edge = $this->vertex->createEdge($this->vertex);
81+
82+
$this->assertEquals(array($edge, $edge), $this->vertex->getEdges()->getVector());
83+
$this->assertEquals(array($edge, $edge), $this->vertex->getEdgesIn()->getVector());
84+
$this->assertEquals(array($edge, $edge), $this->vertex->getEdgesOut()->getVector());
85+
86+
$this->assertEquals(array($this->vertex, $this->vertex), $this->vertex->getVerticesEdge()->getVector());
87+
$this->assertEquals(array($this->vertex, $this->vertex), $this->vertex->getVerticesEdgeTo()->getVector());
88+
$this->assertEquals(array($this->vertex, $this->vertex), $this->vertex->getVerticesEdgeFrom()->getVector());
89+
}
90+
91+
public function testDirectedLoopEdgeReturnsEdgeTwiceUndirectedAndOnceEachInAndOut()
92+
{
93+
$edge = $this->vertex->createEdgeTo($this->vertex);
94+
95+
$this->assertEquals(array($edge, $edge), $this->vertex->getEdges()->getVector());
96+
$this->assertEquals(array($edge), $this->vertex->getEdgesIn()->getVector());
97+
$this->assertEquals(array($edge), $this->vertex->getEdgesOut()->getVector());
98+
99+
$this->assertEquals(array($this->vertex, $this->vertex), $this->vertex->getVerticesEdge()->getVector());
100+
$this->assertEquals(array($this->vertex), $this->vertex->getVerticesEdgeTo()->getVector());
101+
$this->assertEquals(array($this->vertex), $this->vertex->getVerticesEdgeFrom()->getVector());
102+
}
103+
78104
public function testBalance()
79105
{
80106
$this->vertex->setBalance(10);

0 commit comments

Comments
 (0)