Skip to content

Commit 2ef9a2c

Browse files
Pol DellaieraPol Dellaiera
Pol Dellaiera
authored and
Pol Dellaiera
committed
#4: Convert the Permutations object into a PHP Generator.
1 parent 7039ca1 commit 2ef9a2c

File tree

3 files changed

+70
-408
lines changed

3 files changed

+70
-408
lines changed

Diff for: src/Generators/Permutations.php

+70-7
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,36 @@
22

33
namespace drupol\phpermutations\Generators;
44

5-
use drupol\phpermutations\Permutations as PermutationsClass;
5+
use drupol\phpermutations\Combinatorics;
66

77
/**
88
* Class Permutations.
99
*
1010
* @package drupol\phpermutations\Generators
1111
*
12-
* @author Mark Wilson <[email protected]>
12+
* Inspired by the work of Mark Wilson <[email protected]>
1313
*/
14-
class Permutations extends PermutationsClass {
14+
class Permutations extends Combinatorics {
15+
16+
/**
17+
* The combinations generator.
18+
*
19+
* @var \drupol\phpermutations\Generators\Combinations
20+
*/
21+
public $combinations;
22+
23+
/**
24+
* Combinatorics constructor.
25+
*
26+
* @param array $dataset
27+
* The dataset.
28+
* @param int|null $length
29+
* The length.
30+
*/
31+
public function __construct(array $dataset = array(), $length = NULL) {
32+
parent::__construct($dataset, $length);
33+
$this->combinations = new Combinations($dataset, $this->getLength());
34+
}
1535

1636
/**
1737
* Alias of the get() method.
@@ -20,11 +40,31 @@ class Permutations extends PermutationsClass {
2040
* The prime generator.
2141
*/
2242
public function generator() {
23-
return $this->get($this->getDataset());
43+
return $this->get($this->getDataset(), $this->getLength());
44+
}
45+
46+
/**
47+
* The combination generator.
48+
*
49+
* @param array $dataset
50+
* The dataset.
51+
* @param int $length
52+
* The length.
53+
*
54+
* @codingStandardsIgnoreStart
55+
* @return \Generator
56+
* @codingStandardsIgnoreEnd
57+
*/
58+
protected function get(array $dataset, $length) {
59+
foreach ($this->combinations->generator() as $combination) {
60+
foreach ($this->getPermutations($combination) as $current) {
61+
yield $current;
62+
}
63+
}
2464
}
2565

2666
/**
27-
* The generator.
67+
* The permutations generator.
2868
*
2969
* @param array $dataset
3070
* The dataset.
@@ -33,19 +73,42 @@ public function generator() {
3373
* @return \Generator
3474
* @codingStandardsIgnoreEnd
3575
*/
36-
protected function get(array $dataset) {
76+
protected function getPermutations(array $dataset) {
3777
foreach ($dataset as $key => $firstItem) {
3878
$remaining = $dataset;
3979
array_splice($remaining, $key, 1);
4080
if (count($remaining) === 0) {
4181
yield [$firstItem];
4282
continue;
4383
}
44-
foreach ($this->get($remaining) as $permutation) {
84+
foreach ($this->getPermutations($remaining) as $permutation) {
4585
array_unshift($permutation, $firstItem);
4686
yield $permutation;
4787
}
4888
}
4989
}
5090

91+
/**
92+
* Convert the generator into an array.
93+
*
94+
* @return array
95+
* The elements.
96+
*/
97+
public function toArray() {
98+
$data = array();
99+
100+
foreach ($this->generator() as $value) {
101+
$data[] = $value;
102+
}
103+
104+
return $data;
105+
}
106+
107+
/**
108+
* {@inheritdoc}
109+
*/
110+
public function count() {
111+
return $this->combinations->count() * $this->fact($this->getLength());
112+
}
113+
51114
}

Diff for: src/Permutations.php

-119
This file was deleted.

0 commit comments

Comments
 (0)