From 2e62e78901b0814aca4260de120b2765d9123df6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sat, 28 Sep 2019 00:04:36 +0200 Subject: [PATCH] Fix returning directed loop edges & adjacent vertices from vertex twice --- src/Vertex.php | 26 ++++++++++++++++++++++---- tests/VertexTest.php | 26 ++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/Vertex.php b/src/Vertex.php index 13e5b9cf..4701c5ae 100644 --- a/src/Vertex.php +++ b/src/Vertex.php @@ -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; }); } @@ -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; }); } diff --git a/tests/VertexTest.php b/tests/VertexTest.php index 69103e39..3451aaa5 100644 --- a/tests/VertexTest.php +++ b/tests/VertexTest.php @@ -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);