Open In App

PHP Ds\Deque Functions Complete Reference

Improve
Improve
Like Article
Like
Save
Share
Report

A deque data structure is used to store the sequence of values in a contiguous memory buffer which grows and shrinks automatically. The deque is the abbreviation of “double-ended queue”.

Requirements: PHP 7 is required for both extension and the compatibility polyfill.

Installation: The easiest way to install data structure by using the PECL extension.

pecl install ds

Syntax:

public Ds\Deque:: function()

Example: Below programs illustrate the Ds\Deque::get() fon in PHP:

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 get() function to find the index value
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)

The complete list of Ds\Deque functions are given below:

 

Functions 

Description

allocate()  This is used to allocate memory as specified in the argument.
apply()  Update the values of Deque by performing operations as defined by the callback function.
capacity()  Get the current capacity of the Deque.
clear()  Clear the Deque by removing all elements from the Deque.
__construct()  It is used to create a new instance.
copy()  Return a shallow copy of the Deque.
count()  Get the number of elements in the Deque.
filter()  Filter out the elements from the deque based on the operation defined in the callback function.
find()  Find the index of the element in the Deque if element found in the Deque.
first()  Returns the first value in the Deque if Deque is not empty.
get()  Return the value at the given index.
insert()  Insert the value at the given index in the Deque.
isEmpty()  Check the Deque is empty or not.
join()  Join all values in the Deque as a string. 
last()  Return the last element of Deque if Deque is not empty.
map()  Returns the result of applying a callback to each value
merge()  Returns the result of adding all given values to the deque.
pop()  Remove the last element from Deque (if Deque is not empty) and return it.
push()  Add the elements to the Deque by appending an element at the end of the Deque.
remove()  Remove and return the index value.
reverse()  Reverse the elements in the Deque in place.
reversed()  Return the copy of Deque which contains the elements in reversed order.
rotate()  Rotate the elements of Deque by the given number of rotations.
set()  Set the value at the given index in the Deque.
shift()  Remove and return the first value of deque.
slice()  Return a sub-Deque which contains elements of the Deque within the index range.
sort()  Sort the Deque in place by arranging the elements in increasing order.
sorted()  Return a copy of Deque which contains the element in the original Deque in increasing order.
sum()  Return the sum of all elements present in the Deque.
toArray()  The elements of the array will be in the same order as they are in Deque.
unshift()  It is used to add the value in front of the deque.


Last Updated : 25 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads