1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graphp\Edge\Specifics; |
4
|
|
|
|
5
|
|
|
use BadMethodCallException; |
6
|
|
|
use Graphp\GraphInterface; |
7
|
|
|
use Graphp\Edge\EdgeInterface; |
8
|
|
|
use Graphp\Edge\EdgeSet; |
9
|
|
|
use Graphp\Vertex\VertexInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class UniformEdgeSpecifics |
13
|
|
|
* |
14
|
|
|
* @package Graphp\Edge\Specifics |
15
|
|
|
*/ |
16
|
|
|
class UniformEdgeSpecifics implements EdgeSpecificsInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* The edge set |
20
|
|
|
* |
21
|
|
|
* @var EdgeSet |
22
|
|
|
*/ |
23
|
|
|
protected $edgeSet; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Construct a new specifics |
27
|
|
|
*/ |
28
|
43 |
|
public function __construct() |
29
|
|
|
{ |
30
|
43 |
|
$this->edgeSet = new EdgeSet(); |
31
|
43 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Check if the edge exists |
35
|
|
|
* |
36
|
|
|
* @param EdgeInterface $edge - the edge |
37
|
|
|
* |
38
|
|
|
* @return bool |
39
|
|
|
*/ |
40
|
31 |
|
public function containsEdge(EdgeInterface $edge): bool |
41
|
|
|
{ |
42
|
31 |
|
return $this->edgeSet->contains($edge); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get the edge set |
47
|
|
|
* |
48
|
|
|
* @return EdgeSet |
49
|
|
|
*/ |
50
|
12 |
|
public function getEdgeSet(): EdgeSet |
51
|
|
|
{ |
52
|
12 |
|
return $this->edgeSet; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Remove the edge from the set |
57
|
|
|
* |
58
|
|
|
* @param EdgeInterface $edge - the edge |
59
|
|
|
*/ |
60
|
3 |
|
public function remove(EdgeInterface $edge): void |
61
|
|
|
{ |
62
|
3 |
|
if ($this->edgeSet->contains($edge)) { |
63
|
3 |
|
$this->edgeSet->remove($edge); |
64
|
|
|
} |
65
|
3 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the edge source vertex |
69
|
|
|
* |
70
|
|
|
* @param EdgeInterface $edge - the edge |
71
|
|
|
* |
72
|
|
|
* @return VertexInterface |
73
|
|
|
*/ |
74
|
30 |
|
public function getEdgeSource(EdgeInterface $edge): VertexInterface |
75
|
|
|
{ |
76
|
30 |
|
return $edge->getSource(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the edge target vertex |
81
|
|
|
* |
82
|
|
|
* @param EdgeInterface $edge - the edge |
83
|
|
|
* |
84
|
|
|
* @return VertexInterface |
85
|
|
|
*/ |
86
|
30 |
|
public function getEdgeTarget(EdgeInterface $edge): VertexInterface |
87
|
|
|
{ |
88
|
30 |
|
return $edge->getTarget(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the edge weight |
93
|
|
|
* |
94
|
|
|
* @param EdgeInterface $edge - the edge |
95
|
|
|
* |
96
|
|
|
* @return null|float |
97
|
|
|
*/ |
98
|
1 |
|
public function getEdgeWeight(EdgeInterface $edge): ?float |
99
|
|
|
{ |
100
|
1 |
|
return GraphInterface::DEFAULT_EDGE_WEIGHT; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Set the edge weight |
105
|
|
|
* |
106
|
|
|
* @param EdgeInterface $edge - the edge |
107
|
|
|
* @param float $weight - the weight |
108
|
|
|
* |
109
|
|
|
* @throws BadMethodCallException |
110
|
|
|
*/ |
111
|
1 |
|
public function setEdgeWeight(EdgeInterface $edge, ?float $weight = null): void |
112
|
|
|
{ |
113
|
1 |
|
throw new BadMethodCallException("Method is not supported by this type of edge"); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get the edge |
118
|
|
|
* |
119
|
|
|
* @param EdgeInterface $edge - the edge |
120
|
|
|
* |
121
|
|
|
* @return null|EdgeInterface |
122
|
|
|
*/ |
123
|
1 |
|
public function getEdge(EdgeInterface $edge): ?EdgeInterface |
124
|
|
|
{ |
125
|
1 |
|
if (($id = array_search($edge, $this->edgeSet->getArrayCopy())) !== false) { |
126
|
1 |
|
return $this->edgeSet[$id]; |
127
|
|
|
} |
128
|
1 |
|
return null; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Add a new edge connecting the specified vertices |
133
|
|
|
* Return true, if edge was added |
134
|
|
|
* |
135
|
|
|
* @param EdgeInterface $edge - the edge |
136
|
|
|
* @param VertexInterface $sourceVertex - the source vertex |
137
|
|
|
* @param VertexInterface $sourceVertex - tge target vertex |
138
|
|
|
* |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
31 |
|
public function add(EdgeInterface $edge, VertexInterface $sourceVertex, VertexInterface $targetVertex): bool |
142
|
|
|
{ |
143
|
31 |
|
$edge->setSource($sourceVertex); |
144
|
31 |
|
$edge->setTarget($targetVertex); |
145
|
|
|
|
146
|
31 |
|
if (!$this->containsEdge($edge)) { |
147
|
31 |
|
$this->edgeSet[] = $edge; |
148
|
31 |
|
return true; |
149
|
|
|
} |
150
|
|
|
|
151
|
1 |
|
return false; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|