class Vector implements Sequence (View source)

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).

Constants

MIN_CAPACITY

Methods

__construct(TValue[] $values = [])

Creates a new instance, using either a traversable object or an array for the initial values.

void
allocate(int $capacity)

Ensures that enough memory is allocated for a required capacity.

void
apply(callable $callback)

Updates all values by applying a callback function to each value in the vector.

int
capacity()

Returns the current capacity.

void
clear()

Removes all values from the vector.

bool
contains(TValue ...$values)

Determines if the vector contains all values.

TValue>
copy()

Returns a shallow copy of the vector.

TValue>
filter(callable|null $callback = null)

Creates a new vector using a callable to determine which values to include.

int|false
find(TValue $value)

Returns the index of the value, or FALSE if not found.

TValue
first()

Returns the first value in the vector.

TValue
get(int $index)

Returns the value at a given index.

getIterator()

No description

void
insert(int $index, TValue ...$values)

Inserts values into the sequence at a given index.

string
join(string $glue = null)

Joins all values together as a string using an optional separator between each value.

TValue
last()

Returns the last value in the sequence.

map(callable $callback)

Returns the result of applying a callback function to each value in the sequence.

merge(TValue2> $values)

Returns the result of adding all given values to the sequence.

TValue
pop()

Removes and returns the last value.

void
push(TValue ...$values)

Adds values to the end of the sequence.

TCarry
reduce(callable $callback, TCarry $initial = null)

Reduces the sequence to a single value using a callback function.

TValue
remove(int $index)

Removes and returns a value by index.

void
reverse()

Reverses the sequence in-place.

TValue>
reversed()

Returns a reversed copy of the sequence.

void
rotate(int $rotations)

Rotates the sequence by a given number of rotations, which is equivalent to successively calling $sequence->push($sequence->shift()) if the number of rotations is positive, or $sequence->unshift($sequence->pop()) if negative.

void
set(int $index, TValue $value)

Updates a value at a given index.

TValue
shift()

Removes and returns the first value.

TValue>
slice(int $index, int $length = null)

Creates a sub-sequence of a given range.

void
sort(callable|null $comparator = null)

Sorts the sequence in-place, using an optional comparator function.

TValue>
sorted(callable|null $comparator = null)

Returns a sorted copy, using an optional comparator function.

float|int
sum()

Returns the sum of all values in the sequence.
Note: Arrays and objects are considered equal to zero when calculating the sum.

void
unshift(TValue $values)

Adds values to the front of the sequence, moving all the current values forward to make room for the new values.

int
count()

Count elements of an object

bool
isEmpty()

Returns whether the collection is empty.

array
toArray()

Converts the collection to an array.

mixed
jsonSerialize()

Specify data which should be serialized to JSON

bool
offsetExists(TKey $offset)

No description

mixed
offsetGet(TKey $offset)

No description

void
offsetSet(TKey $offset, TValue $value)

No description

void
offsetUnset(TKey $offset)

No description

Details

__construct(TValue[] $values = [])

Creates a new instance, using either a traversable object or an array for the initial values.

Parameters

TValue[] $values

void allocate(int $capacity)

Ensures that enough memory is allocated for a required capacity.

This removes the need to reallocate the internal as values are added.

Parameters

int $capacity

The number of values for which capacity should be allocated.

Note: Capacity will stay the same if this value is less than or equal to the current capacity.

Return Value

void

void apply(callable $callback)

Updates all values by applying a callback function to each value in the vector.

Parameters

callable $callback

Return Value

void

int capacity()

Returns the current capacity.

Return Value

int

The current capacity.

void clear()

Removes all values from the vector.

Return Value

void

bool contains(TValue ...$values)

Determines if the vector contains all values.

Parameters

TValue ...$values

Values to check.

Return Value

bool

FALSE if any of the provided values are not in the sequence, TRUE otherwise.

TValue> copy()

Returns a shallow copy of the vector.

Return Value

TValue>

TValue> filter(callable|null $callback = null)

Creates a new vector using a callable to determine which values to include.

Parameters

callable|null $callback

Return Value

TValue>

A new sequence containing all the values for which either the callback returned TRUE, or all values that convert to TRUE if a callback was not provided.

int|false find(TValue $value)

Returns the index of the value, or FALSE if not found.

Parameters

TValue $value

The value to find.

Return Value

int|false

The index of the value, or FALSE if not found.

TValue first()

Returns the first value in the vector.

Return Value

TValue

The first value in the sequence.

Exceptions

UnderflowException

TValue get(int $index)

Returns the value at a given index.

Parameters

int $index

The index to access, starting at 0.

Return Value

TValue

The value at the requested index.

Traversable getIterator()

No description

Return Value

Traversable

An instance of an object implementing Iterator or Traversable

void insert(int $index, TValue ...$values)

Inserts values into the sequence at a given index.

Parameters

int $index

The index at which to insert. 0 <= index <= count

Note: You can insert at the index equal to the number of values.

TValue ...$values

The value or values to insert.

Return Value

void

string join(string $glue = null)

Joins all values together as a string using an optional separator between each value.

Parameters

string $glue

An optional string to separate each value.

Return Value

string

All values of the sequence joined together as a string.

TValue last()

Returns the last value in the sequence.

Return Value

TValue

The last value in the sequence.

Sequence map(callable $callback)

Returns the result of applying a callback function to each value in the sequence.

Parameters

callable $callback

Return Value

Sequence

The result of applying a callback to each value in the sequence.

Note: The values of the current instance won't be affected.

Sequence merge(TValue2> $values)

Returns the result of adding all given values to the sequence.

Parameters

TValue2> $values

A traversable object or an array.

Return Value

Sequence

The result of adding all given values to the sequence, effectively the same as adding the values to a copy, then returning that copy.

TValue pop()

Removes and returns the last value.

Return Value

TValue

The removed last value.

void push(TValue ...$values)

Adds values to the end of the sequence.

Parameters

TValue ...$values

The values to add.

Return Value

void

TCarry reduce(callable $callback, TCarry $initial = null)

Reduces the sequence to a single value using a callback function.

Parameters

callable $callback
TCarry $initial

The initial value of the carry value. Can be NULL.

Return Value

TCarry

The return value of the final callback.

TValue remove(int $index)

Removes and returns a value by index.

Parameters

int $index

The index of the value to remove.

Return Value

TValue

The value that was removed.

void reverse()

Reverses the sequence in-place.

Return Value

void

TValue> reversed()

Returns a reversed copy of the sequence.

Return Value

TValue>

A reversed copy of the sequence.

Note: The current instance is not affected.

void rotate(int $rotations)

Rotates the sequence by a given number of rotations, which is equivalent to successively calling $sequence->push($sequence->shift()) if the number of rotations is positive, or $sequence->unshift($sequence->pop()) if negative.

Parameters

int $rotations

The number of times the sequence should be rotated.

Return Value

void

void set(int $index, TValue $value)

Updates a value at a given index.

Parameters

int $index

The index of the value to update.

TValue $value

The new value.

Return Value

void

Exceptions

OutOfRangeException

TValue shift()

Removes and returns the first value.

Return Value

TValue

Exceptions

UnderflowException

TValue> slice(int $index, int $length = null)

Creates a sub-sequence of a given range.

Parameters

int $index

The index at which the sub-sequence starts. If positive, the sequence will start at that index in the sequence. If negative, the sequence will start that far from the end.

int $length

If a length is given and is positive, the resulting sequence will have up to that many values in it. If the length results in an overflow, only values up to the end of the sequence will be included. If a length is given and is negative, the sequence will stop that many values from the end. If a length is not provided, the resulting sequence will contain all values between the index and the end of the sequence.

Return Value

TValue>

A sub-sequence of the given range.

void sort(callable|null $comparator = null)

Sorts the sequence in-place, using an optional comparator function.

Parameters

callable|null $comparator

Return Value

void

TValue> sorted(callable|null $comparator = null)

Returns a sorted copy, using an optional comparator function.

Parameters

callable|null $comparator

Return Value

TValue>

Returns a sorted copy of the sequence.

float|int sum()

Returns the sum of all values in the sequence.
Note: Arrays and objects are considered equal to zero when calculating the sum.

Return Value

float|int

The sum of all the values in the sequence as either a float or int depending on the values in the sequence.

void unshift(TValue $values)

Adds values to the front of the sequence, moving all the current values forward to make room for the new values.

Parameters

TValue $values

The values to add to the front of the sequence.

Note: Multiple values will be added in the same order that they are passed.

Return Value

void

int count()

Since: 5.1

Count elements of an object

Return Value

int

The custom count as an integer.

The return value is cast to an integer.

bool isEmpty()

Returns whether the collection is empty.

Return Value

bool

array toArray()

Converts the collection to an array.

Note: Casting to an array is not supported yet.

Return Value

array

An array containing all the values in the same order as the collection.

mixed jsonSerialize()

Since: 5.4

Specify data which should be serialized to JSON

Return Value

mixed

data which can be serialized by json_encode, which is a value of any type other than a resource.

bool offsetExists(TKey $offset)

No description

Parameters

TKey $offset

An offset to check for.

Return Value

bool

true on success or false on failure.

The return value will be casted to boolean if non-boolean was returned.

mixed offsetGet(TKey $offset)

No description

Parameters

TKey $offset

The offset to retrieve.

Return Value

mixed

Can return all value types.

void offsetSet(TKey $offset, TValue $value)

No description

Parameters

TKey $offset

The offset to assign the value to.

TValue $value

The value to set.

Return Value

void

void offsetUnset(TKey $offset)

No description

Parameters

TKey $offset

The offset to unset.

Return Value

void