Skip to content

Instantly share code, notes, and snippets.

@morrisonlevi
Last active February 12, 2019 16:59
Show Gist options
  • Save morrisonlevi/3a5d6a1c23c0eed69be52f77cce3d6ed to your computer and use it in GitHub Desktop.
Save morrisonlevi/3a5d6a1c23c0eed69be52f77cce3d6ed to your computer and use it in GitHub Desktop.
Brainstorming the interfaces for Deque, Queue, and Stack.
<?php
namespace Spl;
/**
* Indicates that a structure is designed to operate efficiently at both the
* front and the back of a dataset.
* @template T
*/
interface Deque
extends \Countable, \IteratorAggregate
{
/**
* Prepends the given value to the front of the structure.
* @psalm-param T $value
*/
function push_front($value);
/**
* Removes and returns the first value of the structure.
*
* @psalm-return T The value that was removed from the front of the structure.
*
* @throws EmptyContainerException if the structure is empty.
*/
function pop_front();
/**
* Returns the first value of the structure without removing it.
* @psalm-return T The value at the front of the structure.
* @throws EmptyContainerException if the structure is empty.
*/
function front();
/**
* Appends the given value to the back of the structure.
* @psalm-param T $value
*/
function push_back($value);
/**
* Removes and returns the last value of the structure, the back of the structure.
*
* @psalm-return T The value that was removed from the back of the structure.
*
* @throws EmptyContainerException if the structure is empty.
*/
function pop_back();
/**
* Returns the last value of the structure without removing it.
* @psalm-return T The value at the back of the structure.
* @throws EmptyContainerException if the structure is empty.
*/
function back();
}
<?php
namespace Spl;
/**
* A structure with First-In, Last-Out (FIFO) behavior.
* @template T
*/
interface Queue
extends \Countable, \IteratorAggregate
{
/**
* Appends the given value to the back of the structure.
* @psalm-param T $value
*/
function push_back($value);
/**
* Removes and returns the first value of the structure.
*
* @psalm-return T The value that was removed from the front of the structure.
*
* @throws EmptyContainerException if the structure is empty.
*/
function pop_front();
/**
* Returns the first value of the structure without removing it.
* @psalm-return T The value at the front of the structure.
* @throws EmptyContainerException if the structure is empty.
*/
function front();
}
<?php
namespace Spl;
/**
* A structure with First-In, Last-Out (FIFO) behavior.
* @template T
*/
interface Stack
extends \Countable, \IteratorAggregate
{
/**
* Appends the given value to the back of the structure.
* @psalm-param T $value
*/
function push_back($value);
/**
* Removes and returns the last value of the structure, the back of the structure.
*
* @psalm-return T The value that was removed from the back of the structure.
*
* @throws EmptyContainerException if the structure is empty.
*/
function pop_back();
/**
* Returns the last value of the structure without removing it.
* @psalm-return T The value at the back of the structure.
* @throws EmptyContainerException if the structure is empty.
*/
function back();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment