forked from graphp/graph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVerticesMap.php
More file actions
67 lines (57 loc) · 1.4 KB
/
VerticesMap.php
File metadata and controls
67 lines (57 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
namespace Graphp\Graph\Set;
use Graphp\Graph\Exception\OutOfBoundsException;
use Graphp\Graph\Vertex;
/**
* A set of Vertices that are already stored in a vertex ID => Vertex instance mapping array
*
* Among others, using a mapped array significantly speeds up accessing vertices
* by ID. However, there's no way to store multiple vertices with the same ID
* (i.e. each Vertex ID has to be unique).
*/
class VerticesMap extends Vertices
{
public function getMap()
{
return $this->vertices;
}
public function getVertexId($id)
{
if (!isset($this->vertices[$id])) {
throw new OutOfBoundsException('Invalid vertex ID');
}
return $this->vertices[$id];
}
public function hasVertexId($id)
{
return isset($this->vertices[$id]);
}
public function getVerticesDistinct()
{
return $this;
}
public function getIds()
{
return array_keys($this->vertices);
}
public function getIndexVertex(Vertex $vertex)
{
$id = $vertex->getId();
if (!isset($this->vertices[$id]) || $this->vertices[$id] !== $vertex) {
throw new OutOfBoundsException();
}
return $id;
}
/**
*
* @return VerticesMap
*/
public function getVertices()
{
return $this;
}
public function hasDuplicates()
{
return false;
}
}