66
77use drupol \phpermutations \Iterators ;
88
9- class Combinations extends Iterators
9+ final class Combinations extends Iterators
1010{
11- /**
12- * The values.
13- *
14- * @var array<int, mixed>
15- */
16- protected $ c = [];
11+ private array $ c = [];
1712
18- /**
19- * Combinations constructor.
20- *
21- * @param array<int, mixed> $dataset
22- * The dataset
23- * @param int|null $length
24- * The length
25- */
26- public function __construct (array $ dataset = [], $ length = null )
13+ public function __construct (array $ dataset = [], ?int $ length = null )
2714 {
2815 parent ::__construct (array_values ($ dataset ), $ length );
2916 $ this ->rewind ();
3017 }
3118
32- /**
33- * {@inheritdoc}
34- */
35- public function count (): int
36- {
37- $ i = 0 ;
38-
39- for ($ this ->rewind (); $ this ->valid (); $ this ->next ()) {
40- ++$ i ;
41- }
42-
43- return $ i ;
44- }
45-
46- /**
47- * {@inheritdoc}
48- */
4919 public function current (): mixed
5020 {
51- $ r = [];
52-
53- for ($ i = 0 ; $ i < $ this ->length ; ++$ i ) {
54- $ r [] = $ this ->dataset [$ this ->c [$ i ]];
55- }
56-
57- return $ r ;
21+ return array_map (
22+ fn (int $ index ): mixed => $ this ->dataset [$ this ->c [$ index ]],
23+ range (0 , $ this ->length - 1 )
24+ );
5825 }
5926
60- /**
61- * {@inheritdoc}
62- *
63- * @return void
64- */
6527 public function next (): void
6628 {
6729 if ($ this ->nextHelper ()) {
@@ -71,32 +33,18 @@ public function next(): void
7133 }
7234 }
7335
74- /**
75- * {@inheritdoc}
76- */
7736 public function rewind (): void
7837 {
7938 $ this ->c = range (0 , $ this ->length );
8039 $ this ->key = 0 ;
8140 }
8241
83- /**
84- * {@inheritdoc}
85- *
86- * @return bool
87- */
8842 public function valid (): bool
8943 {
9044 return 0 <= $ this ->key ;
9145 }
9246
93- /**
94- * Custom next() callback.
95- *
96- * @return bool
97- * Return true or false
98- */
99- protected function nextHelper (): bool
47+ private function nextHelper (): bool
10048 {
10149 $ i = $ this ->length - 1 ;
10250
0 commit comments