|
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
|
5 |
|
public function __construct(array $input = []) |
|
23
|
|
|
{ |
|
24
|
5 |
|
parent::__construct($input, ArrayObject::ARRAY_AS_PROPS); |
|
25
|
5 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Get the edge container of the specified vertex |
|
29
|
|
|
* |
|
30
|
|
|
* @return null|EdgeContainerInterface |
|
31
|
|
|
*/ |
|
32
|
2 |
|
public function get(VertexInterface $vertex): ?EdgeContainerInterface |
|
33
|
|
|
{ |
|
34
|
2 |
|
$id = $vertex->getHash(); |
|
35
|
2 |
|
if (array_key_exists($id, $this->getArrayCopy())) { |
|
36
|
2 |
|
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
|
3 |
|
public function put(VertexInterface $vertex, ?EdgeContainerInterface $ec = null): void |
|
48
|
|
|
{ |
|
49
|
3 |
|
$id = $vertex->getHash(); |
|
50
|
3 |
|
parent::offsetSet($id, $ec); |
|
51
|
3 |
|
$this->vertices[$id] = $vertex; |
|
52
|
3 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Remove a vertex from the vertexmap |
|
56
|
|
|
* |
|
57
|
|
|
* @param VertexInterface $vertex - niddle |
|
58
|
|
|
*/ |
|
59
|
|
|
public function remove(VertexInterface $vertex): void |
|
60
|
|
|
{ |
|
61
|
|
|
$this->offsetUnset($vertex->getHash()); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Unset VertexMap value by key |
|
66
|
|
|
* |
|
67
|
|
|
* @param mixed $offset - array offset |
|
68
|
|
|
*/ |
|
69
|
|
|
public function offsetUnset($offset): void |
|
70
|
3 |
|
{ |
|
71
|
|
|
$iter = $this->getIterator(); |
|
72
|
3 |
|
$iter->offsetUnset($offset); |
|
73
|
|
|
unset($this->vertices[$offset]); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Get array of vertices |
|
78
|
|
|
* |
|
79
|
|
|
* @return array |
|
80
|
|
|
*/ |
|
81
|
|
|
public function keySet(): VertexSet |
|
82
|
|
|
{ |
|
83
|
|
|
return new VertexSet(array_values($this->vertices)); |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|