@@ -26,11 +26,57 @@ class Parallel extends BaseGraph
26
26
public function hasEdgeParallel ()
27
27
{
28
28
foreach ($ this ->graph ->getEdges () as $ edge ) {
29
- if ($ edge -> hasEdgeParallel ( )) {
29
+ if ($ this -> hasEdgeParallelEdge ( $ edge )) {
30
30
return true ;
31
31
}
32
32
}
33
33
34
34
return false ;
35
35
}
36
+
37
+
38
+ /**
39
+ * checks whether this edge has any parallel edges
40
+ *
41
+ * @return boolean
42
+ * @uses Edge::getEdgesParallel()
43
+ */
44
+ public function hasEdgeParallelEdge (Edge $ edge )
45
+ {
46
+ return !!$ this ->getEdgesParallelEdge ($ edge );
47
+ }
48
+
49
+ /**
50
+ * get all edges parallel to this edge (excluding self)
51
+ *
52
+ * @return Edge[]
53
+ * @throws LogicException
54
+ */
55
+ public function getEdgesParallelEdge (Edge $ edge )
56
+ {
57
+ $ ends = $ edge ->getVertices ();
58
+
59
+ // get all edges between this edge's endpoints
60
+ $ edges = $ ends [0 ]->getEdgesTo ($ ends [1 ]);
61
+ // edge points into both directions (undirected/bidirectional edge)
62
+ if ($ edge ->isConnection ($ ends [1 ], $ ends [0 ])) {
63
+ // also get all edges in other direction
64
+ $ back = $ ends [1 ]->getEdgesTo ($ ends [0 ]);
65
+ foreach ($ back as $ edgee ) {
66
+ if (!in_array ($ edgee , $ edges )) {
67
+ $ edges [] = $ edgee ;
68
+ }
69
+ } // alternative implementation for array_unique(), because it requires casting edges to string
70
+ }
71
+
72
+ $ pos = array_search ($ edge , $ edges , true );
73
+ if ($ pos === false ) {
74
+ throw new LogicException ('Internal error: Current edge not found ' );
75
+ }
76
+
77
+ // exclude current edge from parallel edges
78
+ unset($ edges [$ pos ]);
79
+
80
+ return array_values ($ edges );
81
+ }
36
82
}
0 commit comments