php8.5
Home/ Manual/ reference / ds/ The Vector class

The Vector class

A Vector is a sequence of values in a contiguous buffer that grows and shrinks automatically. It’s the most efficient sequential structure because a value’s index is a direct mapping to its index in the buffer, and the growth factor isn't bound to a specific multiple or exponent.

Intro

A Vector is a sequence of values in a contiguous buffer that grows and shrinks automatically. It’s the most efficient sequential structure because a value’s index is a direct mapping to its index in the buffer, and the growth factor isn't bound to a specific multiple or exponent.

Strengths

  • Supports array syntax (square brackets).
  • Uses less overall memory than an Array for the same number of values.
  • Automatically frees allocated memory when its size drops low enough.
  • Capacity does not have to be a power of 2.
  • get(), set(), push(), pop() are all O(1).

Weaknesses

Class synopsis

class Ds\Vector { }

Predefined Constants

Ds\Vector::MIN_CAPACITY

Changelog

VersionDescription
PECL ds 1.3.0The class now implements ArrayAccess.
PECL ds 1.2.0Ds\Vector::MIN_CAPACITY changed from 10 to 8.

Vector

Source: reference/ds/ds.vector.xml · from the official PHP manual (php/doc-en)