22
33namespace 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}
0 commit comments