Skip to content

Commit

Permalink
Fix returning directed loop edges & adjacent vertices from vertex twice
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Sep 28, 2019
1 parent a5455ed commit 2e62e78
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/Vertex.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,18 @@ public function getEdges()
public function getEdgesOut()
{
$that = $this;
$prev = null;

return $this->getEdges()->getEdgesMatch(function (Edge $edge) use ($that) {
return $edge->hasVertexStart($that);
return $this->getEdges()->getEdgesMatch(function (Edge $edge) use ($that, &$prev) {
$ret = $edge->hasVertexStart($that);

// skip duplicate directed loop edges
if ($edge === $prev && $edge instanceof EdgeDirected) {
$ret = false;
}
$prev = $edge;

return $ret;
});
}

Expand All @@ -243,9 +252,18 @@ public function getEdgesOut()
public function getEdgesIn()
{
$that = $this;
$prev = null;

return $this->getEdges()->getEdgesMatch(function (Edge $edge) use ($that, &$prev) {
$ret = $edge->hasVertexTarget($that);

// skip duplicate directed loop edges
if ($edge === $prev && $edge instanceof EdgeDirected) {
$ret = false;
}
$prev = $edge;

return $this->getEdges()->getEdgesMatch(function (Edge $edge) use ($that) {
return $edge->hasVertexTarget($that);
return $ret;
});
}

Expand Down
26 changes: 26 additions & 0 deletions tests/VertexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,32 @@ public function testEdges()
$this->assertEquals(array($v3, $v4), $this->vertex->getVerticesEdgeFrom()->getVector());
}

public function testUndirectedLoopEdgeReturnsEdgeTwiceInAndOut()
{
$edge = $this->vertex->createEdge($this->vertex);

$this->assertEquals(array($edge, $edge), $this->vertex->getEdges()->getVector());
$this->assertEquals(array($edge, $edge), $this->vertex->getEdgesIn()->getVector());
$this->assertEquals(array($edge, $edge), $this->vertex->getEdgesOut()->getVector());

$this->assertEquals(array($this->vertex, $this->vertex), $this->vertex->getVerticesEdge()->getVector());
$this->assertEquals(array($this->vertex, $this->vertex), $this->vertex->getVerticesEdgeTo()->getVector());
$this->assertEquals(array($this->vertex, $this->vertex), $this->vertex->getVerticesEdgeFrom()->getVector());
}

public function testDirectedLoopEdgeReturnsEdgeTwiceUndirectedAndOnceEachInAndOut()
{
$edge = $this->vertex->createEdgeTo($this->vertex);

$this->assertEquals(array($edge, $edge), $this->vertex->getEdges()->getVector());
$this->assertEquals(array($edge), $this->vertex->getEdgesIn()->getVector());
$this->assertEquals(array($edge), $this->vertex->getEdgesOut()->getVector());

$this->assertEquals(array($this->vertex, $this->vertex), $this->vertex->getVerticesEdge()->getVector());
$this->assertEquals(array($this->vertex), $this->vertex->getVerticesEdgeTo()->getVector());
$this->assertEquals(array($this->vertex), $this->vertex->getVerticesEdgeFrom()->getVector());
}

public function testBalance()
{
$this->vertex->setBalance(10);
Expand Down

0 comments on commit 2e62e78

Please sign in to comment.