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

PHP - Ds Deque::slice() Function



ThePHP Ds\Deque::slice()function retrieves a new sub-deque from the current deque within a specified range. The sub-deque is a part of the original deque. For example, deque {1, 2} is a sub-deque of this original deque {1, 2, 3}.

This function takestwo parameters (both are required)to define the range from which the elements should be extracted. The elements atboth positionswill be included in the new sub-deque.

Syntax

Following is the syntax of the PHP Ds\Deque::slice() function −

public Ds\Deque::slice(int $index, int $length = ?): Ds\Deque

Parameters

Following are the parameters of this function −

  • index − An integer value that specifies the index at which the sub-deque starts.
  • length − An integer value that specifies the length of sub-deque.

Return value

This function returns a sub-deque of the given range.

Example 1

The following program demonstrates the usage of the PHP Ds\Deque::slice() function −

<?php
   $deque = new \Ds\Deque([1, 2, 3, 4, 5]);
   echo "The original deque: \n";
   print_r($deque);
   $start = 1;
   $end = 4;
   echo "The given range is: (".$start.", ".$end.")";
   echo "\nThe sub-deque of given range is: \n";
   print_r($deque->slice($start, $end));
?>

Output

The above program produces the following output −

The original deque:
Ds\Deque Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
The given range is: (1, 4)
The sub-deque of given range is:
Ds\Deque Object
(
    [0] => 2
    [1] => 3
    [2] => 4
    [3] => 5
)

Example 2

Following is another example of theslice()function. We use this function to create a new sub-deque within the specified range of (0, 2) of this deque (['a', 'e', 'i', 'o', 'u']) −

<?php
   $deque = new \Ds\Deque(['a', 'e', 'i', 'o', 'u']);
   echo "The original deque: \n";
   print_r($deque);
   $start = 0;
   $end = 2;
   echo "The given range is: (".$start.", ".$end.")";
   echo "\nThe sub-deque of given range is: \n";
   print_r($deque->slice($start, $end));
?>

Output

On executing the above program, it will display the following output −

The original deque:
Ds\Deque Object
(
    [0] => a
    [1] => e
    [2] => i
    [3] => o
    [4] => u
)
The given range is: (0, 2)
The sub-deque of given range is:
Ds\Deque Object
(
    [0] => a
    [1] => e
)

Example 3

If the "length" parameter is omitted and provides only the "start" parameter, this function returns a sub-deque from the range between the start and till end of the original deque.

<?php
   $deque = new \Ds\Deque(["Tutorials", "Point", "Hyderabad", "India"]);
   echo "The original deque: \n";
   print_r($deque);
   $start = 1;
   echo "The start value is: ".$start;
   echo "\nThe sub-deque of given range is: \n";
   print_r($deque->slice($start));
?>

Output

On executing the above program, it will display the following output −

The original deque:
Ds\Deque Object
(
    [0] => Tutorials
    [1] => Point
    [2] => Hyderabad
    [3] => India
)
The start value is: 1
The sub-deque of given range is:
Ds\Deque Object
(
    [0] => Point
    [1] => Hyderabad
    [2] => India
)

Example 4

If the given "start" parameter value is negative, this function will slice that many elements from the end of the original deque as follows:

<?php
   $deque = new \Ds\Deque([10, 20, 30, 40, 50]);
   echo "The original deque: \n";
   print_r($deque);
   $start = -2;
   echo "The start value is: ".$start;
   echo "\nThe sub-deque of given range is: \n";
   print_r($deque->slice($start));
?>

Output

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

The original deque:
Ds\Deque Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)
The start value is: -2
The sub-deque of given range is:
Ds\Deque Object
(
    [0] => 40
    [1] => 50
)
Advertisements