-
-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathCartesianParameterIterator.php
More file actions
122 lines (96 loc) · 2.62 KB
/
CartesianParameterIterator.php
File metadata and controls
122 lines (96 loc) · 2.62 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/*
* This file is part of the PHPBench package
*
* (c) Daniel Leech <daniel@dantleech.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace PhpBench\Benchmark;
use ArrayIterator;
use Iterator;
use PhpBench\Model\ParameterContainer;
use PhpBench\Model\ParameterSet;
use PhpBench\Model\ParameterSetsCollection;
/**
* @implements Iterator<ParameterSet>
*/
class CartesianParameterIterator implements Iterator
{
/**
* @var array<int,ArrayIterator<string, ParameterSet>>
*/
private array $sets = [];
private int $index = 0;
private readonly int $max;
/**
* @var array<string, ParameterContainer>
*/
private array $current = [];
private bool $break = false;
private string $key;
public function __construct(ParameterSetsCollection $parameterSetsCollection)
{
foreach ($parameterSetsCollection as $parameterSets) {
$this->sets[] = $parameterSets->getIterator();
}
if (0 === $parameterSetsCollection->count()) {
$this->break = true;
}
$this->max = count($parameterSetsCollection) - 1;
}
public function current(): ParameterSet
{
return $this->getParameterSet();
}
public function next(): void
{
for ($index = 0; $index <= $this->max; $index++) {
$this->sets[$index]->next();
if (true === $this->sets[$index]->valid()) {
break;
}
$this->sets[$index]->rewind();
if ($index === $this->max) {
$this->break = true;
break;
}
}
$this->index++;
$this->update();
}
public function key(): string
{
return $this->key;
}
public function rewind(): void
{
$this->index = 0;
foreach ($this->sets as $set) {
$set->rewind();
}
$this->update();
}
public function valid(): bool
{
return false === $this->break;
}
private function update(): void
{
$this->current = [];
$key = [];
foreach ($this->sets as $set) {
/** @var ParameterSet|null $current */
$current = $set->current();
$this->current = array_merge($this->current, $current ? $current->toArray() : []);
$key[] = $set->key();
}
$this->key = implode(',', $key);
}
private function getParameterSet(): ParameterSet
{
return ParameterSet::fromParameterContainers($this->key, $this->current);
}
}