HH\Vector::reserve

Reserves enough memory to accommodate a given number of elements

public function reserve(
  int $sz,
): void;

Reserves enough memory for $sz elements. If $sz is less than or equal to the current capacity of the current Vector, this method does nothing.

If $sz is less than zero, an exception is thrown.

Parameters

  • int $sz - The pre-determined size you want for the current Vector.

Returns

  • void

Examples

This example reserves space for 1000 elements and then fills the Vector with 1000 integers:

const int VECTOR_SIZE = 1000;

<<__EntryPoint>>
function basic_usage_main(): void {
  $v = Vector {};
  $v->reserve(VECTOR_SIZE);

  for ($i = 0; $i < VECTOR_SIZE; $i++) {
    $v[] = $i * 10;
  }

  \var_dump($v);
}