PHP - Ds Vector::allocate() Function



The PHP Ds\Vector::allocate() function is used to allocate enough memory for the required capacity of the vector. The capacity of the vector is the memory or the number of elements that the vector can occupy.

If the current capacity of the vector is greater than the allocated capacity, then the old capacity remains the same, and the new capacity will not be allocated. You can use the capacity() function to check the current capacity of the vector.

Syntax

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

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

Parameters

This function accepts a single parameter named 'capacity' that holds the new capacity value that needs to be allocated.

Return value

This function doesn't return any value.

Example 1

The following program demonstrates the usage of the PHP Ds\Vector::allocate() function −

<?php 
   $vector = new \Ds\Vector([10, 20, 30]);
   echo "The vector elements are: \n";
   print_r($vector);
   $capacity = 20;
   echo "The capacity need to allocate: ".$capacity;
   $vector->allocate($capacity);
   echo "\nAfter allocating capacity is: ";
   print_r($vector->capacity());
?>

Output

The above program displays the following output −

The vector elements are:
Ds\Vector Object
(
    [0] => 10
    [1] => 20
    [2] => 30
)
The capacity need to allocate: 20
After allocating capacity is: 20

Example 2

Following is another example of the PHP Ds\Vector::allocate() function. We use this function to allocate a capacity "32" to this vector (["Tutorials", "Point", "India"]) −

<?php 
   $vector = new \Ds\Vector(["Tutorials", "Point", "India"]);
   echo "The vector elements are: \n";
   print_r($vector);
   echo "Capacity before allocating: ";
   print_r($vector->capacity());
   $capacity = 32;
   echo "\nThe capacity need to allocate: ".$capacity;
   echo "\nThe allocated capacity: ";
   $vector->allocate($capacity);
   print_r($vector->capacity());
?>

Output

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

The vector elements are:
Ds\Vector Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)
Capacity before allocating: 8
The capacity need to allocate: 32
The allocated capacity: 32

Example 3

If the specified capacity is less than the current capacity of the vector, the old capacity will remain the same, and the new capacity will not be allocated to the vector −

<?php 
   $vector = new \Ds\Vector(['a', 'e', 'i', 'o', 'u']);
   echo "The vector elements are: \n";
   print_r($vector);
   $capacity = 5;
   echo "The capacity need to allocate: ".$capacity;
   $vector->allocate($capacity);
   echo "\nAfter allocating a new capacity is: ";
   print_r($vector->capacity());
?>

Output

On executing the above program, it generates the following output −

The vector elements are:
Ds\Vector Object
(
    [0] => a
    [1] => e
    [2] => i
    [3] => o
    [4] => u
)
The capacity need to allocate: 5
After allocating a new capacity is: 8
php_function_reference.htm
Advertisements