class Deque implements Sequence (View source)

Methods

__construct(TValue ...$values)

Creates a new instance, using either a traversable object or an array for the initial values.

int
count()

Count elements of an object

void
clear()

Removes all values from the deque.

TValue>
copy()

Returns a shallow copy of the deque.

getIterator()

No description

bool
isEmpty()

Returns whether the deque is empty.

array
toArray()

Converts the deque to an array.

void
allocate(int $capacity)

Ensures that enough memory is allocated for a required capacity.

void
apply(callable $callback)

Updates all values by applying a callback function to each value in the deque.

int
capacity()

Returns the current capacity.

bool
contains(TValue ...$values)

Determines if the deque contains all values.

TValue>
filter(callable|null $callback = null)

Creates a new deque using a callable to determine which values to include.

int|false
find(TValue $value)

Returns the index of the value, or FALSE if not found.

TValue
first()

Returns the first value in the deque.

TValue
get(int $index)

Returns the value at a given index.

void
insert(int $index, TValue ...$values)

Inserts values into the deque at a given index.

string
join(string $glue = '')

Joins all values together as a string using an optional separator between each value.

TValue
last()

Returns the last value in the deque.

map(callable $callback)

Returns the result of applying a callback function to each value in the deque.

merge(TValue2> $values)

Returns the result of adding all given values to the deque.

TValue
pop()

Removes and returns the last value.

void
push(TValue ...$values)

Adds values to the end of the deque.

TCarry
reduce(callable $callback, TCarry $initial = null)

Reduces the deque to a single value using a callback function.

TValue
remove(int $index)

Removes and returns a value by index.

void
reverse()

Reverses the deque in-place.

TValue>
reversed()

Returns a reversed copy of the deque.

void
rotate(int $rotations)

Rotates the deque by a given number of rotations, which is equivalent to successively calling $deque->push($deque->shift()) if the number of rotations is positive, or $deque->unshift($deque->pop()) if negative.

void
set(int $index, TValue $value)

Updates a value at a given index.

TValue
shift()

Removes and returns the first value.

TValue>
slice(int $index, int $length = null)

Creates a sub-deque of a given range.

void
sort(callable|null $comparator = null)

Sorts the deque in-place, using an optional comparator function.

TValue>
sorted(callable|null $comparator = null)

Returns a sorted copy, using an optional comparator function.

float|int
sum()

Returns the sum of all values in the deque.

void
unshift(TValue ...$values)

Adds values to the front of the deque, moving all the current values forward to make room for the new values.

mixed
jsonSerialize()

Specify data which should be serialized to JSON

bool
offsetExists(TKey $offset)

No description

mixed
offsetGet(TKey $offset)

No description

void
offsetSet(TKey $offset, TValue $value)

No description

void
offsetUnset(TKey $offset)

No description

Details

__construct(TValue ...$values)

Creates a new instance, using either a traversable object or an array for the initial values.

Parameters

TValue ...$values

A traversable object or an array to use for the initial values.

int count()

Since: 5.1

Count elements of an object

Return Value

int

The custom count as an integer.

The return value is cast to an integer.

void clear()

Removes all values from the deque.

Return Value

void

TValue> copy()

Returns a shallow copy of the deque.

Return Value

TValue>

Traversable getIterator()

No description

Return Value

Traversable

An instance of an object implementing Iterator or Traversable

bool isEmpty()

Returns whether the deque is empty.

Return Value

bool

array toArray()

Converts the deque to an array.

Note: Casting to an array is not supported yet.

Return Value

array

An array containing all the values in the same order as the collection.

void allocate(int $capacity)

Ensures that enough memory is allocated for a required capacity.

This removes the need to reallocate the internal as values are added.

Parameters

int $capacity

The number of values for which capacity should be allocated.

Note: Capacity will stay the same if this value is less than or equal to the current capacity.

Return Value

void

void apply(callable $callback)

Updates all values by applying a callback function to each value in the deque.

Parameters

callable $callback

Return Value

void

int capacity()

Returns the current capacity.

Return Value

int

The current capacity.

bool contains(TValue ...$values)

Determines if the deque contains all values.

Parameters

TValue ...$values

Values to check.

Return Value

bool

FALSE if any of the provided values are not in the sequence, TRUE otherwise.

TValue> filter(callable|null $callback = null)

Creates a new deque using a callable to determine which values to include.

Parameters

callable|null $callback

Return Value

TValue>

A new sequence containing all the values for which either the callback returned TRUE, or all values that convert to TRUE if a callback was not provided.

int|false find(TValue $value)

Returns the index of the value, or FALSE if not found.

Parameters

TValue $value

The value to find.

Return Value

int|false

The index of the value, or FALSE if not found.

TValue first()

Returns the first value in the deque.

Return Value

TValue

The first value in the sequence.

Exceptions

UnderflowException

TValue get(int $index)

Returns the value at a given index.

Parameters

int $index

The index to access, starting at 0.

Return Value

TValue

The value at the requested index.

Exceptions

OutOfRangeException

void insert(int $index, TValue ...$values)

Inserts values into the deque at a given index.

Parameters

int $index

The index at which to insert. 0 <= index <= count

Note: You can insert at the index equal to the number of values.

TValue ...$values

The value or values to insert.

Return Value

void

Exceptions

OutOfRangeException

string join(string $glue = '')

Joins all values together as a string using an optional separator between each value.

Parameters

string $glue

An optional string to separate each value.

Return Value

string

All values of the sequence joined together as a string.

TValue last()

Returns the last value in the deque.

Return Value

TValue

The last value in the sequence.

Exceptions

UnderflowException

Sequence map(callable $callback)

Returns the result of applying a callback function to each value in the deque.

Parameters

callable $callback

Return Value

Sequence

The result of applying a callback to each value in the sequence.

Note: The values of the current instance won't be affected.

Sequence merge(TValue2> $values)

Returns the result of adding all given values to the deque.

Parameters

TValue2> $values

A traversable object or an array.

Return Value

Sequence

The result of adding all given values to the sequence, effectively the same as adding the values to a copy, then returning that copy.

TValue pop()

Removes and returns the last value.

Return Value

TValue

The removed last value.

Exceptions

UnderflowException

void push(TValue ...$values)

Adds values to the end of the deque.

Parameters

TValue ...$values

The values to add.

Return Value

void

TCarry reduce(callable $callback, TCarry $initial = null)

Reduces the deque to a single value using a callback function.

Parameters

callable $callback
TCarry $initial

The initial value of the carry value. Can be NULL.

Return Value

TCarry

The return value of the final callback.

TValue remove(int $index)

Removes and returns a value by index.

Parameters

int $index

The index of the value to remove.

Return Value

TValue

The value that was removed.

void reverse()

Reverses the deque in-place.

Return Value

void

TValue> reversed()

Returns a reversed copy of the deque.

Return Value

TValue>

A reversed copy of the sequence.

Note: The current instance is not affected.

void rotate(int $rotations)

Rotates the deque by a given number of rotations, which is equivalent to successively calling $deque->push($deque->shift()) if the number of rotations is positive, or $deque->unshift($deque->pop()) if negative.

Parameters

int $rotations

The number of times the sequence should be rotated.

Return Value

void

void set(int $index, TValue $value)

Updates a value at a given index.

Parameters

int $index

The index of the value to update.

TValue $value

The new value.

Return Value

void

Exceptions

OutOfRangeException

TValue shift()

Removes and returns the first value.

Return Value

TValue

Exceptions

UnderflowException

TValue> slice(int $index, int $length = null)

Creates a sub-deque of a given range.

Parameters

int $index

The index at which the sub-sequence starts. If positive, the sequence will start at that index in the sequence. If negative, the sequence will start that far from the end.

int $length

If a length is given and is positive, the resulting sequence will have up to that many values in it. If the length results in an overflow, only values up to the end of the sequence will be included. If a length is given and is negative, the sequence will stop that many values from the end. If a length is not provided, the resulting sequence will contain all values between the index and the end of the sequence.

Return Value

TValue>

A sub-sequence of the given range.

void sort(callable|null $comparator = null)

Sorts the deque in-place, using an optional comparator function.

Parameters

callable|null $comparator

Return Value

void

TValue> sorted(callable|null $comparator = null)

Returns a sorted copy, using an optional comparator function.

Parameters

callable|null $comparator

Return Value

TValue>

Returns a sorted copy of the sequence.

float|int sum()

Returns the sum of all values in the deque.

Note: Arrays and objects are considered equal to zero when calculating the sum.

Return Value

float|int

The sum of all the values in the sequence as either a float or int depending on the values in the sequence.

void unshift(TValue ...$values)

Adds values to the front of the deque, moving all the current values forward to make room for the new values.

Parameters

TValue ...$values

The values to add to the front of the sequence.

Note: Multiple values will be added in the same order that they are passed.

Return Value

void

mixed jsonSerialize()

Since: 5.4

Specify data which should be serialized to JSON

Return Value

mixed

data which can be serialized by json_encode, which is a value of any type other than a resource.

bool offsetExists(TKey $offset)

No description

Parameters

TKey $offset

An offset to check for.

Return Value

bool

true on success or false on failure.

The return value will be casted to boolean if non-boolean was returned.

mixed offsetGet(TKey $offset)

No description

Parameters

TKey $offset

The offset to retrieve.

Return Value

mixed

Can return all value types.

void offsetSet(TKey $offset, TValue $value)

No description

Parameters

TKey $offset

The offset to assign the value to.

TValue $value

The value to set.

Return Value

void

void offsetUnset(TKey $offset)

No description

Parameters

TKey $offset

The offset to unset.

Return Value

void