The Vector class
(No version information available, might only be in Git)
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
Predefined Constants
Ds\Vector::MIN_CAPACITY
Table of Contents
- Ds\Vector::allocate — Allocates enough memory for a required capacity
- Ds\Vector::apply — Updates all values by applying a callback function to each value
- Ds\Vector::capacity — Returns the current capacity
- Ds\Vector::clear — Removes all values
- Ds\Vector::__construct — Creates a new instance
- Ds\Vector::contains — Determines if the vector contains given values
- Ds\Vector::copy — Returns a shallow copy of the vector
- Ds\Vector::count — Returns the number of values in the collection
- Ds\Vector::filter — Creates a new vector using a callable to determine which values to include
- Ds\Vector::find — Attempts to find a value's index
- Ds\Vector::first — Returns the first value in the vector
- Ds\Vector::get — Returns the value at a given index
- Ds\Vector::insert — Inserts values at a given index
- Ds\Vector::isEmpty — Returns whether the vector is empty
- Ds\Vector::join — Joins all values together as a string
- Ds\Vector::jsonSerialize — Returns a representation that can be converted to JSON
- Ds\Vector::last — Returns the last value
- Ds\Vector::map — Returns the result of applying a callback to each value
- Ds\Vector::merge — Returns the result of adding all given values to the vector
- Ds\Vector::pop — Removes and returns the last value
- Ds\Vector::push — Adds values to the end of the vector
- Ds\Vector::reduce — Reduces the vector to a single value using a callback function
- Ds\Vector::remove — Removes and returns a value by index
- Ds\Vector::reverse — Reverses the vector in-place
- Ds\Vector::reversed — Returns a reversed copy
- Ds\Vector::rotate — Rotates the vector by a given number of rotations
- Ds\Vector::set — Updates a value at a given index
- Ds\Vector::shift — Removes and returns the first value
- Ds\Vector::slice — Returns a sub-vector of a given range
- Ds\Vector::sort — Sorts the vector in-place
- Ds\Vector::sorted — Returns a sorted copy
- Ds\Vector::sum — Returns the sum of all values in the vector
- Ds\Vector::toArray — Converts the vector to an array
- Ds\Vector::unshift — Adds values to the front of the vector