|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PHP\Math\Vector; |
|
4
|
|
|
|
|
5
|
|
|
use ArrayObject; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
use Iterator; |
|
8
|
|
|
use PHP\Math\BigNumber\BigNumber; |
|
9
|
|
|
|
|
10
|
|
|
class Tuple extends ArrayObject |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Initializes a new instance of this class. |
|
14
|
|
|
* |
|
15
|
|
|
* @param array|Iterator $components The components to set. |
|
16
|
|
|
*/ |
|
17
|
44 |
|
public function __construct($components = array()) |
|
18
|
|
|
{ |
|
19
|
44 |
|
parent::__construct(array()); |
|
20
|
|
|
|
|
21
|
44 |
|
$index = 0; |
|
22
|
|
|
|
|
23
|
44 |
|
foreach ($components as $component) { |
|
24
|
34 |
|
$this->setElement($index++, $component); |
|
25
|
44 |
|
} |
|
26
|
44 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Adds an element to the tuple. |
|
30
|
|
|
* |
|
31
|
|
|
* @param float|BigNumber $value The value to add. |
|
32
|
|
|
* @return int Returns the index of the element that was added. |
|
33
|
|
|
*/ |
|
34
|
1 |
|
public function addElement($value) |
|
35
|
|
|
{ |
|
36
|
1 |
|
$index = $this->getSize(); |
|
37
|
|
|
|
|
38
|
1 |
|
$this->setElement($index, $value); |
|
39
|
|
|
|
|
40
|
1 |
|
return $index; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Gets the element located at the given index. |
|
45
|
|
|
* |
|
46
|
|
|
* @param int $index The index of the element to get. |
|
47
|
|
|
* @return BigNumber |
|
48
|
|
|
*/ |
|
49
|
20 |
|
public function getElement($index) |
|
50
|
|
|
{ |
|
51
|
20 |
|
$this->validateIndex($index, true); |
|
52
|
|
|
|
|
53
|
16 |
|
return $this[$index]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Removes the element located at the given index. |
|
58
|
|
|
* |
|
59
|
|
|
* @param int $index The index of the element to remove. |
|
60
|
|
|
* @return null|BigNumber Returns the element that is removed or null if nothing was removed. |
|
61
|
|
|
*/ |
|
62
|
2 |
|
public function removeElementIndex($index) |
|
63
|
|
|
{ |
|
64
|
2 |
|
if (array_key_exists($index, $this)) { |
|
65
|
1 |
|
$element = $this[$index]; |
|
66
|
|
|
|
|
67
|
1 |
|
unset($this[$index]); |
|
68
|
1 |
|
} else { |
|
69
|
1 |
|
$element = null; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
2 |
|
return $element; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Removes the given element. |
|
77
|
|
|
* |
|
78
|
|
|
* @param float|BigNumber $element The element to remove. |
|
79
|
|
|
* @return bool Returns true when the element was removed; false otherwise. |
|
80
|
|
|
*/ |
|
81
|
2 |
|
public function removeElement($element) |
|
82
|
|
|
{ |
|
83
|
2 |
|
$bigNumber = new BigNumber($element); |
|
|
|
|
|
|
84
|
2 |
|
$key = array_search($bigNumber, $this->getArrayCopy(), false); |
|
85
|
|
|
|
|
86
|
2 |
|
if ($key === false) { |
|
87
|
1 |
|
return false; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
unset($this[$key]); |
|
91
|
|
|
|
|
92
|
1 |
|
return true; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Sets the element located at the given index. |
|
97
|
|
|
* |
|
98
|
|
|
* @param int $index The index of the element to get. |
|
99
|
|
|
* @param float|BigNumber $value The value to set. |
|
100
|
|
|
*/ |
|
101
|
44 |
|
public function setElement($index, $value) |
|
102
|
|
|
{ |
|
103
|
44 |
|
$this->validateIndex($index, false); |
|
104
|
|
|
|
|
105
|
44 |
|
$bigNumber = new BigNumber($value); |
|
|
|
|
|
|
106
|
|
|
|
|
107
|
44 |
|
for ($i = count($this); $i < $index; ++$i) { |
|
108
|
1 |
|
$this->setElement($i, new BigNumber(0)); |
|
109
|
1 |
|
} |
|
110
|
|
|
|
|
111
|
44 |
|
$this[$index] = $bigNumber; |
|
112
|
44 |
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Validates the index. |
|
116
|
|
|
* |
|
117
|
|
|
* @param int $index The index to validate. |
|
118
|
|
|
* @param bool $indexShouldExists Whether or not the index should exists. |
|
119
|
|
|
* @throws InvalidArgumentException Thrown when the index is invalid. |
|
120
|
|
|
*/ |
|
121
|
34 |
|
protected function validateIndex($index, $indexShouldExists) |
|
122
|
|
|
{ |
|
123
|
34 |
|
if ($index < 0) { |
|
124
|
2 |
|
throw new InvalidArgumentException(sprintf('The index %d is invalid.', $index)); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
34 |
|
if ($indexShouldExists && !array_key_exists($index, $this)) { |
|
128
|
1 |
|
throw new InvalidArgumentException(sprintf('The index %d is invalid.', $index)); |
|
129
|
|
|
} |
|
130
|
34 |
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Gets the amount of components in this tuple. |
|
134
|
|
|
* |
|
135
|
|
|
* @return int |
|
136
|
|
|
*/ |
|
137
|
20 |
|
public function getSize() |
|
138
|
|
|
{ |
|
139
|
20 |
|
return count($this); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Gets the maximum value in the tuple. |
|
144
|
|
|
* |
|
145
|
|
|
* @return BigNumber |
|
146
|
|
|
*/ |
|
147
|
1 |
|
public function getMaximumValue() |
|
148
|
|
|
{ |
|
149
|
1 |
|
return max($this->getArrayCopy()); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Gets the minimum value in the tuple. |
|
154
|
|
|
* |
|
155
|
|
|
* @return BigNumber |
|
156
|
|
|
*/ |
|
157
|
1 |
|
public function getMinimumValue() |
|
158
|
|
|
{ |
|
159
|
1 |
|
return min($this->getArrayCopy()); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Converts this tuple to a string. |
|
164
|
|
|
* |
|
165
|
|
|
* @return string |
|
166
|
|
|
*/ |
|
167
|
10 |
|
public function toString() |
|
168
|
|
|
{ |
|
169
|
10 |
|
return '[' . implode(', ', $this->getArrayCopy()) . ']'; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Converts this tuple to a string. |
|
174
|
|
|
* |
|
175
|
|
|
* @return string |
|
176
|
|
|
*/ |
|
177
|
9 |
|
public function __toString() |
|
178
|
|
|
{ |
|
179
|
9 |
|
return $this->toString(); |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: