Vector
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).
- shift(), unshift(), insert() and remove() are all O(n).
Constants
MIN_CAPACITY |
|
Methods
Creates a new instance, using either a traversable object or an array for the initial values.
Ensures that enough memory is allocated for a required capacity.
Updates all values by applying a callback function to each value in the vector.
Returns the current capacity.
Removes all values from the vector.
Determines if the vector contains all values.
Returns a shallow copy of the vector.
Creates a new vector using a callable to determine which values to include.
Returns the index of the value, or FALSE if not found.
Returns the first value in the vector.
Returns the value at a given index.
No description
Inserts values into the sequence at a given index.
Joins all values together as a string using an optional separator between each value.
Returns the last value in the sequence.
Returns the result of applying a callback function to each value in the sequence.
Removes and returns the last value.
Adds values to the end of the sequence.
Reduces the sequence to a single value using a callback function.
Removes and returns a value by index.
Reverses the sequence in-place.
Returns a reversed copy of the sequence.
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.
Updates a value at a given index.
Removes and returns the first value.
Creates a sub-sequence of a given range.
Sorts the sequence in-place, using an optional comparator function.
Returns a sorted copy, using an optional comparator function.
Returns the sum of all values in the sequence.
Note: Arrays and objects are considered equal to zero when
calculating the sum.
Adds values to the front of the sequence, moving all the current values forward to make room for the new values.
Count elements of an object
Returns whether the collection is empty.
Converts the collection to an array.
Specify data which should be serialized to JSON
No description
No description
No description
No description
Details
__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.
This removes the need to reallocate the internal as values are added.
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.
Traversable
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.
Sequence
map(callable $callback)
Returns the result of applying a callback function to each value in the sequence.
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.
Note: Casting to an array is not supported yet.
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