Skip to content

Commit 214de45

Browse files
authored
Merge pull request #195 from clue-labs/remove-sets
Remove `Vertices` and `Edges collection classes, use plain arrays instead
2 parents eaff236 + 77ff8e1 commit 214de45

20 files changed

+377
-1729
lines changed

src/Edge.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,36 @@
22

33
namespace Graphp\Graph;
44

5-
use Graphp\Graph\Set\Vertices;
6-
use Graphp\Graph\Set\VerticesAggregate;
7-
85
/**
96
* Abstract base for `EdgeUndirected` and `EdgeDirected` containing common interfaces and behavior for all edges.
107
*
118
* @see EdgeUndirected
129
* @see EdgeDirected
1310
*/
14-
abstract class Edge extends Entity implements VerticesAggregate
11+
abstract class Edge extends Entity
1512
{
1613
protected $attributes = array();
1714

1815
/**
19-
* get Vertices that are a target of this edge
16+
* get vertices that are a target of this edge
2017
*
21-
* @return Vertices
18+
* @psalm-return list<Vertex>
19+
* @return Vertex[]
2220
*/
2321
abstract public function getVerticesTarget();
2422

2523
/**
26-
* get Vertices that are the start of this edge
24+
* get vertices that are the start of this edge
2725
*
28-
* @return Vertices
26+
* @psalm-return list<Vertex>
27+
* @return Vertex[]
2928
*/
3029
abstract public function getVerticesStart();
3130

3231
/**
3332
* return true if this edge is an outgoing edge of the given vertex (i.e. the given vertex is a valid start vertex of this edge)
3433
*
35-
* @param Vertex $startVertex
34+
* @param Vertex $startVertex
3635
* @return bool
3736
* @uses Vertex::getVertexToFrom()
3837
*/
@@ -41,7 +40,7 @@ abstract public function hasVertexStart(Vertex $startVertex);
4140
/**
4241
* return true if this edge is an ingoing edge of the given vertex (i . e. the given vertex is a valid end vertex of this edge)
4342
*
44-
* @param Vertex $targetVertex
43+
* @param Vertex $targetVertex
4544
* @return bool
4645
* @uses Vertex::getVertexFromTo()
4746
*/
@@ -77,11 +76,12 @@ abstract public function getVertexToFrom(Vertex $startVertex);
7776
abstract public function getVertexFromTo(Vertex $endVertex);
7877

7978
/**
80-
* get set of all Vertices this edge connects
79+
* get list of all vertices this edge connects
8180
*
82-
* @return Vertices
81+
* @psalm-return list<Vertex>
82+
* @return Vertex[]
8383
*/
84-
//abstract public function getVertices();
84+
abstract public function getVertices();
8585

8686
/**
8787
* get graph instance this edge is attached to
@@ -90,7 +90,11 @@ abstract public function getVertexFromTo(Vertex $endVertex);
9090
*/
9191
public function getGraph()
9292
{
93-
return $this->getVertices()->getVertexFirst()->getGraph();
93+
$vertices = $this->getVertices();
94+
$vertex = \reset($vertices);
95+
\assert($vertex instanceof Vertex);
96+
97+
return $vertex->getGraph();
9498
}
9599

96100
/**

src/EdgeDirected.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Graphp\Graph;
44

5-
use Graphp\Graph\Set\Vertices;
6-
75
class EdgeDirected extends Edge
86
{
97
/**
@@ -47,17 +45,17 @@ public function __construct(Vertex $from, Vertex $to, array $attributes = array(
4745

4846
public function getVerticesTarget()
4947
{
50-
return new Vertices(array($this->to));
48+
return array($this->to);
5149
}
5250

5351
public function getVerticesStart()
5452
{
55-
return new Vertices(array($this->from));
53+
return array($this->from);
5654
}
5755

5856
public function getVertices()
5957
{
60-
return new Vertices(array($this->from, $this->to));
58+
return array($this->from, $this->to);
6159
}
6260

6361
/**

src/EdgeUndirected.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Graphp\Graph;
44

5-
use Graphp\Graph\Set\Vertices;
6-
75
class EdgeUndirected extends Edge
86
{
97
/**
@@ -47,17 +45,17 @@ public function __construct(Vertex $a, Vertex $b, array $attributes = array())
4745

4846
public function getVerticesTarget()
4947
{
50-
return new Vertices(array($this->b, $this->a));
48+
return array($this->b, $this->a);
5149
}
5250

5351
public function getVerticesStart()
5452
{
55-
return new Vertices(array($this->a, $this->b));
53+
return array($this->a, $this->b);
5654
}
5755

5856
public function getVertices()
5957
{
60-
return new Vertices(array($this->a, $this->b));
58+
return array($this->a, $this->b);
6159
}
6260

6361
public function isConnection(Vertex $from, Vertex $to)

src/Graph.php

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22

33
namespace Graphp\Graph;
44

5-
use Graphp\Graph\Set\DualAggregate;
6-
use Graphp\Graph\Set\Edges;
7-
use Graphp\Graph\Set\Vertices;
8-
9-
class Graph extends Entity implements DualAggregate
5+
class Graph extends Entity
106
{
117
protected $vertices = array();
128
protected $edges = array();
@@ -20,23 +16,25 @@ public function __construct(array $attributes = array())
2016
}
2117

2218
/**
23-
* return set of Vertices added to this graph
19+
* return list of all vertices added to this graph
2420
*
25-
* @return Vertices
21+
* @psalm-return list<Vertex>
22+
* @return Vertex[]
2623
*/
2724
public function getVertices()
2825
{
29-
return new Vertices($this->vertices);
26+
return $this->vertices;
3027
}
3128

3229
/**
33-
* return set of ALL Edges added to this graph
30+
* return list of all edges added to this graph
3431
*
35-
* @return Edges
32+
* @psalm-return list<Edge>
33+
* @return Edge[]
3634
*/
3735
public function getEdges()
3836
{
39-
return new Edges($this->edges);
37+
return $this->edges;
4038
}
4139

4240
/**
@@ -97,7 +95,7 @@ public function createEdgeDirected(Vertex $source, Vertex $target, array $attrib
9795
*/
9896
public function withoutVertex(Vertex $vertex)
9997
{
100-
return $this->withoutVertices(new Vertices(array($vertex)));
98+
return $this->withoutVertices(array($vertex));
10199
}
102100

103101
/**
@@ -107,10 +105,10 @@ public function withoutVertex(Vertex $vertex)
107105
* silently be ignored. If neither of the vertices can be found in this graph,
108106
* the returned graph will be identical.
109107
*
110-
* @param Vertices $vertices
108+
* @param Vertex[] $vertices
111109
* @return self
112110
*/
113-
public function withoutVertices(Vertices $vertices)
111+
public function withoutVertices(array $vertices)
114112
{
115113
// keep copy of original vertices and edges and temporarily remove all $vertices and their adjacent edges
116114
$originalEdges = $this->edges;
@@ -133,7 +131,7 @@ public function withoutVertices(Vertices $vertices)
133131

134132
// clone graph with vertices/edges temporarily removed, then restore
135133
$clone = clone $this;
136-
$this->edges= $originalEdges;
134+
$this->edges = $originalEdges;
137135
$this->vertices = $originalVertices;
138136

139137
return $clone;
@@ -150,7 +148,7 @@ public function withoutVertices(Vertices $vertices)
150148
*/
151149
public function withoutEdge(Edge $edge)
152150
{
153-
return $this->withoutEdges(new Edges(array($edge)));
151+
return $this->withoutEdges(array($edge));
154152
}
155153

156154
/**
@@ -160,10 +158,10 @@ public function withoutEdge(Edge $edge)
160158
* silently be ignored. If neither of the edges can be found in this graph,
161159
* the returned graph will be identical.
162160
*
163-
* @param Edges $edges
161+
* @param Edge[] $edges
164162
* @return self
165163
*/
166-
public function withoutEdges(Edges $edges)
164+
public function withoutEdges(array $edges)
167165
{
168166
// keep copy of original edges and temporarily remove all $edges
169167
$original = $this->edges;
@@ -236,7 +234,7 @@ public function __clone()
236234
\assert($originalEdge instanceof Edge);
237235

238236
// use map to match old vertex hashes to new vertex objects
239-
$vertices = $originalEdge->getVertices()->getVector();
237+
$vertices = $originalEdge->getVertices();
240238
$v1 = $map[\spl_object_hash($vertices[0])];
241239
$v2 = $map[\spl_object_hash($vertices[1])];
242240

src/Set/DualAggregate.php

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)