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

PHP - Ds Map::allocate() Function



The PHP Ds\Map::allocate() is used to allocate a new memory (capacity) for the current map. The allocated memory is sufficient for the required capacity.

The map capacity will be the remain same if the specified value is less than or equal to the current capacity. The capacity will always be rounded up to the nearest power of 2.

After allocating a new capacity to a map, you can check the current capacity using the capacity() function.

Syntax

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

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

Parameters

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

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

Return value

This function does not return any value.

Example 1

The following is the basic example of the PHP Ds\Map::allocate() function −

<?php  
   $map = new \Ds\Map();
   echo "The map values are: \n";
   print_r($map);
   $capacity = 50;
   echo "The capacity need to allocate: ".$capacity;
   #using allocate() function
   $map->allocate($capacity);
   echo "\nThe map capacity is: ";
   print_r($map->capacity());
?>

Output

The above program produces the following output −

The map values are:
Ds\Map Object
(
)
The capacity need to allocate: 50
The map capacity is: 64

Example 2

The following is another example of the PHP Ds\Map::allocate() function. We use this function to allocate a new capacity to this map ([10, 20, 30, 40, 50]) −

<?php  
   $map = new \Ds\Map([10, 20, 30, 40, 50]);
   echo "The map values are: \n";
   foreach ($map as $key => $value) {
       echo "[".$key."] => ".$value."\n";
   }
   echo "The map capacity (initial): ";
   print_r($map->capacity());
   $capacity = 64;
   echo "\nThe capacity need to allocate: ".$capacity;
   #using allocate() function
   $map->allocate($capacity);
   echo "\nThe map capacity after allocating new capacity is: ";
   print_r($map->capacity());
?> 

Output

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

The map values are:
[0] => 10
[1] => 20
[2] => 30
[3] => 40
[4] => 50
The map capacity (initial): 8
The capacity need to allocate: 64
The map capacity after allocating new capacity is: 64

Example 3

If the specified value is less than or equal to the current capacity, the capacity will remain unchanged −

<?php  
   $map = new \Ds\Map(['a', 'b', 'c']);
   echo "The map values are: \n";
   foreach ($map as $key => $value) {
       echo "[".$key."] => ".$value."\n";
   }
   echo "The map capacity (initial): ";
   print_r($map->capacity());
   $capacity = 5;
   echo "\nThe capacity need to allocate: ".$capacity;
   #using allocate() function
   $map-<allocate($capacity);
   echo "\nThe map capacity after allocating new capacity is: ";
   print_r($map->capacity());
?>

Output

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

The map values are:
[0] => a
[1] => b
[2] => c
The map capacity (initial): 8
The capacity need to allocate: 5
The map capacity after allocating new capacity is: 8
php_function_reference.htm
Advertisements