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).
Class synopsis
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
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-class.ds-vector.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.