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

PHP - Ds\Queue::allocate() Function



The PHP Ds\Queue::allocate() function is used to allocate memory for the specified capacity of the queue. The "capacity" of a queue refers to the number of elements it can hold.

If the specified capacity is less than the current capacity (e.g., negative), the current capacity remains unchanged, and no new memory will be allocated for the queue. You can check the current capacity of a queue using capacity() function.

Syntax

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

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

Parameters

This function accepts a single parameter, 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\Queue::allocate()

<?php  
   $queue = new \Ds\Queue([1, 2, 3, 4, 5]);
   echo "The queue elements are: \n";
   print_r($queue);
   #using allocate() function
   $queue->allocate(50);
   echo "Capacity of this queue: ".$queue->capacity();
?>

Output

The above program produces the following output −

The queue elements are:
Ds\Queue Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
Capacity of this queue: 64

Example 2

Following is another example of the PHPDs\Queue::allocate()function. We use this function to allocate enough memory 100 for the required capacity −

<?php  
   $queue = new \Ds\Queue(['a', 'e', 'i', 'o', 'u']);
   echo "The queue elements are: \n";
   print_r($queue);
   echo "The current capacity before allocated: ";
   print_r($queue->capacity());
   $new_cap = 100;
   echo "\nThe capacity need to allocate: $new_cap";
   #using allocate() function
   $queue->allocate($new_cap);
   echo "\nThe Capacity after allocated: ".$queue->capacity();
?>

Output

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

The queue elements are:
Ds\Queue Object
(
    [0] => a
    [1] => e
    [2] => i
    [3] => o
    [4] => u
)
The current capacity before allocated: 8
The capacity need to allocate: 100
The Capacity after allocated: 128

Example 3

If the specified capacity is less than or equal to the current capacity, the capacity will remain the same, this function does nothing −

<?php  
   $queue = new \Ds\Queue(["Tutorials", "Point", "India"]);
   echo "The queue elements are: \n";
   print_r($queue);
   echo "The current capacity before allocated: ";
   print_r($queue->capacity());
   $new_cap = -10;
   echo "\nThe capacity need to allocate: $new_cap";
   #using allocate() function
   $queue->allocate($new_cap);
   echo "\nThe Capacity after allocated: ".$queue->capacity();
?>

Output

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

The queue elements are:
Ds\Queue Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)
The current capacity before allocated: 8
The capacity need to allocate: -10
The Capacity after allocated: 8
php_function_reference.htm
Advertisements