A deque (short for "double-ended queue") is a data structure that stores a
sequence of values in a memory buffer that automatically grows and
shrinks.
Requirements: You need PHP 7 to use this extension and its
compatibility polyfill.
Installation: The easiest way to install the deque data structure is
with the PECL extension. Run the following command:
pecl install ds
Syntax:
public Ds\Deque:: function()
Example:
Here’s an example of how to use the
`Ds\Deque::get()`
function in PHP:
<?php // Declare a deque $deck = new \Ds\Deque([10, 20, 3, 40, 50, 6]); echo "Elements in the Deque\n"; // Display the deque elements print_r($deck); echo "\nElement at index 2 in the deque: "; // Use the get() function to find the value at index 2 var_dump($deck->get(2)); ?>
Output:
Elements in the Deque Ds\Deque Object ( [0] => 10 [1] => 20 [2] => 3 [3] => 40 [4] => 50 [5] => 6 ) Element at index 2 in the deque: int(3)
This example shows how to declare a deque, display its elements, and get a
specific element by its index.
The complete list of Ds\Deque functions are given below:
Deque Functions | Description |
---|---|
allocate(size) | Allocate memory for the deque to hold a specific number of elements (`size`). This pre-allocates space to potentially avoid performance overhead during insertions or removals. |
apply(callback) | Apply a callback function to each element in the deque. The callback function takes the current element value and index as arguments and can modify the element in-place. |
capacity() | Return the current maximum number of elements the deque can hold without needing reallocation. |
clear() | Remove all elements from the deque. |
__construct([initial_size]) | **Optional:** Create a new deque instance. In some implementations, a deque can be created without an explicit constructor call. You can optionally provide an initial size for the deque. |
copy() | Create a new deque that is a shallow copy of the original deque. Changes to the original deque will not be reflected in the copy, and vice versa. |
count() | Return the number of elements currently in the deque. |
filter(callback) | Create a new deque containing only the elements that pass a test defined by the callback function. |
find(value) | Return the index of the first occurrence of a specific value in the deque, or -1 if not found. |
first() | Return the first element in the deque (same as get(0)). If the deque is empty, it may return an error or null value. |
get(index) | Return the element at the specified index in the deque. |
insert(index, value) | Insert a new element at a specific index in the deque. |
isEmpty() | Check whether the deque is empty (contains no elements). |
join(separator) | Join all elements of the deque into a string, separated by the provided delimiter (separator). |
last() | Return the last element in the deque (same as get(count() - 1)). If the deque is empty, it may return an error or null value. |
map(callback) | Create a new deque by applying a callback function to each element of the original deque. The callback function's return value becomes the element in the new deque. |
merge(...values) | Add all the provided values to the end of the deque. |
pop() | Remove and return the last element from the deque. If the deque is empty, it may return an error or null value. |
push(value) | Add a new element to the end of the deque. |
remove(index) | Remove and return the element at the specified index from the deque. |
reverse() | Reverse the order of elements in the deque in-place. |
reversed() | Create a new deque containing a reversed copy of the elements from the original deque. |
rotate(offset) | Rotate the elements in the deque by a specified number of positions. Positive offsets shift elements to the right, negative offsets to the left. |
set(index, value) | Set the value at the specified index in the deque. |
shift() | Remove and return the first element from the deque. If the deque is empty, it may return an error or null value. |
slice(start, length) | Extract a sub-deque containing a range of elements from the original deque. |
sort(comparison_function) | Sort the elements of the deque in-place. You can optionally provide a comparison function to define the sorting order. |
sorted(comparison_function) | Create a new deque containing a sorted copy of the elements from the original deque. You can optionally provide a comparison function to define the sorting order. |
sum() | Return the sum of all the elements in the deque. |
toArray() | Convert the deque to a regular PHP array. |
unshift(value) | Add one or more elements to the beginning of the deque. |