2
2
3
3
namespace drupol \phpermutations \Generators ;
4
4
5
- use drupol \phpermutations \Permutations as PermutationsClass ;
5
+ use drupol \phpermutations \Combinatorics ;
6
6
7
7
/**
8
8
* Class Permutations.
9
9
*
10
10
* @package drupol\phpermutations\Generators
11
11
*
12
- * @author Mark Wilson <[email protected] >
12
+ * Inspired by the work of Mark Wilson <[email protected] >
13
13
*/
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
+ }
15
35
16
36
/**
17
37
* Alias of the get() method.
@@ -20,11 +40,31 @@ class Permutations extends PermutationsClass {
20
40
* The prime generator.
21
41
*/
22
42
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
+ }
24
64
}
25
65
26
66
/**
27
- * The generator.
67
+ * The permutations generator.
28
68
*
29
69
* @param array $dataset
30
70
* The dataset.
@@ -33,19 +73,42 @@ public function generator() {
33
73
* @return \Generator
34
74
* @codingStandardsIgnoreEnd
35
75
*/
36
- protected function get (array $ dataset ) {
76
+ protected function getPermutations (array $ dataset ) {
37
77
foreach ($ dataset as $ key => $ firstItem ) {
38
78
$ remaining = $ dataset ;
39
79
array_splice ($ remaining , $ key , 1 );
40
80
if (count ($ remaining ) === 0 ) {
41
81
yield [$ firstItem ];
42
82
continue ;
43
83
}
44
- foreach ($ this ->get ($ remaining ) as $ permutation ) {
84
+ foreach ($ this ->getPermutations ($ remaining ) as $ permutation ) {
45
85
array_unshift ($ permutation , $ firstItem );
46
86
yield $ permutation ;
47
87
}
48
88
}
49
89
}
50
90
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
+
51
114
}
0 commit comments