Skip to content

Commit

Permalink
Detach the right splos.
Browse files Browse the repository at this point in the history
Fixes #13.

Conflicts:
	src/Gliph/Graph/AdjacencyList.php
	src/Gliph/Graph/DirectedAdjacencyList.php
	src/Gliph/Graph/UndirectedAdjacencyList.php
	tests/Gliph/Graph/DirectedAdjacencyListTest.php
	tests/Gliph/Graph/UndirectedAdjacencyListTest.php
  • Loading branch information
sdboyer committed Aug 3, 2014
1 parent d66ae44 commit db9e4b7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/Gliph/Graph/AdjacencyList.php
Expand Up @@ -75,11 +75,11 @@ public function eachAdjacent($vertex, $callback) {
throw new NonexistentVertexException('Vertex is not in graph; cannot iterate over its adjacent vertices.');
}

foreach ($this->_getTraversableSplos($this->vertices[$vertex]) as $adjacent_vertex) {
$set = $this->_getTraversableSplos($this->vertices[$vertex]);
foreach ($set as $adjacent_vertex) {
call_user_func($callback, $adjacent_vertex);
}
$this->walking->detach($this->vertices[$vertex]);

$this->walking->detach($set);
return $this;
}

Expand Down Expand Up @@ -121,7 +121,7 @@ protected function fev($callback) {
$outgoing = $set->getInfo();
$callback($vertex, $outgoing);
}
$this->walking->detach($this->vertices);
$this->walking->detach($set);

return $this;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Gliph/Graph/DirectedAdjacencyList.php
Expand Up @@ -52,10 +52,11 @@ public function eachEdge($callback) {
$edges = array();
$that = $this;
$this->fev(function ($from, $outgoing) use (&$edges, $that) {
foreach ($that->_getTraversableSplos($outgoing) as $to) {
$set = $that->_getTraversableSplos($outgoing);
foreach ($set as $to) {
$edges[] = array($from, $to);
}
$that->_cleanupSplosTraversal($outgoing);
$that->_cleanupSplosTraversal($set);
});

foreach ($edges as $edge) {
Expand Down
5 changes: 3 additions & 2 deletions src/Gliph/Graph/UndirectedAdjacencyList.php
Expand Up @@ -49,12 +49,13 @@ public function eachEdge($callback) {
$complete = new \SplObjectStorage();
$that = $this;
$this->fev(function ($a, $adjacent) use (&$edges, &$complete, $that) {
foreach ($that->_getTraversableSplos($adjacent) as $b) {
$set = $that->_getTraversableSplos($adjacent);
foreach ($set as $b) {
if (!$complete->contains($b)) {
$edges[] = array($a, $b);
}
}
$that->_cleanupSplosTraversal($adjacent);
$that->_cleanupSplosTraversal($set);
$complete->attach($a);
});

Expand Down
12 changes: 11 additions & 1 deletion tests/Gliph/Graph/DirectedAdjacencyListTest.php
Expand Up @@ -65,8 +65,11 @@ public function testEachAdjacent() {
$g->eachAdjacent($a, function($to) use (&$found) {
$found[] = $to;
});
$g->eachAdjacent($a, function($to) use (&$found) {
$found[] = $to;
});
});
$this->assertEquals(array($b, $b, $c, $c, $b, $c), $found);
$this->assertEquals(array($b, $b, $c, $b, $c, $c, $b, $c, $b, $c), $found);
}

/**
Expand Down Expand Up @@ -129,12 +132,19 @@ public function testEachEdge() {
$g->eachEdge(function($edge) use (&$found) {
$found[] = $edge;
});
$g->eachEdge(function($edge) use (&$found) {
$found[] = $edge;
});
});

$expected = array(
array($a, $b),
array($a, $b),
array($a, $c),
array($a, $b),
array($a, $c),
array($a, $c),
array($a, $b),
array($a, $c),
array($a, $b),
array($a, $c),
Expand Down
30 changes: 22 additions & 8 deletions tests/Gliph/Graph/UndirectedAdjacencyListTest.php
Expand Up @@ -69,9 +69,13 @@ public function testEachAdjacent() {
$g->eachAdjacent($b, function($to) use (&$found) {
$found[] = $to;
});
$g->eachAdjacent($b, function($to) use (&$found) {
$found[] = $to;
});
});

$this->assertCount(6, $found);
$this->assertCount(10, $found);
$this->assertEquals(array($a, $a, $c, $a, $c, $c, $a, $c, $a, $c), $found);
}

/**
Expand Down Expand Up @@ -120,15 +124,25 @@ public function testEachEdge() {
$g->eachEdge(function($edge) use (&$found) {
$found[] = $edge;
});
$g->eachEdge(function($edge) use (&$found) {
$found[] = $edge;
});
});

$this->assertCount(6, $found);
$this->assertEquals(array($a, $b), $found[0]);
$this->assertEquals(array($a, $b), $found[1]);
$this->assertEquals(array($b, $c), $found[2]);
$this->assertEquals(array($b, $c), $found[3]);
$this->assertEquals(array($a, $b), $found[4]);
$this->assertEquals(array($b, $c), $found[5]);
$this->assertCount(10, $found);
$expected = array(
array($a, $b),
array($a, $b),
array($b, $c),
array($a, $b),
array($b, $c),
array($b, $c),
array($a, $b),
array($b, $c),
array($a, $b),
array($b, $c),
);
$this->assertEquals($expected, $found);
}

/**
Expand Down

0 comments on commit db9e4b7

Please sign in to comment.