The ArrayDeque class

(Available since version 1.0)

Introduction

The ArrayDeque class builds on the functionality of SEIDS::Arrays::Dynamic::DynamicArray (which itself builds on SplFixedArray), providing an array that can grow (and shrink) from either end.

The array is grown simply by adding an element to either the beginning or the end of the array.

The API is the same as SplFixedArray but with the following extra methods:

Class synopsis

SEIDS::Arrays::Dynamic::ArrayDeque extends SEIDS::Arrays::Dynamic::DynamicArray {
/* Properties */
protected int $start ;
protected int $current_key ;
/* Inherited properties */
protected SplFixedArray $data ;
protected int $count ;
/* Methods */
public __construct ([ int $size = 0 ] )
public mixed current ( void )
public int key ( void )
public void next ( void )
public bool offsetExists ( int $index )
public mixed offsetGet ( int $index )
public void offsetSet ( int $index , mixed $newval )
public void offsetUnset ( int $index )
public void rewind ( void )
public mixed shift ( void )
public array toArray ( void )
public void unshift ( mixed $value )
public bool valid ( void )
protected int map ( int $index )
protected bool offsetValid ( int $index )
protected void resize ( int $size [, int $count ] )
/* Inherited methods */
public void DynamicArray::__clone ( void )
public int DynamicArray::count ( void )
public static SEIDS::Arrays::Dynamic::DynamicArray DynamicArray::fromArray ( array $array [, bool $save_indexes = true ] )
public int DynamicArray::getAllocatedSize ( void )
public int DynamicArray::getSize ( void )
public bool DynamicArray::isEmpty ( void )
public mixed DynamicArray::pop ( void )
public void DynamicArray::push ( mixed $value )
public int DynamicArray::setSize ( int $size )
public void DynamicArray::__wakeup ( void )
protected void DynamicArray::grow ( void )
protected void DynamicArray::shrink ( void )
}

Examples

Example #1 ArrayDeque usage example

<?php
require 'vendor/autoload.php'// A PSR-4 or PSR-0 autoloader
use SEIDS\Arrays\Dynamic\ArrayDeque;

// Initialize the array with an initial length (optional)
$array = new ArrayDeque(5);

$array[1] = 2;
$array[4] = "foo";

var_dump($array[0]); // NULL
var_dump($array[1]); // int(2)

var_dump($array["4"]); // string(3) "foo"

// Increase the size of the array to 10
$array->setSize(10);

$array[9] = "asdf";

// Shrink the array to a size of 2
$array->setSize(2);

// Add an element to the start
$array->unshift("first");

// Add two elements to the end
$array->push("second to last");
$array[] = "last";

var_dump($array->toArray());

// The following lines throw a RuntimeException: Index invalid or out of range
try {
    
var_dump($array["non-numeric"]);
} catch(
RuntimeException $re) {
    echo 
"RuntimeException: ".$re->getMessage()."\n";
}

try {
    
var_dump($array[-1]);
} catch(
RuntimeException $re) {
    echo 
"RuntimeException: ".$re->getMessage()."\n";
}

try {
    
var_dump($array[5]);
} catch(
RuntimeException $re) {
    echo 
"RuntimeException: ".$re->getMessage()."\n";
}
?>

The above example will output:

NULL
int(2)
string(3) "foo"
array(5) {
  [0] =>
  string(5) "first"
  [1] =>
  NULL
  [2] =>
  int(2)
  [3] =>
  string(14) "second to last"
  [4] =>
  string(4) "last"
}
RuntimeException: Index invalid or out of range
RuntimeException: Index invalid or out of range
RuntimeException: Index invalid or out of range

Properties

start

The index of the start of the external array within the internal array.

current_key

The current array index.

Table of Contents

To Top