1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the bisarca/graph package. |
5
|
|
|
* |
6
|
|
|
* (c) Emanuele Minotto <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Bisarca\Graph\Vertex; |
13
|
|
|
|
14
|
|
|
use Bisarca\Graph\AbstractSet; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Set of vertices. |
18
|
|
|
*/ |
19
|
|
View Code Duplication |
class Set extends AbstractSet |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Sets the optional contained vertices. |
23
|
|
|
* |
24
|
|
|
* @param VertexInterface[] $vertices |
25
|
|
|
*/ |
26
|
|
|
public function __construct(VertexInterface ...$vertices) |
27
|
|
|
{ |
28
|
|
|
$this->data = $vertices; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Sets the contained vertices. |
33
|
|
|
* |
34
|
|
|
* @param VertexInterface[] $vertices |
35
|
|
|
*/ |
36
|
|
|
public function set(VertexInterface ...$vertices) |
37
|
|
|
{ |
38
|
|
|
$this->data = $vertices; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Adds some vertices. |
43
|
|
|
* |
44
|
|
|
* @param VertexInterface[] $vertices |
45
|
|
|
*/ |
46
|
|
|
public function add(VertexInterface ...$vertices) |
47
|
|
|
{ |
48
|
|
|
$this->data = array_merge($this->data, $vertices); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Checks if all the vertices are contained. |
53
|
|
|
* |
54
|
|
|
* @param VertexInterface[] $vertices |
55
|
|
|
* |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
|
|
public function has(VertexInterface ...$vertices): bool |
59
|
|
|
{ |
60
|
|
|
$intersection = array_uintersect( |
61
|
|
|
$this->data, |
62
|
|
|
$vertices, |
63
|
|
|
function (VertexInterface $a, VertexInterface $b) { |
64
|
|
|
return $a !== $b; |
65
|
|
|
} |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
return count($intersection) === count($vertices); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Removes some vertices. |
73
|
|
|
* |
74
|
|
|
* @param VertexInterface[] $vertices |
75
|
|
|
*/ |
76
|
|
|
public function remove(VertexInterface ...$vertices) |
77
|
|
|
{ |
78
|
|
|
$this->data = array_udiff( |
79
|
|
|
$this->data, |
80
|
|
|
$vertices, |
81
|
|
|
function (VertexInterface $a, VertexInterface $b) { |
82
|
|
|
return $a !== $b; |
83
|
|
|
} |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.