1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace drupol\phpermutations; |
6
|
|
|
|
7
|
|
|
use function count; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Combinatorics. |
11
|
|
|
*/ |
12
|
|
|
abstract class Combinatorics |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The dataset. |
16
|
|
|
* |
17
|
|
|
* @var array<int, mixed> |
18
|
|
|
*/ |
19
|
|
|
protected $dataset; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The number of values in the dataset. |
23
|
|
|
* |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
protected $datasetCount; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The length. |
30
|
|
|
* |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
protected $length; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Combinatorics constructor. |
37
|
|
|
* |
38
|
|
|
* @param array<int, mixed> $dataset |
39
|
53 |
|
* The dataset |
40
|
|
|
* @param int|null $length |
41
|
53 |
|
* The length |
42
|
53 |
|
*/ |
43
|
53 |
|
public function __construct(array $dataset = [], $length = null) |
44
|
53 |
|
{ |
45
|
|
|
$this->setDataset($dataset); |
46
|
|
|
$this->datasetCount = count($this->dataset); |
47
|
|
|
$this->setLength($length); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Count elements of an object. |
52
|
|
|
* |
53
|
|
|
* @return int |
54
|
41 |
|
* The number of element |
55
|
|
|
*/ |
56
|
41 |
|
public function count() |
57
|
41 |
|
{ |
58
|
|
|
return count($this->toArray()); |
59
|
41 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the dataset. |
63
|
|
|
* |
64
|
|
|
* @return mixed[] |
65
|
|
|
* The dataset |
66
|
|
|
*/ |
67
|
|
|
public function getDataset() |
68
|
14 |
|
{ |
69
|
|
|
return $this->dataset; |
70
|
14 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get the length. |
74
|
|
|
* |
75
|
|
|
* @return int |
76
|
|
|
* The length |
77
|
|
|
*/ |
78
|
|
|
public function getLength() |
79
|
|
|
{ |
80
|
|
|
return (int) $this->length; |
81
|
53 |
|
} |
82
|
|
|
|
83
|
53 |
|
/** |
84
|
|
|
* Set the dataset. |
85
|
53 |
|
* |
86
|
|
|
* @param array<int, mixed> $dataset |
87
|
|
|
* The dataset |
88
|
|
|
* |
89
|
|
|
* @return $this |
90
|
|
|
*/ |
91
|
|
|
public function setDataset(array $dataset = []) |
92
|
|
|
{ |
93
|
|
|
$this->dataset = $dataset; |
94
|
34 |
|
|
95
|
|
|
return $this; |
96
|
34 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Set the length. |
100
|
|
|
* |
101
|
|
|
* @param int|null $length |
102
|
|
|
* The length |
103
|
|
|
* |
104
|
|
|
* @return $this |
105
|
10 |
|
*/ |
106
|
|
|
public function setLength($length = null) |
107
|
10 |
|
{ |
108
|
|
|
$length = $length ?? $this->datasetCount; |
109
|
|
|
$this->length = (abs($length) > $this->datasetCount) ? $this->datasetCount : $length; |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return array<int, mixed> |
116
|
23 |
|
*/ |
117
|
|
|
public function toArray() |
118
|
23 |
|
{ |
119
|
|
|
return []; |
120
|
23 |
|
} |
121
|
23 |
|
|
122
|
|
|
/** |
123
|
|
|
* Compute the factorial of an integer. |
124
|
23 |
|
* |
125
|
|
|
* @param int $n |
126
|
|
|
* The number to get its factorial |
127
|
|
|
* @param int $total |
128
|
|
|
* The total |
129
|
|
|
* |
130
|
16 |
|
* @return int |
131
|
|
|
* The factorial of $n |
132
|
16 |
|
*/ |
133
|
|
|
protected function fact($n, $total = 1) |
134
|
|
|
{ |
135
|
|
|
return (2 > $n) ? $total : $this->fact($n - 1, $total * $n); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|