Vector

A vector is a linear data structure where values are stored sequentially and also the size grows and shrinks automatically. Vector is one of the most efficient linear data structures as the value's index is mapped directly with the index of the buffer and allows faster access. DS vector class allows us to use the PHP array syntax for operations, but internally, it has less memory consumption than PHP array. It has constant time operations for push, pop, get, and set. Here is an example of vector:

$vector = new \Ds\Vector(["a", "b", "c"]); echo $vector->get(1)."\n"; $vector[1] = "d"; echo $vector->get(1)."\n"; $vector->push('f'); echo "Size of vector: ".$vector->count(); 

As we can see from the preceding code, we can define a vector ...

Get PHP 7 Data Structures and Algorithms now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.