Data structure in PHP: DS extension

Source: Internet
Author: User
Tags hashable map class

  PHP7 above to install and use this data structure extension, the installation is relatively simple:

1. Run the command pecl install DS2. Add EXTENSION=DS.SO3 to PHP.ini. 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 ().
    Implements Traversable, countable, jsonserializable {/**/abstractpublic  void Clear (void)abstractpubliccopy  (void)abstract   Public bool IsEmpty (void)abstractpublicarray  ToArray (void)}

  • hashable Interface : which allows objects to be used as keys.
     ds\hashable { /*   method  */ abstract  public  bool Equals (object   $obj   abstract  Span style= "COLOR: #0000ff" >public  mixed   Hash (void)}  

     

  • Sequence Interface : a Sequence 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 forA required capacity.Ds\vector:: Apply-updates All values by applying aCallback functionTo eachValue.Ds\vector:: Capacity-returns the Currentcapacity.Ds\vector:: Clear-removes all values.Ds\vector:: __construct-creates ANewInstance.Ds\vector:: Contains-determinesifThe vector contains given values.Ds\vector::Copy-returns a shallowCopyof the vector.Ds\vector::Count-returns the Numberof values in the collection.Ds\vector:: Filter-creates ANewVector using a callable to determine which values toinclude.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 RO Om. 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 it size drops low enough.
      • get (),  set (),  push (),  pop (),  < Span class= "function" >shift (), And unshift ()  are all O (1).
      • But capacity must be a power of 2.insert ()  and remove ()  are 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. It has 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\stackImplementsds\collection {/*Method*/ Publicvoid Allocate (int$capacity ) Publicint capacity (void) Publicvoid Clear (void) PublicDs\stackCopy(void) Publicbool IsEmpty (void) Public Mixedpeek (void) Public Mixedpop (void) Publicvoid Push ([Mixed$...values]) Public ArraytoArray (void)}

  • Queue Class:"First in, first Out" collection, which allows access and iteration only at the front of the fabric.
    Ds\queueImplementsds\collection {/*Constants*/Constint min_capacity = 8 ;/*Method*/ Publicvoid Allocate (int$capacity ) Publicint capacity (void) Publicvoid Clear (void) PublicDs\queueCopy(void) Publicbool IsEmpty (void) Public Mixedpeek (void) Public Mixedpop (void) Publicvoid Push ([Mixed$...values]) Public ArraytoArray (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 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\priorityqueueImplementsds\collection {/*Constants*/Constint min_capacity = 8 ;/*Method*/ Publicvoid Allocate (int$capacity ) Publicint capacity (void) Publicvoid Clear (void) PublicDs\priorityqueueCopy(void) Publicbool IsEmpty (void) Public Mixedpeek (void) Public Mixedpop (void) Publicvoid Push (Mixed $valueInt$priority ) Public ArraytoArray (void)}

Data structure in PHP: DS extension

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.