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

PHP - Ds Sequence::allocate() Function



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

If the current capacity of the sequence 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 sequence.

Syntax

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

abstract public Ds\Sequence::allocate(int $capacity): void

Parameters

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

  • capacity − The capacity needs to be allocated.

Return value

This function doesn't return any value.

Example 1

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

<?php 
   $seq = new \Ds\Vector([]);
   echo "The sequence elements are: \n";
   print_r($seq);
   #using allocate() function
   $capacity = 30;
   echo "The capacity to be allocated: ".$capacity;
   $seq->allocate($capacity);
   echo "\nThe capacity after allocated: ";
   print_r($seq->capacity());
?>

Output

The above program produces the following output −

The sequence elements are:
Ds\Vector Object
(
)
The capacity to be allocated: 30
The capacity after allocated: 30

Example 2

Following is another example of the PHP Ds\Sequence::allocate() function. We use this function to allocate a new capacity 64 to this sequence ([10, 20, 30, 40, 50]) −

<?php 
   $seq = new \Ds\Stack([10, 20, 30, 40, 50]);
   echo "The sequence elements are: \n";
   print_r($seq);
   echo "The current capacity before allocation: ";
   print_r($seq->capacity());
   $capacity = 64;
   echo "\nThe capacity need to be allocated: ".$capacity;
   #using allocate() function
   $seq->allocate($capacity);
   echo "\nThe capacity after allocated: ";
   print_r($seq->capacity());
?>

Output

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

The sequence elements are:
Ds\Stack Object
(
    [0] => 50
    [1] => 40
    [2] => 30
    [3] => 20
    [4] => 10
)
The current capacity before allocation: 8
The capacity need to be allocated: 64
The capacity after allocated: 64

Example 3

If the allocated capacity is less than the current capacity of the sequence, the old capacity will remain the same −

<?php 
   $seq = new \Ds\Set(['a', 'e', 'i', 'o', 'u']);
   echo "The sequence elements are: \n";
   print_r($seq);
   echo "The current capacity before allocation: ";
   print_r($seq->capacity());
   $capacity = 4;
   echo "\nThe capacity need to be allocated: ".$capacity;
   #using allocate() function
   $seq->allocate($capacity);
   echo "\nThe capacity after allocated: ";
   print_r($seq->capacity());
?>

Output

Once the above program is executed, it displays the following output −

The sequence elements are:
Ds\Set Object
(
    [0] => a
    [1] => e
    [2] => i
    [3] => o
    [4] => u
)
The current capacity before allocation: 8
The capacity need to be allocated: 4
The capacity after allocated: 8
php_function_reference.htm
Advertisements