Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP - Ds Deque::capacity() Function



The PHPDs\Deque::capacity()function is used to retrieve the current capacity of a deque. The "capacity" of a deque is the number of elements that the deque can occupy.

The 'Ds\Deque' class provides another function namedallocate() function, using which you can allocate a new capacity to a deque.

Syntax

Following is the syntax of the PHP Ds\Deque::capacity() function −

public Ds\Deque::capacity(): int

Parameters

This function does not accept any parameter.

Return value

This function returns the current capacity of a deque.

Example 1

The following is the basic example of the Ds\Deque::capacity() function −

<?php
   $deque = new \Ds\Deque([1, 2, 3, 4, 5]);
   echo "The deque elements are: \n";
   print_r($deque);
   echo "The current capacity of a deque: ";
   print_r($deque->capacity());
?>

Output

After executing the above program, the following output will be displayed −

The deque elements are:
Ds\Deque Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
The current capacity of a deque: 8

Example 2

Following is another example of the PHP Ds\Deque::capacity() function. We use this function to find the current capacity of this deque (["Tutorials", "Point", "India"]) −

<?php
   $deque = new \Ds\Deque(["Tutorials", "Point", "India"]);
   echo "The deque elements are: \n";
   print_r($deque);
   echo "The current capacity of a deque: ";
   print_r($deque->capacity());
   $deque->allocate(64);
   echo "\nThe capacity after allocating: ";
   print_r($deque->capacity());
?>

Output

The above program generates the following output −

The deque elements are:
Ds\Deque Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)
The current capacity of a deque: 8
The capacity after allocating: 64

Example 3

Retrieving the current capacity of an empty ([]) deque −

<?php
   $deque = new \Ds\Deque([]);
   echo "The deque elements are: \n";
   print_r($deque);
   echo "The current capacity of a deque: ";
   print_r($deque->capacity());
?>

Output

Following is the capacity of an empty deque −

The deque elements are:
Ds\Deque Object
(
)
The current capacity of a deque: 8
php_function_reference.htm
Advertisements