The following small series for everyone to bring a cliché data structure in PHP: DS extension. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.
PHP7 above to install and use this data structure extension, the installation is relatively simple:
1. Run Command pecl install DS
2. Add extension=ds.so to PHP.ini
3. Restart PHP or reload configuration
Collection Interface: contains basic Interface for all the common features of data structures in this library. It guarantees that all structures is traversable, countable, and can be converted to JSON using Json_encode ().
Ds\collection implements Traversable, countable, jsonserializable {/* method */abstract public void Clear (void) Abstract P ublic ds\collection copy (void) abstract public bool IsEmpty (void) abstract public array toArray (void)}
hashable Interface:which allows objects to be used as keys.
ds\hashable {/* method */abstract public bool Equals (object $obj) abstract public mixed hash (void)}
Sequence Interface: A Sequence is equivalent to a one-dimensional numeric key array with the exception of a few characteristics:
Values would always be indexed as [0, 1, 2, ..., size-1].
Only allowed to access values by index in the range [0, size-1].
Use cases:
Wherever you would use an array as a list (not concerned with keys).
A more efficient alternative to spldoublylinkedlist and Splfixedarray.
Vector Class: Vectors are a series of values in a continuous buffer that automatically grows and shrinks. It is the most efficient sequential structure in which the index of the value is mapped directly to the index in the buffer, and the growth factor is not bound to a specific multiplier or exponent. It has the following advantages and disadvantages:
Supports array Syntax (square brackets).
Uses less overall memory than a array for the same number of values.
Automatically frees allocated memory when it size drops low enough.
Capacity does not has to be a power of 2.
Get (), set (), push (), pop () is all O (1).
But shift (), unshift (), insert () and remove () is all O (n).
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 the 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 emptyds\vector::join-joins all values together as a string. Ds\vector::jsonserialize-returns a representation that can is 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::p op-removes and returns the last value. Ds\vector::p Ush-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.
Deque Class:the abbreviation for "double-ended queue", also used in Ds\queue, has a head, tail two pointers. The pointers can "wrap around" the end of the buffer, which avoids the need to move other values around to make. This makes the shift and Unshift very fast-something a ds\vector can ' t compete with. It has the following advantages and disadvantages:
Supports array Syntax (square brackets).
Uses less overall memory than a array for the same number of values.
Automatically frees allocated memory when it size drops low enough.
Get (), set (), push (), pop (), Shift (), and Unshift () is all O (1).
But capacity must be a power of 2.insert () and remove () is O (n).
Map Class: a contiguous set of key-value pairs, almost the same as an array. The key can be of any type, but must be unique. If you use the same key to add to the map, the value is replaced. It has the following advantages and disadvantages:
Keys and values can is any type, including objects.
Supports array Syntax (square brackets).
Insertion Order is preserved.
Performance and memory efficiency is very similar to an array.
Automatically frees allocated memory when it size drops low enough.
Can ' t is converted to a array when objects is used as keys.
Pair Class: A pair is used by Ds\map to pair keys with values.
Ds\pair implements Jsonserializable {/* method */public construct ([Mixed $key [, mixed $value])}
Set Class: A sequence of unique values. This implementation uses the same hash table as Ds\map, where values be used as keys and the mapped value is ignored. The following advantages and disadvantages:
Values can is any type, including objects.
Supports array Syntax (square brackets).
Insertion Order is preserved.
Automatically frees allocated memory when it size drops low enough.
Add (), remove () and contains () is all O (1).
But doesn ' t support push (), pop (), insert (), Shift (), or unshift (). Get () is O (n) If there was deleted values in the buffer before the accessed index, O (1) otherwise.
Stack Class: "Last in, first Out" collection, which allows access and iteration only at the top of the structure.
Ds\stack implements Ds\collection {/* method */public void Allocate (int $capacity) public int capacity (void) public void C Lear (void) public ds\stack copy (void) public bool IsEmpty (void) public mixed peek (void) public mixed pop (void) p ublic void Push ([mixed $...values]) public array toArray (void)}
Queue Class:"First in, first Out" collection, which allows access and iteration only at the front of the fabric.
Ds\queue implements Ds\collection {/* Constants */const int min_capacity = 8;/* method */public void Allocate (int $capacity public int capacity (void) public void clear (void) public ds\queue copy (void) public bool IsEmpty (void) public mi Xed peek (void) public mixed pop (void) public void push ([mixed $...values]) public array toArray (void)}
Priorityqueue class: The priority queue is very similar to the queue, but the value is pushed into the queue with the specified priority, the highest priority value is always in front of the queue, and the priority element is retained in first in, first out order. Recursion on a priorityqueue is destructive, equivalent to a continuous popup operation until the queue is empty. Implemented using a Max heap.
ds\priorityqueue implements Ds\collection {/* Constants */const int min_capacity = 8;/* method */public void Allocate (int $capacity) public int capacity (void) public void clear (void) public Ds\priorit Yqueue copy (void) public bool IsEmpty (void) public mixed peek (void) public mixed pops (void) public void push (Mixe d $value, int $priority) public array toArray (void)}