Data Structures

Table of Contents

The Collection interface

Introduction

Collection is the base interface which covers functionality common to all the data structures in this library. It guarantees that all structures are traversable, countable, and can be converted to json using json_encode.

Interface synopsis

Ds\Collection
class Ds\Collection implements Traversable , Countable , JsonSerializable {
/* Methods */
abstract public void clear ( void )
abstract public Ds\Collection copy ( void )
abstract public bool isEmpty ( void )
abstract public array toArray ( void )
}

The Hashable interface

Introduction

Hashable is an interface which allows objects to be used as keys. It’s an alternative to spl_object_hash, which determines an object’s hash based on its handle: this means that two objects that are considered equal by an implicit definition would not treated as equal because they are not the same instance.

hash is used to return a scalar value to be used as the object's hash value, which determines where it goes in the hash table. While this value does not have to be unique, objects which are equal must have the same hash value.

equals is used to determine if two objects are equal. It's guaranteed that the comparing object will be an instance of the same class as the subject.

Interface synopsis

Ds\Hashable
class Ds\Hashable {
/* Methods */
abstract public bool equals ( object $obj )
abstract public mixed hash ( void )
}

The Sequence interface

Introduction

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:

  • Values will always be indexed as [0, 1, 2, …, size - 1].
  • Only allowed to access values by index in the range [0, size - 1].

Use cases:

  • Wherever you would use an array as a list (not concerned with keys).
  • A more efficient alternative to SplDoublyLinkedList and SplFixedArray.

Interface synopsis

Ds\Sequence
class Ds\Sequence implements Ds\Collection {
/* Methods */
abstract public void allocate ( int $capacity )
abstract public void apply ( callable $callback )
abstract public int capacity ( void )
abstract public bool contains ([ mixed $...values ] )
abstract public Ds\Sequence filter ([ callable $callback ] )
abstract public mixed find ( mixed $value )
abstract public mixed first ( void )
abstract public mixed get ( int $index )
abstract public void insert ( int $index [, mixed $...values ] )
abstract public string join ([ string $glue ] )
abstract public mixed last ( void )
abstract public Ds\Sequence map ( callable $callback )
abstract public Ds\Sequence merge ( mixed $values )
abstract public mixed pop ( void )
abstract public void push ([ mixed $...values ] )
abstract public mixed reduce ( callable $callback [, mixed $initial ] )
abstract public mixed remove ( int $index )
abstract public void reverse ( void )
abstract public Ds\Sequence reversed ( void )
abstract public void rotate ( int $rotations )
abstract public void set ( int $index , mixed $value )
abstract public mixed shift ( void )
abstract public Ds\Sequence slice ( int $index [, int $length ] )
abstract public void sort ([ callable $comparator ] )
abstract public Ds\Sequence sorted ([ callable $comparator ] )
abstract public number sum ( void )
abstract public void unshift ([ mixed $values ] )
}

The Vector class

Introduction

A Vector is a sequence of values in a contiguous buffer that grows and shrinks automatically. It’s the most efficient sequential structure because a value’s index is a direct mapping to its index in the buffer, and the growth factor isn't bound to a specific multiple or exponent.

Strengths

  • Supports array syntax (square brackets).
  • Uses less overall memory than an array for the same number of values.
  • Automatically frees allocated memory when its size drops low enough.
  • Capacity does not have to be a power of 2.
  • get, set, push, pop are all O(1).

Weaknesses

  • shift, unshift, insert and remove are all O(n).

Class synopsis

Ds\Vector
class Ds\Vector implements Ds\Sequence {
/* Constants */
const int Ds\Vector::MIN_CAPACITY = 10 ;
/* Methods */
public void allocate ( int $capacity )
public void apply ( callable $callback )
public int capacity ( void )
public void clear ( void )
public bool contains ([ mixed $...values ] )
public Ds\Vector copy ( void )
public Ds\Vector filter ([ callable $callback ] )
public mixed find ( mixed $value )
public mixed first ( void )
public mixed get ( int $index )
public void insert ( int $index [, mixed $...values ] )
public bool isEmpty ( void )
public string join ([ string $glue ] )
public mixed last ( void )
public Ds\Vector map ( callable $callback )
public Ds\Vector merge ( mixed $values )
public mixed pop ( void )
public void push ([ mixed $...values ] )
public mixed reduce ( callable $callback [, mixed $initial ] )
public mixed remove ( int $index )
public void reverse ( void )
public Ds\Vector reversed ( void )
public void rotate ( int $rotations )
public void set ( int $index , mixed $value )
public mixed shift ( void )
public Ds\Vector slice ( int $index [, int $length ] )
public void sort ([ callable $comparator ] )
public Ds\Vector sorted ([ callable $comparator ] )
public number sum ( void )
public array toArray ( void )
public void unshift ([ mixed $values ] )
}

Predefined Constants

Ds\Vector::MIN_CAPACITY

The Deque class

Introduction

A Deque (pronounced “deck”) is a sequence of values in a contiguous buffer that grows and shrinks automatically. The name is a common abbreviation of “double-ended queue” and is used internally by Ds\Queue.

Two pointers are used to keep track of a head and a tail. The pointers can “wrap around” the end of the buffer, which avoids the need to move other values around to make room. This makes shift and unshift very fast —  something a Ds\Vector can’t compete with.

Accessing a value by index requires a translation between the index and its corresponding position in the buffer: ((head + position) % capacity).

Strengths

  • Supports array syntax (square brackets).
  • Uses less overall memory than an array for the same number of values.
  • Automatically frees allocated memory when its size drops low enough.
  • get, set, push, pop, shift, and unshift are all O(1).

Weaknesses

  • Capacity must be a power of 2.
  • insert and remove are O(n).

Class synopsis

Ds\Deque
class Ds\Deque implements Ds\Sequence {
/* Constants */
const int Ds\Deque::MIN_CAPACITY = 8 ;
/* Methods */
public void allocate ( int $capacity )
public void apply ( callable $callback )
public int capacity ( void )
public void clear ( void )
public bool contains ([ mixed $...values ] )
public Ds\Deque copy ( void )
public Ds\Deque filter ([ callable $callback ] )
public mixed find ( mixed $value )
public mixed first ( void )
public mixed get ( int $index )
public void insert ( int $index [, mixed $...values ] )
public bool isEmpty ( void )
public string join ([ string $glue ] )
public mixed last ( void )
public Ds\Deque map ( callable $callback )
public Ds\Deque merge ( mixed $values )
public mixed pop ( void )
public void push ([ mixed $...values ] )
public mixed reduce ( callable $callback [, mixed $initial ] )
public mixed remove ( int $index )
public void reverse ( void )
public Ds\Deque reversed ( void )
public void rotate ( int $rotations )
public void set ( int $index , mixed $value )
public mixed shift ( void )
public Ds\Deque slice ( int $index [, int $length ] )
public void sort ([ callable $comparator ] )
public Ds\Deque sorted ([ callable $comparator ] )
public number sum ( void )
public array toArray ( void )
public void unshift ([ mixed $values ] )
}

Predefined Constants

Ds\Deque::MIN_CAPACITY

The Map class

Introduction

A Map is a sequential collection of key-value pairs, almost identical to an array used in a similar context. Keys can be any type, but must be unique. Values are replaced if added to the map using the same key.

Strengths

  • Keys and values can be any type, including objects.
  • Supports array syntax (square brackets).
  • Insertion order is preserved.
  • Performance and memory efficiency is very similar to an array.
  • Automatically frees allocated memory when its size drops low enough.

Weaknesses

  • Can’t be converted to an array when objects are used as keys.

Class synopsis

Ds\Map
class Ds\Map implements Ds\Collection {
/* Constants */
const int Ds\Map::MIN_CAPACITY = 16 ;
/* Methods */
public void allocate ( int $capacity )
public void apply ( callable $callback )
public int capacity ( void )
public void clear ( void )
public Ds\Map copy ( void )
public Ds\Map diff ( Ds\Map $map )
public Ds\Map filter ([ callable $callback ] )
public Ds\Pair first ( void )
public mixed get ( mixed $key [, mixed $default ] )
public bool hasKey ( mixed $key )
public bool hasValue ( mixed $value )
public Ds\Map intersect ( Ds\Map $map )
public bool isEmpty ( void )
public Ds\Set keys ( void )
public void ksort ([ callable $comparator ] )
public Ds\Map ksorted ([ callable $comparator ] )
public Ds\Pair last ( void )
public Ds\Map map ( callable $callback )
public Ds\Map merge ( mixed $values )
public Ds\Sequence pairs ( void )
public void put ( mixed $key , mixed $value )
public void putAll ( mixed $pairs )
public mixed reduce ( callable $callback [, mixed $initial ] )
public mixed remove ( mixed $key [, mixed $default ] )
public void reverse ( void )
public Ds\Map reversed ( void )
public Ds\Pair skip ( int $position )
public Ds\Map slice ( int $index [, int $length ] )
public void sort ([ callable $comparator ] )
public Ds\Map sorted ([ callable $comparator ] )
public number sum ( void )
public array toArray ( void )
public Ds\Map union ( Ds\Map $map )
public Ds\Sequence values ( void )
public Ds\Map xor ( Ds\Map $map )
}

Predefined Constants

Ds\Map::MIN_CAPACITY

The Pair class

Introduction

A pair is used by Ds\Map to pair keys with values.

Class synopsis

Ds\Pair
class Ds\Pair implements JsonSerializable {
/* Methods */
public __construct ([ mixed $key [, mixed $value ]] )
}

The Set class

Introduction

A Set is a sequence of unique values. This implementation uses the same hash table as Ds\Map, where values are used as keys and the mapped value is ignored.

Strengths

  • Values can be any type, including objects.
  • Supports array syntax (square brackets).
  • Insertion order is preserved.
  • Automatically frees allocated memory when its size drops low enough.
  • add, remove and contains are all O(1).

Weaknesses

  • Doesn’t support push, pop, insert, shift, or unshift.
  • get is O(n) if there are deleted values in the buffer before the accessed index, O(1) otherwise.

Class synopsis

Ds\Set
class Ds\Set implements Ds\Collection {
/* Constants */
const int Ds\Set::MIN_CAPACITY = 16 ;
/* Methods */
public void add ([ mixed $...values ] )
public void allocate ( int $capacity )
public int capacity ( void )
public void clear ( void )
public bool contains ([ mixed $...values ] )
public Ds\Set copy ( void )
public Ds\Set diff ( Ds\Set $set )
public Ds\Set filter ([ callable $callback ] )
public void first ( void )
public mixed get ( int $index )
public Ds\Set intersect ( Ds\Set $set )
public bool isEmpty ( void )
public string join ([ string $glue ] )
public void last ( void )
public Ds\Set merge ( mixed $values )
public mixed reduce ( callable $callback [, mixed $initial ] )
public void remove ([ mixed $...values ] )
public void reverse ( void )
public Ds\Set reversed ( void )
public Ds\Set slice ( int $index [, int $length ] )
public void sort ([ callable $comparator ] )
public Ds\Set sorted ([ callable $comparator ] )
public number sum ( void )
public array toArray ( void )
public Ds\Set union ( Ds\Set $set )
public Ds\Set xor ( Ds\Set $set )
}

Predefined Constants

Ds\Set::MIN_CAPACITY

The Stack class

Introduction

A Stack is a “last in, first out” or “LIFO” collection that only allows access to the value at the top of the structure and iterates in that order, destructively.

Uses a Ds\Vector internally.

Class synopsis

Ds\Stack
class Ds\Stack implements Ds\Collection {
/* Methods */
public void allocate ( int $capacity )
public int capacity ( void )
public void clear ( void )
public Ds\Stack copy ( void )
public bool isEmpty ( void )
public mixed peek ( void )
public mixed pop ( void )
public void push ([ mixed $...values ] )
public array toArray ( void )
}

The Queue class

Introduction

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.

Class synopsis

Ds\Queue
class Ds\Queue implements Ds\Collection {
/* Constants */
const int Ds\Queue::MIN_CAPACITY = 8 ;
/* Methods */
public void allocate ( int $capacity )
public int capacity ( void )
public void clear ( void )
public Ds\Queue copy ( void )
public bool isEmpty ( void )
public mixed peek ( void )
public mixed pop ( void )
public void push ([ mixed $...values ] )
public array toArray ( void )
}

Predefined Constants

Ds\Queue::MIN_CAPACITY

The PriorityQueue class

Introduction

A PriorityQueue is very similar to a Queue. Values are pushed into the queue with an assigned priority, and the value with the highest priority will always be at the front of the queue.

Implemented using a max heap.

Note:

"First in, first out" ordering is preserved for values with the same priority.

Note:

Iterating over a PriorityQueue is destructive, equivalent to successive pop operations until the queue is empty.

Class synopsis

Ds\PriorityQueue
class Ds\PriorityQueue implements Ds\Collection {
/* Constants */
const int Ds\PriorityQueue::MIN_CAPACITY = 8 ;
/* Methods */
public void allocate ( int $capacity )
public int capacity ( void )
public void clear ( void )
public Ds\PriorityQueue copy ( void )
public bool isEmpty ( void )
public mixed peek ( void )
public mixed pop ( void )
public void push ( mixed $value , int $priority )
public array toArray ( void )
}

Predefined Constants

Ds\PriorityQueue::MIN_CAPACITY