|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace drupol\phpermutations; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class Permutations. |
|
7
|
|
|
* |
|
8
|
|
|
* @package drupol\phpermutations |
|
9
|
|
|
*/ |
|
10
|
|
|
class Permutations extends Combinatorics { |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Permutations constructor. |
|
14
|
|
|
* |
|
15
|
|
|
* @param array $dataset |
|
16
|
|
|
* @param int $length |
|
|
|
|
|
|
17
|
|
|
*/ |
|
18
|
3 |
|
public function __construct(array $dataset = array(), $length = NULL) { |
|
|
|
|
|
|
19
|
3 |
|
parent::__construct($dataset, $length); |
|
20
|
3 |
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @return int |
|
24
|
|
|
*/ |
|
25
|
3 |
|
public function count() { |
|
26
|
3 |
|
return $this->fact(count($this->getDataset())) / $this->fact(count($this->getDataset()) - $this->getLength()); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @return \Generator |
|
|
|
|
|
|
31
|
|
|
*/ |
|
32
|
3 |
|
public function generator() { |
|
33
|
3 |
|
$combinations = new Combinations($this->getDataset(), $this->getLength()); |
|
34
|
3 |
|
$result = array(); |
|
35
|
|
|
|
|
36
|
3 |
|
foreach ($combinations->toArray() as $subset) { |
|
37
|
3 |
|
$set = $subset; |
|
38
|
3 |
|
$perms = array(); |
|
39
|
3 |
|
$size = count($subset) - 1; |
|
40
|
3 |
|
$perm = range(0, $size); |
|
41
|
3 |
|
$j = 0; |
|
42
|
|
|
|
|
43
|
|
|
do { |
|
44
|
3 |
|
foreach ($perm as $i) { |
|
45
|
3 |
|
$perms[$j][] = $set[$i]; |
|
46
|
3 |
|
} |
|
47
|
3 |
|
} while ($perm = $this->permute($perm, $size) and ++$j); |
|
48
|
|
|
|
|
49
|
3 |
|
$result = array_merge($result, $perms); |
|
50
|
3 |
|
} |
|
51
|
|
|
|
|
52
|
3 |
|
return $result; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param $items |
|
57
|
|
|
* @param array $perms |
|
|
|
|
|
|
58
|
|
|
* |
|
59
|
|
|
* @return array |
|
|
|
|
|
|
60
|
|
|
*/ |
|
61
|
3 |
|
protected function permute($p, $size) { |
|
|
|
|
|
|
62
|
|
|
// slide down the array looking for where we're smaller than the next guy |
|
63
|
3 |
|
for ($i = $size - 1; $i >= 0 && $p[$i] >= $p[$i+1]; --$i) { } |
|
64
|
|
|
|
|
65
|
|
|
// if this doesn't occur, we've finished our permutations |
|
66
|
|
|
// the array is reversed: (1, 2, 3, 4) => (4, 3, 2, 1) |
|
67
|
3 |
|
if ($i == -1) { |
|
68
|
3 |
|
return false; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
// slide down the array looking for a bigger number than what we found before |
|
72
|
3 |
|
for ($j = $size; $p[$j] <= $p[$i]; --$j) { } |
|
73
|
|
|
|
|
74
|
|
|
// swap them |
|
75
|
3 |
|
$tmp = $p[$i]; $p[$i] = $p[$j]; $p[$j] = $tmp; |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
// now reverse the elements in between by swapping the ends |
|
78
|
3 |
|
for (++$i, $j = $size; $i < $j; ++$i, --$j) { |
|
79
|
3 |
|
$tmp = $p[$i]; $p[$i] = $p[$j]; $p[$j] = $tmp; |
|
|
|
|
|
|
80
|
3 |
|
} |
|
81
|
|
|
|
|
82
|
3 |
|
return $p; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return array |
|
87
|
|
|
*/ |
|
88
|
3 |
|
public function toArray() { |
|
89
|
3 |
|
$results = array(); |
|
90
|
|
|
|
|
91
|
3 |
|
foreach ($this->generator() as $value) { |
|
92
|
3 |
|
$results[] = $value; |
|
93
|
3 |
|
} |
|
94
|
|
|
|
|
95
|
3 |
|
return $results; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.