• PHP Video Tutorials

PHP - Ds Set::allocate() Function



The PHP Ds\Set::allocate() function is used to allocate the specified capacity (or memory) for a required capacity to the current set. If the specified capacity is less than or equal to the current capacity, it will remain the same.

Syntax

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

public void Ds\Set::allocate( int $capacity )

Parameters

This function accepts a single parameter called 'capacity', which is described below −

  • capacity − The number of values for which capacity should be allocated.

Return value

This function does not return any value.

Example 1

The following program demonstrates the usage of the PHP Ds\Set::allocate() function.

<?php
   $set = new \Ds\Set();
   echo "The capacity of the set (initial): ";
   var_dump($set->capacity());
   $val = 10;
   echo "The capacity need to be allocated: ".$val;
   #using the allocate() function
   $set->allocate($val);
   echo "\nThe capacity of the set after allocating new capacity: ";
   #using capacity() function
   var_dump($set->capacity());
?>

Output

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

The capacity of the set (initial): int(8)
The capacity need to be allocated: 10
The capacity of the set after allocating new capacity: int(16)

Example 2

The following is another example of the PHP Ds\Set::allocate() function. We use this function to allocate new specified capacities 16 and 64 to this set ([]).

<?php
   $set = new \Ds\Set();
   echo "The capacity of the set (initial): ";
   var_dump($set->capacity());
   $val1 = 16;
   echo "The first capacity value: ".$val1;
   #using allocate() function
   $set->allocate($val1);
   echo "\nThe capacity of the set after the first capacity allocated: ";
   #using capacity() function
   var_dump($set->capacity());
   $val2 = 64;
   echo "The second capacity value: ".$val2;
   $set->allocate($val2);
   echo "\nThe capacity of the set after the second capacity allocated: ";
   var_dump($set->capacity());
?>

Output

The above program displays the following output −

The capacity of the set (initial): int(8)
The first capacity value: 16
The capacity of the set after the first capacity allocated: int(16)
The second capacity value: 64
The capacity of the set after the second capacity allocated: int(64)
php_function_reference.htm
Advertisements