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