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

PHP - Ds\Queue::capacity() Function



The PHP Ds\Queue::capacity() function is used to retrieve the current capacity of a queue. The "capacity" of a queue refers to the number of elements it can hold.

The PHP Ds\Queue class provides another function namedallocate()function, which you can use to allocate enough memory for a required capacity.

Syntax

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

public Ds\Queue::capacity(): int

Parameters

This function does not accept any parameter.

Return value

This function returns the current capacity of a queue.

Example 1

The following program demonstrates the usages of the PHP Ds\Queue::capacity() function −

<?php  
   $queue = new \Ds\Queue([10, 20, 30]);
   echo "The queue elements are: \n";
   print_r($queue);
   echo "The current capacity of a queue: ";
   #using capacity() function   
   print_r($queue->capacity());
?>

Output

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

The queue elements are:
Ds\Queue Object
(
    [0] => 10
    [1] => 20
    [2] => 30
)
The current capacity of a queue: 8

Example 2

Following is another example of the PHP Ds\Queue::capacity() function. We use this function to retrieve the current capacity of this queue (['a', 'b', 'c', 'd']) −

<?php  
   $queue = new \Ds\Queue(['a', 'b', 'c', 'd']);
   echo "The queue elements are: \n";
   print_r($queue);
   echo "The current capacity before allocating new one: ";
   #using capacity() function   
   print_r($queue->capacity());
   echo "\nThe current capacity after allocating new one: ";
   $queue->allocate(16);   
   print_r($queue->capacity());
?>

Output

The above program provides the following output −

The queue elements are:
Ds\Queue Object
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
)
The current capacity before allocating new one: 8
The current capacity after allocating new one: 16

Example 3

Retrieving capacity of an empty ([""]) queue −

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

Output

Following is the output of the above program −

The queue elements are:
Ds\Queue Object
(
    [0] =>
)
The current capacity of a queue: 8
php_function_reference.htm
Advertisements