Menu

Reference / Manticore\Ds

Int32Array

finalclass
final class Int32Array<T> extends IntTypedArray

Signed 32-bit integers (4 bytes per element), range -2147483648..2147483647.

A value outside the range throws ValueError; it never wraps.

Type parameters

T
= int @extends IntTypedArray<T>

Example

use Manticore\Ds\Int32Array;

$a = new Int32Array(3);         // three zeros, 4 bytes each
$a[0] = 10;
$a[1] = '20';                   // a whole numeric string is accepted
$a[] = 30;                      // grows the array

echo count($a), ' ', $a->sum(), ' ', $a->max(), "\n";
echo json_encode($a), "\n";

try {
    $a[0] = 2147483648;         // one past the maximum: nothing wraps
} catch (ValueError $e) {
    echo $e->getMessage(), "\n";
}
try {
    $a[0] = 1.5;
} catch (TypeError $e) {
    echo $e->getMessage(), "\n";
}
try {
    echo $a[4];
} catch (OutOfBoundsException $e) {
    echo $e->getMessage(), "\n";
}

$a->apply(fn(int $x, int $i) => $x * 2);
echo implode(',', $a->toArray()), "\n";
echo $a->indexOf(40), "\n";

Output

4 60 30
[10,20,0,30]
Value 2147483648 is out of range for Int32Array (-2147483648..2147483647)
Manticore\Ds\Int32Array element must be of type int, float given
Index invalid or out of range
20,40,0,60
1

View source