1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace drupol\phpermutations\Iterators; |
4
|
|
|
|
5
|
|
|
use drupol\phpermutations\Combinatorics; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Product. |
9
|
|
|
* |
10
|
|
|
* @package drupol\phpermutations\Iterators |
11
|
|
|
*/ |
12
|
|
|
class Product extends Combinatorics implements \Iterator { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var int |
16
|
|
|
*/ |
17
|
|
|
protected $key; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var \Iterator[] |
21
|
|
|
*/ |
22
|
|
|
protected $iterators = array(); |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Product constructor. |
26
|
|
|
* |
27
|
|
|
* @param array $datasetArray |
28
|
|
|
* @param int|null $length |
29
|
|
|
*/ |
30
|
4 |
|
public function __construct(array $datasetArray, $length = NULL) { |
|
|
|
|
31
|
4 |
|
parent::__construct($datasetArray); |
32
|
|
|
|
33
|
4 |
|
foreach ($datasetArray as $dataset) { |
34
|
4 |
|
$this->iterators[] = new \ArrayIterator($dataset); |
35
|
|
|
} |
36
|
|
|
|
37
|
4 |
|
$this->key = 0; |
38
|
4 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritdoc |
42
|
|
|
*/ |
43
|
4 |
|
public function current() { |
44
|
4 |
|
$tuple = array(); |
45
|
|
|
|
46
|
4 |
|
foreach ($this->iterators as $iterator) { |
47
|
4 |
|
$tuple[] = $iterator->current(); |
48
|
|
|
} |
49
|
|
|
|
50
|
4 |
|
return $tuple; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritdoc |
55
|
|
|
*/ |
56
|
4 |
|
public function next() { |
57
|
|
|
/** @var $reverseIterators \Iterator[] */ |
58
|
4 |
|
$reverseIterators = array_reverse($this->iterators); |
59
|
|
|
|
60
|
4 |
|
foreach ($reverseIterators as $key => $iterator) { |
61
|
4 |
|
$iterator->next(); |
62
|
4 |
|
if ($iterator->valid()) { |
63
|
4 |
|
foreach ($this->iterators as $key2 => $iterator2) { |
64
|
4 |
|
if ($key >= count($this->iterators) - $key2) { |
65
|
4 |
|
$iterator2->rewind(); |
66
|
|
|
} |
67
|
|
|
} |
68
|
4 |
|
break; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
4 |
|
$this->key++; |
73
|
4 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritdoc |
77
|
|
|
*/ |
78
|
|
|
public function key() { |
79
|
|
|
return $this->key; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritdoc |
84
|
|
|
*/ |
85
|
4 |
|
public function valid() { |
86
|
4 |
|
$isUnlessOneValid = FALSE; |
|
|
|
|
87
|
|
|
|
88
|
4 |
|
foreach ($this->iterators as $iterator) { |
89
|
4 |
|
if ($iterator->valid()) { |
90
|
4 |
|
$isUnlessOneValid = TRUE; |
|
|
|
|
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
4 |
|
return $isUnlessOneValid; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @inheritdoc |
99
|
|
|
*/ |
100
|
4 |
|
public function rewind() { |
101
|
4 |
|
foreach ($this->iterators as $iterator) { |
102
|
4 |
|
$iterator->rewind(); |
103
|
|
|
} |
104
|
|
|
|
105
|
4 |
|
$this->key = 0; |
106
|
4 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return int |
110
|
|
|
*/ |
111
|
4 |
|
public function count() { |
112
|
4 |
|
$product = 1; |
113
|
|
|
|
114
|
4 |
|
foreach ($this->getDataset() as $dataset) { |
115
|
4 |
|
$product *= count($dataset); |
116
|
|
|
} |
117
|
|
|
|
118
|
4 |
|
return $product; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
4 |
View Code Duplication |
public function toArray() { |
|
|
|
|
125
|
4 |
|
$data = array(); |
126
|
|
|
|
127
|
4 |
|
for ($this->rewind(); $this->valid(); $this->next()) { |
128
|
4 |
|
$data[] = $this->current(); |
129
|
|
|
} |
130
|
|
|
|
131
|
4 |
|
return $data; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|