1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Smoren\GraphTools\Structs; |
4
|
|
|
|
5
|
|
|
use Smoren\GraphTools\Models\Interfaces\EdgeInterface; |
6
|
|
|
use Smoren\GraphTools\Models\Interfaces\VertexInterface; |
7
|
|
|
use Smoren\GraphTools\Structs\Interfaces\TraverseBranchContextInterface; |
8
|
|
|
use Smoren\GraphTools\Structs\Interfaces\TraverseContextInterface; |
9
|
|
|
|
10
|
|
|
class TraverseContext implements TraverseContextInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var VertexInterface |
14
|
|
|
*/ |
15
|
|
|
protected VertexInterface $vertex; |
16
|
|
|
/** |
17
|
|
|
* @var EdgeInterface|null |
18
|
|
|
*/ |
19
|
|
|
protected ?EdgeInterface $edge; |
20
|
|
|
/** |
21
|
|
|
* @var TraverseBranchContextInterface |
22
|
|
|
*/ |
23
|
|
|
protected TraverseBranchContextInterface $branchContext; |
24
|
|
|
/** |
25
|
|
|
* @var array<string, VertexInterface> |
26
|
|
|
*/ |
27
|
|
|
protected array $passedVertexesMap; |
28
|
|
|
/** |
29
|
|
|
* @var array<string, VertexInterface> |
30
|
|
|
*/ |
31
|
|
|
protected array $globalPassedVertexesMap; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param VertexInterface $vertex |
35
|
|
|
* @param EdgeInterface|null $edge |
36
|
|
|
* @param TraverseBranchContextInterface $branchContext |
37
|
|
|
* @param array<string, VertexInterface> $passedVertexesMap |
38
|
|
|
* @param array<string, VertexInterface> $globalPassedVertexesMap |
39
|
|
|
*/ |
40
|
|
|
public function __construct( |
41
|
|
|
VertexInterface $vertex, |
42
|
|
|
?EdgeInterface $edge, |
43
|
|
|
TraverseBranchContextInterface $branchContext, |
44
|
|
|
array $passedVertexesMap, |
45
|
|
|
array &$globalPassedVertexesMap |
46
|
|
|
) { |
47
|
|
|
$this->vertex = $vertex; |
48
|
|
|
$this->edge = $edge; |
49
|
|
|
$this->branchContext = $branchContext; |
50
|
|
|
$this->passedVertexesMap = $passedVertexesMap; |
51
|
|
|
$this->globalPassedVertexesMap = &$globalPassedVertexesMap; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritDoc |
56
|
|
|
*/ |
57
|
|
|
public function getVertex(): VertexInterface |
58
|
|
|
{ |
59
|
|
|
return $this->vertex; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return VertexInterface|null |
64
|
|
|
*/ |
65
|
|
|
public function getPrevVertex(): ?VertexInterface |
66
|
|
|
{ |
67
|
|
|
$candidate = end($this->passedVertexesMap); |
68
|
|
|
return $candidate ?: null; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return EdgeInterface|null |
73
|
|
|
*/ |
74
|
|
|
public function getEdge(): ?EdgeInterface |
75
|
|
|
{ |
76
|
|
|
return $this->edge; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritDoc |
81
|
|
|
*/ |
82
|
|
|
public function getBranchContext(): TraverseBranchContextInterface |
83
|
|
|
{ |
84
|
|
|
return $this->branchContext; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @inheritDoc |
89
|
|
|
*/ |
90
|
|
|
public function getPassedVertexesMap(): array |
91
|
|
|
{ |
92
|
|
|
return $this->passedVertexesMap; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @inheritDoc |
97
|
|
|
*/ |
98
|
|
|
public function getGlobalPassedVertexesMap(): array |
99
|
|
|
{ |
100
|
|
|
return $this->globalPassedVertexesMap; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @inheritDoc |
105
|
|
|
*/ |
106
|
|
|
public function isLoop(): bool |
107
|
|
|
{ |
108
|
|
|
return isset($this->passedVertexesMap[$this->vertex->getId()]); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|