1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graphp\vertex; |
4
|
|
|
|
5
|
|
|
use ArrayObject; |
6
|
|
|
use Graphp\Edge\EdgeContainerInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class VertexMap |
10
|
|
|
* |
11
|
|
|
* @package Graphp\Vertex |
12
|
|
|
*/ |
13
|
|
|
class VertexMap extends ArrayObject |
14
|
|
|
{ |
15
|
|
|
private $vertices = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Construct a vertex map. It maps vertices to corresponding edge containers |
19
|
|
|
* |
20
|
|
|
* @param array $input - array of vertex - edge container pairs |
21
|
|
|
*/ |
22
|
42 |
|
public function __construct(array $input = []) |
23
|
|
|
{ |
24
|
42 |
|
parent::__construct($input, ArrayObject::ARRAY_AS_PROPS); |
25
|
42 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Get the edge container of the specified vertex |
29
|
|
|
* |
30
|
|
|
* @return null|EdgeContainerInterface |
31
|
|
|
*/ |
32
|
33 |
|
public function get(VertexInterface $vertex): ?EdgeContainerInterface |
33
|
|
|
{ |
34
|
33 |
|
$id = $vertex->getHash(); |
35
|
33 |
|
if (array_key_exists($id, $this->getArrayCopy())) { |
36
|
33 |
|
return $this->getArrayCopy()[$id]; |
37
|
|
|
} |
38
|
1 |
|
return null; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Put a new value to vertexmap |
43
|
|
|
* |
44
|
|
|
* @param VertexInterface $vertex - the vertex |
45
|
|
|
* @param EdgeContainerInterface $ec - the edge container |
46
|
|
|
*/ |
47
|
38 |
|
public function put(VertexInterface $vertex, ?EdgeContainerInterface $ec = null): void |
48
|
|
|
{ |
49
|
38 |
|
$id = $vertex->getHash(); |
50
|
38 |
|
parent::offsetSet($id, $ec); |
51
|
38 |
|
$this->vertices[$id] = $vertex; |
52
|
38 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Remove a vertex from the vertexmap |
56
|
|
|
* |
57
|
|
|
* @param VertexInterface $vertex - niddle |
58
|
|
|
*/ |
59
|
5 |
|
public function remove(VertexInterface $vertex): void |
60
|
|
|
{ |
61
|
5 |
|
$this->offsetUnset($vertex->getHash()); |
62
|
5 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Unset VertexMap value by key |
66
|
|
|
* |
67
|
|
|
* @param mixed $offset - array offset |
68
|
|
|
*/ |
69
|
5 |
|
public function offsetUnset($offset): void |
70
|
|
|
{ |
71
|
5 |
|
$iter = $this->getIterator(); |
72
|
5 |
|
$iter->offsetUnset($offset); |
73
|
5 |
|
unset($this->vertices[$offset]); |
74
|
5 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get array of vertices |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
38 |
|
public function keySet(): VertexSet |
82
|
|
|
{ |
83
|
38 |
|
return new VertexSet(array_values($this->vertices)); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|