Vector (class)
The Vector class
Introduction
(No version information available, might only be in Git)
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
Ds\Vector implements Ds\Sequence {
/* Constants */
/* Methods */
public void allocate ( int $capacity )
public void apply ( callable $callback )
public int capacity ( void )
public void clear ( void )
public bool contains ([ mixed $...values ] )
public Ds\Vector copy ( void )
public Ds\Vector filter ([ callable $callback ] )
public mixed find ( mixed $value )
public mixed first ( void )
public mixed get ( int $index )
public void insert ( int $index [, mixed $...values ] )
public bool isEmpty ( void )
public string join ([ string $glue ] )
public mixed last ( void )
public Ds\Vector map ( callable $callback )
public Ds\Vector merge ( mixed $values )
public mixed pop ( void )
public void push ([ mixed $...values ] )
public mixed reduce ( callable $callback [, mixed $initial ] )
public mixed remove ( int $index )
public void reverse ( void )
public Ds\Vector reversed ( void )
public void rotate ( int $rotations )
public void set ( int $index , mixed $value )
public mixed shift ( void )
public Ds\Vector slice ( int $index [, int $length ] )
public void sort ([ callable $comparator ] )
public Ds\Vector sorted ([ callable $comparator ] )
public number sum ( void )
public array toArray ( void )
public void unshift ([ mixed $values ] )}
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.
© 1997–2017 The PHP Documentation Group
Licensed under the Creative Commons Attribution License v3.0 or later.
https://secure.php.net/manual/en/class.ds-vector.php