1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace drupol\phpermutations; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Combinatorics. |
7
|
|
|
*/ |
8
|
|
|
abstract class Combinatorics implements \Countable |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The dataset. |
12
|
|
|
* |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
protected $dataset; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The number of values in the dataset. |
19
|
|
|
* |
20
|
|
|
* @var int |
21
|
|
|
*/ |
22
|
|
|
protected $datasetCount; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The length. |
26
|
|
|
* |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
protected $length; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Combinatorics constructor. |
33
|
|
|
* |
34
|
|
|
* @param array $dataset |
35
|
|
|
* The dataset |
36
|
|
|
* @param int|null $length |
37
|
|
|
* The length |
38
|
|
|
*/ |
39
|
52 |
|
public function __construct(array $dataset = [], $length = null) |
40
|
|
|
{ |
41
|
52 |
|
$this->setDataset($dataset); |
42
|
52 |
|
$this->datasetCount = count($this->dataset); |
43
|
52 |
|
$this->setLength($length); |
44
|
52 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Set the length. |
48
|
|
|
* |
49
|
|
|
* @param int $length |
50
|
|
|
* The length |
51
|
|
|
* |
52
|
|
|
* @return $this |
53
|
|
|
*/ |
54
|
40 |
View Code Duplication |
public function setLength($length = null) |
|
|
|
|
55
|
|
|
{ |
56
|
40 |
|
$length = (null === $length) ? $this->datasetCount : $length; |
57
|
40 |
|
$this->length = ($length > $this->datasetCount) ? $this->datasetCount : $length; |
58
|
|
|
|
59
|
40 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get the length. |
64
|
|
|
* |
65
|
|
|
* @return int |
66
|
|
|
* The length |
67
|
|
|
*/ |
68
|
14 |
|
public function getLength() |
69
|
|
|
{ |
70
|
14 |
|
return (int) $this->length; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set the dataset. |
75
|
|
|
* |
76
|
|
|
* @param array $dataset |
77
|
|
|
* The dataset |
78
|
|
|
* |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
52 |
|
public function setDataset(array $dataset = []) |
82
|
|
|
{ |
83
|
52 |
|
$this->dataset = $dataset; |
84
|
|
|
|
85
|
52 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the dataset. |
90
|
|
|
* |
91
|
|
|
* @return mixed[] |
92
|
|
|
* The dataset |
93
|
|
|
*/ |
94
|
34 |
|
public function getDataset() |
95
|
|
|
{ |
96
|
34 |
|
return $this->dataset; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Count elements of an object. |
101
|
|
|
* |
102
|
|
|
* @return int |
103
|
|
|
* The number of element |
104
|
|
|
*/ |
105
|
10 |
|
public function count() |
106
|
|
|
{ |
107
|
10 |
|
return count($this->toArray()); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Convert the iterator into an array. |
112
|
|
|
* |
113
|
|
|
* @return array |
114
|
|
|
* The elements |
115
|
|
|
*/ |
116
|
22 |
View Code Duplication |
public function toArray() |
|
|
|
|
117
|
|
|
{ |
118
|
22 |
|
$data = []; |
119
|
|
|
|
120
|
22 |
|
for ($this->rewind(); $this->valid(); $this->next()) { |
|
|
|
|
121
|
22 |
|
$data[] = $this->current(); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
22 |
|
return $data; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
16 |
|
public function key() |
|
|
|
|
131
|
|
|
{ |
132
|
16 |
|
return $this->key; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Compute the factorial of an integer. |
137
|
|
|
* |
138
|
|
|
* @param int $n |
139
|
|
|
* The number to get its factorial |
140
|
|
|
* @param int $total |
141
|
|
|
* The total |
142
|
|
|
* |
143
|
|
|
* @return int |
144
|
|
|
* The factorial of $n |
145
|
|
|
*/ |
146
|
10 |
|
protected function fact($n, $total = 1) |
|
|
|
|
147
|
|
|
{ |
148
|
10 |
|
return ($n < 2) ? $total : $this->fact($n - 1, $total * $n); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.