• PHP Video Tutorials

PHP - Ds Set capacity() Function



The PHP Ds\set::capacity() function is used to retrieve the capacity of any collection such as set, deque, stack, etc, on which this function is invoked. This function returns an integer value that denotes the current capacity of the set.

Syntax

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

public int Ds\Set::capacity( void )

Parameters

This function does not accept any parameter.

Return value

This function returns the current capacity of the set.

Example 1

The following is the basic example of the PHP Ds\Set::capacity() function.

<?php 
   $set = new \Ds\Set();
   echo "The capacity of the set is: ";
   var_dump($set-> capacity());
?>

Output

The above program produces the following output −

The capacity of the set is: int(8)

Example 2

The following is another example of the PHP Ds\Set::capacity() function. We use this function to find the capacity of this set ([]).

<?php 
   $set = new \Ds\Set([1, 2, 3]);
   echo "The capacity of the set (initial): ";
   print_r($set->capacity());
   $set->allocate(16);
   echo "\nThe capacity of the set after allocating new capacity: ";
   print_r($set->capacity());
?>

Output

After executing the above program, it will display the following output −

The capacity of the set (initial): 8
The capacity of the set after allocating new capacity: 16

Example 3

In the below example, we use the capacity() function to retrieve the capacity of the current set.

<?php 
   $set = new \Ds\Set([10, 20, 30, 40]);
   echo "The set values are: \n";
   print_r($set);
   echo "The capacity of the set: ";
   print_r(8 * $set->capacity());
?>

Output

Once the above program is executed, it will generate the following output −

The set values are:
Ds\Set Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
)
The capacity of the set: 64
php_function_reference.htm
Advertisements