Skip to content

Instantly share code, notes, and snippets.

@bastienapp
Last active October 19, 2021 09:04
Show Gist options
  • Save bastienapp/f03f920646c52cdb78059873e5982ef7 to your computer and use it in GitHub Desktop.
Save bastienapp/f03f920646c52cdb78059873e5982ef7 to your computer and use it in GitHub Desktop.

The Collection abstract class :

Collection is the base class which covers functionality common to all the data structures in this library.

property:

  • $container: array - contains the collection values

methods:

  • count - Returns the number of values in the map
  • clear - Removes all values
  • isEmpty - Returns whether the queue is empty

The Queue class (inherits Collection) :

A Queue is a “first in, first out” or “FIFO” collection that only allows access to the value at the front of the queue and iterates in that order, destructively.

methods:

  • peek - Returns the value at the front of the queue
  • pop - Removes and returns the value at the front of the queue
  • push($value) - Pushes a value into the queue

The Sequence class (inherits Collection) :

A Sequence describes the behaviour of values arranged in a single, linear dimension. Some languages refer to this as a "List". It’s similar to an array that uses incremental integer keys, with the exception of a few characteristics:

methods:

  • get($index) - Returns the value for a given index
  • insert($index, $value) - Inserts values at a given index, eg :
$sequence->insert(0, "e"); // [e]
$sequence->insert(1, "f"); // [e, f]
$sequence->insert(2, "g"); // [e, f, g]
$sequence->insert(0, "a"); // [a, e, f, g]

Hint: you should use array_splice:

$original = array( 'a', 'b', 'c', 'd', 'e' );
$inserted = array( 'x' ); // not necessarily an array, see manual quote

array_splice( $original, 3, 0, $inserted ); // splice in at position 3
// $original is now a b c x d e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment