Introduction to PHP data structures extension

Time:2022-5-14

In PHP, because arrays are too powerful to include these data structures, we don’t need to pay much attention to these data structures. Over time, these concepts will fade, not that there are no data structures in PHP.

There is an extension in PHPData Structures, this extension includes these commondata structure, you can view the connection in detaildata structure

PHP data structure

  • Priority queue
  • Double ended queue deque
  • Queue FIFO (first in first out)
  • Stack LIFO (first in, last out)
  • Hash table

    • Set set
    • Map dictionary

Introduction to data structure

Priority queue

PriorityQueue is very similar to queue. The value is pushed into the queue with the specified priority, and the value with the highest priority will always be in front of the queue.

be careful

  • Keep the first in first out order for values with the same priority.
  • Iterative PriorityQueue is destructive, equivalent to continuous pop-up operations until the queue is empty.

Set capacity

The default capacity is 8. You can set the capacity manually. This capacity does not refer to the length of the queue, but the storage space. Make sure there is enough memory when reallocating capacity

  • If the value is less than or equal to the current capacity, the capacity remains unchanged.

    $queue = new Ds\PriorityQueue(); 
    $queue->allocate(8);

Get capacity

  • When the capacity is currently set manually, if the set capacity is greater than the actual occupied capacity, the set capacity will be returned. Conversely, the actual capacity is returned.

    $queue = new Ds\PriorityQueue(); 
    //The default value of 8 is returned
    $queue->capacity();

    set priority

    The higher the value, the higher the priority

    $queue = new Ds\PriorityQueue(); 
    $queue->push('value1', 1);
    $queue->push('value2', 2);

    Examples

    $queue = new Ds\PriorityQueue(); 
    $queue - > push ('sand monk ', 2);
    $queue - > push ('tang monk ', 5);
    $queue - > push ('white dragon horse ', 1);
    $queue - > push ('pig Bajie ', 3);
    $queue - > push ('monkey King ', 4);
    $cout = $queue->count();
    for($i=0; $i<$cout; $i++) {
      echo $queue->pop();
      echo PHP_EOL;
    }

output

Tang Monk
Sun WuKong
Zhu Bajie
Monk Sha
White dragon horse

Application scenario

  • During MySQL query, in order to speed up the query speed and avoid sorting, the index cannot be used, and there is no sorting. Manually sort at the server code level and then return.
  • Other application scenarios

Double ended queue deque

There are two pointers pointing to the head and tail respectively. It can be inserted and ejected at the head and tail respectively.

advantage

  • Support array syntax (square brackets).
  • For the same number of values, it takes less memory than the array.
  • When its size drops low enough, the allocated memory is automatically released.
  • Get(), set(), push(), pop(), shift() and unshift() are all o (1).

shortcoming

  • The set capacity value must be the power of 2, and the default value is 8. For example, 2 ^ 2
  • Insert () and remove () are o (n).

Class method description

Double ended queue deque

Examples

$deque = new Ds\Deque();
$deque - > push (... ['Tang Monk', 'Monkey King', 'pig Bajie', 'sand monk', 'white dragon horse'];
$clone = $deque->copy();
$count = $deque->count();
Echo 'header$ deque->first(). PHP_ EOL;
Echo 'Tail:'$ deque->last(). PHP_ EOL;
Echo '--- from the end of the team -' PHP_ EOL;
for($i=0; $i<$count; $i++) {
    echo $deque->pop();
    echo PHP_EOL;
}

Echo '--- start from the head of the team -' PHP_ EOL;
for($i=0; $i<$count; $i++) {
    echo $clone->shift();
    echo PHP_EOL;
}

output

Head: Tang Monk
Tail: white dragon horse
---Start at the end of the team----
White dragon horse
Monk Sha
Zhu Bajie
Sun WuKong
Tang Monk
---Start at the head of the team----
Tang Monk
Sun WuKong
Zhu Bajie
Monk Sha
White dragon horse

Application scenario

  • Multiple application scenarios

Queue FIFO (first in first out)

A queue is a set of “first in first out” or “FIFO”, which only allows access to the values in front of the queue.

Examples

$queue = new Ds\Queue(); 
$queue - > push ('tang monk ');
$queue - > push (... ['Monkey King', 'pig Bajie']);
$queue - > push (['sand monk', 'white dragon horse']);
print_r($queue);

output

Ds\Queue Object
(
    [0] = > Tang Monk
    [1] = > Monkey King
    [2] = > pig Bajie
    [3] => Array
        (
            [0] = > monk Sha
            [1] = > white dragon horse
        )
)

Stack LIFO (first in, last out)

Stack is a “last in, first out” or “LIFO” collection that allows access to only the values at the top of the structure.

Examples

$Stack = new Ds\Stack(); 
$stack - > push ('tang monk ');
$stack - > push (... ['Monkey King', 'pig Bajie']);
$stack - > push (... ['sand monk', 'white dragon horse']);

$cout = $Stack->count();
for($i=0; $i<$cout; $i++) {
    echo $Stack->pop();
    echo PHP_EOL;
}

output

White dragon horse
Monk Sha
Zhu Bajie
Sun WuKong
Tang Monk

Map dictionary

Map is the sequential set of key value pairs [key = > value], which is similar to array. Keys can be of any type, but they must be unique. If you use the same key to add a value to the map, the later value will replace the previous value.

advantage

  • Keys and values can be of any type, including objects
  • Support array syntax.
  • Keep the insertion order.
  • Performance and memory efficiency are similar to data.
  • When the size drops low enough, the allocated memory is automatically released.

shortcoming

  • When an object is used as a key, it cannot be converted to an array.

Set set

Set is a series of unique values. Only one set of keys does not store value, and keys cannot be repeated.

advantage

  • Values can be of any type, including objects.
  • Support array syntax.
  • Keep the insertion order.
  • When the size drops low enough, the allocated memory is automatically released.
  • The complexity of add(), remove() and contains() is O (1).

shortcoming

  • Push(), pop(), insert(), shift() or unshift() are not supported
  • If there is a deleted value in the buffer before the accessed index, get() is O (n), otherwise it is O (1).

The difference between map and set

  1. Different storage methods. Map stores the form of [key = > value], and set stores [… Keys];
  2. Both map and set use keys to ensure order, so keys are not allowed to be modified.