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

PHP - Ds Sequence::capacity() Function



The PHP Ds\Sequence::capacity() function is used to retrieve the current capacity of a sequence. The"capacity"of the sequence is the memory or the number of elements that the sequence can occupy.

The PHP Ds\Sequence class provides another function named allocate(), which is used to allocate a enough capacity to the current sequence.

Syntax

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

abstract public Ds\Sequence::capacity(): int

Parameters

This function does not accept any parameter.

Return value

This function returns the current capacity of a sequence.

Example 1

The following program demonstrates the usage of the PHP Ds\Sequence::capacity() function −

<?php 
   $seq = new \Ds\Vector([1, 2, 3]);
   echo "The sequence elements are: \n";
   print_r($seq);
   echo "The current capacity of a sequence is: ";
   #using capacity() function
   print_r($seq->capacity());
?>

Output

The above program generates the following output −

The sequence elements are:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
)
The current capacity of a sequence is: 8

Example 2

Following is another example of the PHP Ds\Sequence::capacity() function. We use this function to retrieve the current capacity of this sequence (["Tutorials", "Point", "India"]) −

<?php 
   $seq = new \Ds\Stack(["Tutorials", "Point", "India"]);
   echo "The sequence elements are: \n";
   print_r($seq);
   echo "The capacity before allocating: ";
   #using capacity() function
   print($seq->capacity());
   #using allocate() function
   $seq->allocate(32);
   echo "\nThe capacity after allocating: ";
   print($seq->capacity());
?>

Output

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

The sequence elements are:
Ds\Stack Object
(
    [0] => India
    [1] => Point
    [2] => Tutorials
)
The capacity before allocating: 8
The capacity after allocating: 32
php_function_reference.htm
Advertisements