In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "installation and use of DS extended data structures in PHP". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
You can only install and use the data structure extension above PHP7. It is relatively easy to install:
1. Run the command pecl install ds
two。 Add extension=ds.so to php.ini
3. Restart PHP or reload configuration
Collection Interface: a basic interface that contains common functions for all data structures in this library. It guarantees that all structures are traversable, countable, and can be converted to json using json_encode ().
Ds\ Collection implements Traversable, Countable, JsonSerializable {/ * method * / abstract public void clear (void) abstract public 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 an one-dimensional digital key array, with the exception of a few characteristics:
Values will 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:Vector is a series of values in a continuous buffer that automatically grows and shrinks. It is the most efficient sequential structure, the index of the value is mapped directly to the index in the buffer, and the growth factor is not bound to a specific multiple or index. It has the following advantages and disadvantages:
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).
But shift (), unshift (), insert () and remove () are 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 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 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.
Deque Class: the abbreviation for "double-ended queue", also used in Ds\ Queue, has two pointers, head and tail. The pointers can "wrap around" the end of the buffer, which avoids the need to move other values around to make room. This makes 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 an array for the same number of values.
Automatically frees allocated memory when its size drops low enough.
Get (), set (), push (), pop (), shift (), and unshift () are all O (1).
But Capacity must be a power of 2.insert () and remove () are O (n).
Map Class: a continuous collection of key-value pairs, almost the same as an array. The key can be of any type, but must be unique. If the same key is added to the map, the value is replaced. It has the following advantages and disadvantages:
Keys and values can be 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 its size drops low enough.
Can't be converted to an array when objects are 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: unique value sequence. This implementation uses the same hash table as Ds\ Map, where values are used as keys and the mapped value is ignored. It has the following advantages and disadvantages:
Values can be any type, including objects.
Supports array syntax (square brackets).
Insertion order is preserved.
Automatically frees allocated memory when its size drops low enough.
Add (), remove () and contains () are all O (1).
But Doesn't support push (), pop (), insert (), shift (), or unshift (). Get () is O (n) if there are deleted values in the buffer before the accessed index, O (1) otherwise.
Stack Class: the "last in, first out" collection, which only allows access and iteration at the top of the structure.
Ds\ Stack implements Ds\ Collection {/ * method * / public void allocate (int $capacity) public int capacity (void) public void clear (void) public Ds\ Stack copy (void) public bool isEmpty (void) public mixed peek (void) public mixed pop (void) public void push ([mixed $... values]) public array toArray (void)}
Queue Class: the "first in, first out" collection, which only allows access and iteration at the front end of the structure.
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 mixed peek (void) public mixed pop (void) public void push ([mixed $... values]) public array toArray (void)}
PriorityQueue Class: a priority queue is very similar to a queue, but the value is pushed to the queue at the specified priority, and the highest priority value is always at the front of the queue and remains in the same priority element "first-in, first-out" order. Iterating on a PriorityQueue is destructive, equivalent to a continuous pop-up 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\ PriorityQueue copy (void) public bool isEmpty (void) public mixed peek (void) public mixed pop (void) public void push (mixed $value, int $priority) public array toArray (void)} "installation and use of DS extended data structures in PHP" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.