1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace drupol\phpermutations\Iterators; |
4
|
|
|
|
5
|
|
|
use drupol\phpermutations\Combinatorics; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Combinations. |
9
|
|
|
* |
10
|
|
|
* @package drupol\phpermutations\Iterators |
11
|
|
|
*/ |
12
|
|
|
class Combinations extends Combinatorics implements \Iterator { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The values. |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $c = array(); |
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The key. |
23
|
|
|
* |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
protected $key = 0; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Combinations constructor. |
30
|
|
|
* |
31
|
|
|
* @param array $dataset |
32
|
|
|
* The dataset. |
33
|
|
|
* @param int|null $length |
34
|
|
|
* The length. |
35
|
|
|
*/ |
36
|
6 |
|
public function __construct(array $dataset = array(), $length = NULL) { |
37
|
6 |
|
parent::__construct(array_values($dataset), $length); |
38
|
6 |
|
$this->rewind(); |
39
|
6 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function key() { |
45
|
|
|
return $this->key; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
3 |
|
public function current() { |
52
|
3 |
|
$r = array(); |
|
|
|
|
53
|
3 |
|
for ($i = 0; $i < $this->length; $i++) { |
54
|
3 |
|
$r[] = $this->dataset[$this->c[$i]]; |
55
|
3 |
|
} |
56
|
3 |
|
return $r; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
3 |
|
public function next() { |
63
|
3 |
|
if ($this->nextHelper()) { |
64
|
2 |
|
$this->key++; |
65
|
2 |
|
} |
66
|
|
|
else { |
67
|
3 |
|
$this->key = -1; |
68
|
|
|
} |
69
|
3 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
6 |
|
public function rewind() { |
75
|
6 |
|
$this->c = range(0, $this->length); |
76
|
6 |
|
$this->key = 0; |
77
|
6 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
3 |
|
public function valid() { |
83
|
3 |
|
return $this->key >= 0; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Custom next() callback. |
88
|
|
|
* |
89
|
|
|
* @return bool |
90
|
|
|
* Return true or false. |
91
|
|
|
*/ |
92
|
3 |
|
protected function nextHelper() { |
93
|
3 |
|
$i = $this->length - 1; |
94
|
3 |
|
while ($i >= 0 && $this->c[$i] == $this->datasetCount - $this->length + $i) { |
95
|
3 |
|
$i--; |
96
|
3 |
|
} |
97
|
3 |
|
if ($i < 0) { |
98
|
3 |
|
return FALSE; |
99
|
|
|
} |
100
|
2 |
|
$this->c[$i]++; |
101
|
2 |
|
while ($i++ < $this->length - 1) { |
102
|
2 |
|
$this->c[$i] = $this->c[$i - 1] + 1; |
103
|
2 |
|
} |
104
|
|
|
|
105
|
2 |
|
return TRUE; |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Convert the iterator into an array. |
110
|
|
|
* |
111
|
|
|
* @return array |
112
|
|
|
* The elements. |
113
|
|
|
*/ |
114
|
3 |
View Code Duplication |
public function toArray() { |
|
|
|
|
115
|
3 |
|
$data = []; |
116
|
|
|
|
117
|
3 |
|
for ($this->rewind(); $this->valid(); $this->next()) { |
118
|
3 |
|
$data[] = $this->current(); |
119
|
3 |
|
} |
120
|
|
|
|
121
|
3 |
|
return $data; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
6 |
|
public function count() { |
128
|
6 |
|
return $this->fact(count($this->getDataset())) / ($this->fact($this->getLength()) * $this->fact(count($this->getDataset()) - $this->getLength())); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.