Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 12 additions & 69 deletions src/Set/Edges.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Graphp\Graph\Set;

use Graphp\Graph\Edge;
use Graphp\Graph\Exception\InvalidArgumentException;
use Graphp\Graph\Exception\OutOfBoundsException;
use Graphp\Graph\Exception\UnderflowException;

Expand All @@ -18,38 +17,6 @@
*/
class Edges implements \Countable, \IteratorAggregate, EdgesAggregate
{
/**
* order by edge weight
*
* @var int
* @see Edge::getWeight()
*/
const ORDER_WEIGHT = 1;

/**
* order by edge capacity
*
* @var int
* @see Edge::getCapacity()
*/
const ORDER_CAPACITY = 2;

/**
* order by remaining capacity on edge (maximum capacity - current flow)
*
* @var int
* @see Edge::getCapacityRemaining()
*/
const ORDER_CAPACITY_REMAINING = 3;

/**
* order by edge flow
*
* @var int
* @see Edge::getFlow()
*/
const ORDER_FLOW = 4;

protected $edges = array();

/**
Expand Down Expand Up @@ -237,10 +204,9 @@ public function getEdgesMatch($callbackCheck)
*
* Edge index positions will be left unchanged.
*
* @param string|int $orderBy criterium to sort by. see edge attribute names or self::ORDER_WEIGHT, etc.
* @param bool $desc whether to return biggest first (true) instead of smallest first (default:false)
* @return Edges a new Edges set ordered by the given $orderBy criterium
* @throws InvalidArgumentException if criterium is unknown
* @param string|callable(Edge):number $orderBy criterium to sort by. see edge attribute names or custom callable
* @param bool $desc whether to return biggest first (true) instead of smallest first (default:false)
* @return Edges a new Edges set ordered by the given $orderBy criterium
*/
public function getEdgesOrder($orderBy, $desc = false)
{
Expand Down Expand Up @@ -291,11 +257,10 @@ public function getEdgesShuffled()
/**
* get first edge ordered by given criterium $orderBy
*
* @param string|int $orderBy criterium to sort by. see edge attribute names or self::ORDER_WEIGHT, etc.
* @param bool $desc whether to return biggest (true) instead of smallest (default:false)
* @param string|callable(Edge):number $orderBy criterium to sort by. see edge attribute names or custom callable
* @param bool $desc whether to return biggest (true) instead of smallest (default:false)
* @return Edge
* @throws InvalidArgumentException if criterium is unknown
* @throws UnderflowException if no edges exist
* @throws UnderflowException if no edges exist
*/
public function getEdgeOrder($orderBy, $desc=false)
{
Expand Down Expand Up @@ -431,17 +396,14 @@ public function getIterator()
/**
* call given $callback on each Edge and sum their results
*
* @param callable $callback
* @param string|callable(Edge):number $callback callback to sum by. See edge attribute names or custom callback
* @return number
* @throws InvalidArgumentException for invalid callbacks
* @uses self::getCallback()
*/
public function getSumCallback($callback)
{
$callback = $this->getCallback($callback);

// return array_sum(array_map($callback, $this->edges));

$sum = 0;
foreach ($this->edges as $edge) {
$sum += $callback($edge);
Expand All @@ -464,9 +426,8 @@ private function getEdgeMatchOrNull($callbackCheck)
/**
* get callback/Closure to be called on Edge instances for given callback identifier
*
* @param callable|string|int $callback
* @throws InvalidArgumentException
* @return callable
* @param string|callable(Edge):number $callback
* @return callable(Edge):number
*/
private function getCallback($callback)
{
Expand All @@ -479,27 +440,9 @@ private function getCallback($callback)
return $callback;
}

if (is_string($callback)) {
return function (Edge $edge) use ($callback) {
return $edge->getAttribute($callback);
};
}

static $methods = array(
self::ORDER_WEIGHT => 'getWeight',
self::ORDER_CAPACITY => 'getCapacity',
self::ORDER_CAPACITY_REMAINING => 'getCapacityRemaining',
self::ORDER_FLOW => 'getFlow'
);

if (!is_int($callback) || !isset($methods[$callback])) {
throw new InvalidArgumentException('Invalid callback given');
}

$method = $methods[$callback];

return function (Edge $edge) use ($method) {
return $edge->$method();
return function (Edge $edge) use ($callback) {
return $edge->getAttribute($callback);
};

}
}
62 changes: 11 additions & 51 deletions src/Set/Vertices.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Graphp\Graph\Set;

use Graphp\Graph\Vertex;
use Graphp\Graph\Exception\InvalidArgumentException;
use Graphp\Graph\Exception\OutOfBoundsException;
use Graphp\Graph\Exception\UnderflowException;

Expand All @@ -18,22 +17,6 @@
*/
class Vertices implements \Countable, \IteratorAggregate, VerticesAggregate
{
/**
* order by vertex ID
*
* @var int
* @see Vertex::getId()
*/
const ORDER_ID = 1;

/**
* order by vertex group
*
* @var int
* @see Vertex::getGroup()
*/
const ORDER_GROUP = 6;

protected $vertices = array();

/**
Expand Down Expand Up @@ -241,10 +224,9 @@ public function getVerticesMatch($callbackCheck)
* Vertex index positions will be left unchanged, so if you call this method
* on a VerticesMap, it will also return a VerticesMap.
*
* @param string|int $orderBy criterium to sort by. see vertex attribute names or Vertex::ORDER_ID, etc.
* @param bool $desc whether to return biggest first (true) instead of smallest first (default:false)
* @return Vertices a new Vertices set ordered by the given $orderBy criterium
* @throws InvalidArgumentException if criterium is unknown
* @param string|callable(Vertex):number $orderBy criterium to sort by. see vertex attribute names or custom callback
* @param bool $desc whether to return biggest first (true) instead of smallest first (default:false)
* @return Vertices a new Vertices set ordered by the given $orderBy criterium
* @see self::getVertexOrder()
*/
public function getVerticesOrder($orderBy, $desc = false)
Expand Down Expand Up @@ -329,11 +311,10 @@ public function getVerticesIntersection($otherVertices)
/**
* get first vertex (optionally ordered by given criterium $by) from given array of vertices
*
* @param string|int $orderBy criterium to sort by. see vertex attribute names or Vertex::ORDER_ID, etc.
* @param bool $desc whether to return biggest (true) instead of smallest (default:false)
* @param string|callable(Vertex):number $orderBy criterium to sort by. see vertex attribute names custom callback
* @param bool $desc whether to return biggest (true) instead of smallest (default:false)
* @return Vertex
* @throws InvalidArgumentException if criterium is unknown
* @throws UnderflowException if no vertices exist
* @throws UnderflowException if no vertices exist
* @see self::getVerticesOrder()
*/
public function getVertexOrder($orderBy, $desc=false)
Expand Down Expand Up @@ -470,17 +451,14 @@ public function getIterator()
/**
* call given $callback on each Vertex and sum their results
*
* @param callable $callback
* @param string|callable(Vertex):number $callback callback to sum by. See vertex attribute names or custom callback
* @return number
* @throws InvalidArgumentException for invalid callbacks
* @uses self::getCallback()
*/
public function getSumCallback($callback)
{
$callback = $this->getCallback($callback);

// return array_sum(array_map($callback, $this->vertices));

$sum = 0;
foreach ($this->vertices as $vertex) {
$sum += $callback($vertex);
Expand Down Expand Up @@ -510,9 +488,8 @@ private function getVertexMatchOrNull($callbackCheck)
/**
* get callback/Closure to be called on Vertex instances for given callback identifier
*
* @param callable|string|int $callback
* @throws InvalidArgumentException
* @return callable
* @param string|callable(Vertex):number $callback
* @return callable(Vertex):number
*/
private function getCallback($callback)
{
Expand All @@ -525,25 +502,8 @@ private function getCallback($callback)
return $callback;
}

if (is_string($callback)) {
return function (Vertex $vertex) use ($callback) {
return $vertex->getAttribute($callback);
};
}

static $methods = array(
self::ORDER_ID => 'getId',
self::ORDER_GROUP => 'getGroup'
);

if (!is_int($callback) || !isset($methods[$callback])) {
throw new InvalidArgumentException('Invalid callback given');
}

$method = $methods[$callback];

return function (Vertex $vertex) use ($method) {
return $vertex->$method();
return function (Vertex $vertex) use ($callback) {
return $vertex->getAttribute($callback);
};
}
}
24 changes: 12 additions & 12 deletions src/Walk.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public static function factoryFromEdges($edges, Vertex $startVertex)
/**
* create new walk instance between given set of Vertices / array of Vertex instances
*
* @param Vertices|Vertex[] $vertices
* @param null|string|null $orderBy
* @param bool $desc
* @param Vertices|Vertex[] $vertices
* @param null|string|callable(Edge):number $orderBy
* @param bool $desc
* @return Walk
* @throws UnderflowException if no vertices were given
* @throws UnderflowException if no vertices were given
* @see Edges::getEdgeOrder() for parameters $by and $desc
*/
public static function factoryFromVertices($vertices, $orderBy = null, $desc = false)
Expand Down Expand Up @@ -76,10 +76,10 @@ public static function factoryFromVertices($vertices, $orderBy = null, $desc = f
/**
* create new cycle instance from given predecessor map
*
* @param Vertex[] $predecessors map of vid => predecessor vertex instance
* @param Vertex $vertex start vertex to search predecessors from
* @param null|string|int $orderBy
* @param bool $desc
* @param Vertex[] $predecessors map of vid => predecessor vertex instance
* @param Vertex $vertex start vertex to search predecessors from
* @param null|string|callable(Edge):number $orderBy
* @param bool $desc
* @return Walk
* @throws UnderflowException
* @see Edges::getEdgeOrder() for parameters $by and $desc
Expand Down Expand Up @@ -127,11 +127,11 @@ public static function factoryCycleFromPredecessorMap(array $predecessors, Verte
/**
* create new cycle instance with edges between given vertices
*
* @param Vertex[]|Vertices $vertices
* @param null|string|int $orderBy
* @param bool $desc
* @param Vertex[]|Vertices $vertices
* @param null|string|callable(Edge):number $orderBy
* @param bool $desc
* @return Walk
* @throws UnderflowException if no vertices were given
* @throws UnderflowException if no vertices were given
* @see Edges::getEdgeOrder() for parameters $by and $desc
* @uses self::factoryFromVertices()
*/
Expand Down
14 changes: 10 additions & 4 deletions tests/Set/BaseVerticesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public function testEmpty()
$this->assertEquals(array(), $vertices->getVector());
$this->assertTrue($vertices->isEmpty());
$this->assertTrue($vertices->getVertices()->isEmpty());
$this->assertTrue($vertices->getVerticesOrder(Vertices::ORDER_ID)->isEmpty());
$this->assertTrue($vertices->getVerticesOrder(function (Vertex $vertex) {
return $vertex->getId();
})->isEmpty());
$this->assertTrue($vertices->getVerticesOrder('id')->isEmpty());
$this->assertTrue($vertices->getVerticesDistinct()->isEmpty());
$this->assertTrue($vertices->getVerticesMatch(function() { })->isEmpty());
Expand Down Expand Up @@ -90,7 +92,7 @@ public function testEmptyDoesNotHaveRandom(Vertices $vertices)
*/
public function testEmptyDoesNotHaveOrdered(Vertices $vertices)
{
$vertices->getVertexOrder(Vertices::ORDER_ID);
$vertices->getVertexOrder('group');
}

public function testTwo()
Expand Down Expand Up @@ -227,14 +229,18 @@ public function testOrderByGroup()
$biggest = $graph->createVertex()->setGroup(200);

$vertices = $graph->getVertices();
$verticesOrdered = $vertices->getVerticesOrder(Vertices::ORDER_GROUP);
$verticesOrdered = $vertices->getVerticesOrder(function (Vertex $vertex) {
return $vertex->getGroup();
});

$this->assertInstanceOf('Graphp\Graph\Set\Vertices', $verticesOrdered);
$this->assertEquals(1, $verticesOrdered->getVertexFirst()->getGroup());
$this->assertEquals(200, $verticesOrdered->getVertexLast()->getGroup());

$this->assertSame($biggest, $verticesOrdered->getVertexLast());
$this->assertSame($biggest, $vertices->getVertexOrder(Vertices::ORDER_GROUP, true));
$this->assertSame($biggest, $vertices->getVertexOrder(function (Vertex $vertex) {
return $vertex->getGroup();
}, true));

$sumgroups = function(Vertex $vertex) {
return $vertex->getGroup();
Expand Down
Loading