The Vector class

(PECL ds >= 1.0.0)

Introduction

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

  • shift(), unshift(), insert() and remove() are all O(n).

Class synopsis

classDs\VectorimplementsDs\Sequence, ArrayAccess {
constintMIN_CAPACITY = 10;
publicallocate(int$capacity): void
publicapply(callable$callback): void
publiccapacity(): int
publicclear(): void
publiccontains(mixed...$values): bool
publiccopy(): Ds\Vector
publicfilter(callable$callback = ?): Ds\Vector
publicfind(mixed$value): mixed
publicfirst(): mixed
publicget(int$index): mixed
publicinsert(int$index, mixed...$values): void
publicisEmpty(): bool
publicjoin(string$glue = ?): string
publiclast(): mixed
publicmap(callable$callback): Ds\Vector
publicmerge(mixed$values): Ds\Vector
publicpop(): mixed
publicpush(mixed...$values): void
publicreduce(callable$callback, mixed$initial = ?): mixed
publicremove(int$index): mixed
publicreverse(): void
publicrotate(int$rotations): void
publicset(int$index, mixed$value): void
publicshift(): mixed
publicslice(int$index, int$length = ?): Ds\Vector
publicsort(callable$comparator = ?): void
publicsorted(callable$comparator = ?): Ds\Vector
publicsum(): int|float
publictoArray(): array
publicunshift(mixed$values = ?): void
}

Predefined Constants

Ds\Vector::MIN_CAPACITY

Changelog

VersionDescription
PECL ds 1.3.0 The class now implements ArrayAccess.

Table of Contents

To Top