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\TraverseStepIteratorInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class EdgeVertexIterator |
11
|
|
|
* @author Smoren <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class TraverseStepIterator implements TraverseStepIteratorInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array<TraverseStepItem> source to iterate |
17
|
|
|
*/ |
18
|
|
|
protected array $source; |
19
|
|
|
/** |
20
|
|
|
* @var int iteration pointer |
21
|
|
|
*/ |
22
|
|
|
protected int $index = 0; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @inheritDoc |
26
|
|
|
*/ |
27
|
|
|
public static function combine(TraverseStepIteratorInterface ...$iterators): TraverseStepIteratorInterface |
28
|
|
|
{ |
29
|
|
|
$source = []; |
30
|
|
|
foreach($iterators as $iterator) { |
31
|
|
|
foreach($iterator as $edge => $vertex) { |
32
|
|
|
$source[] = new TraverseStepItem($edge, $vertex); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
return new TraverseStepIterator($source); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* EdgeVertexIterator constructor. |
40
|
|
|
* @param array<TraverseStepItem> $source |
41
|
|
|
*/ |
42
|
|
|
public function __construct(array $source) |
43
|
|
|
{ |
44
|
|
|
$this->source = $source; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritDoc |
49
|
|
|
*/ |
50
|
|
|
public function current(): VertexInterface |
51
|
|
|
{ |
52
|
|
|
return $this->source[$this->index]->getVertex(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @inheritDoc |
57
|
|
|
*/ |
58
|
|
|
public function next(): void |
59
|
|
|
{ |
60
|
|
|
$this->index++; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @inheritDoc |
65
|
|
|
*/ |
66
|
|
|
public function key(): ?EdgeInterface |
67
|
|
|
{ |
68
|
|
|
return $this->source[$this->index]->getEdge(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @inheritDoc |
73
|
|
|
*/ |
74
|
|
|
public function valid(): bool |
75
|
|
|
{ |
76
|
|
|
return $this->index < $this->count(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritDoc |
81
|
|
|
*/ |
82
|
|
|
public function rewind(): void |
83
|
|
|
{ |
84
|
|
|
$this->index = 0; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @inheritDoc |
89
|
|
|
*/ |
90
|
|
|
public function count(): int |
91
|
|
|
{ |
92
|
|
|
return count($this->source); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|