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