Data StructuresTable of Contents
The Collection interfaceIntroductionCollection 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 synopsisDs\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 interfaceIntroductionHashable 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 synopsisDs\Hashable
class Ds\Hashable
{
/* Methods */
abstract public bool equals
( object
$obj
)
abstract public mixed hash
( void
)
}The Sequence interfaceIntroductionA 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:
Use cases:
Interface synopsisDs\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 classIntroductionA 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
Weaknesses
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
The Deque classIntroductionA 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: Strengths
Weaknesses
Class synopsisDs\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
The Map classIntroductionA 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
Weaknesses
Class synopsisDs\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
The Pair classIntroductionA pair is used by Ds\Map to pair keys with values. Class synopsisDs\Pair
class Ds\Pair
implements
JsonSerializable
{
/* Methods */
public __construct
([ mixed
}$key
[, mixed $value
]] )The Set classIntroductionA 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
Weaknesses
Class synopsisDs\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
The Stack classIntroductionA 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 synopsisDs\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 classIntroductionA 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 synopsisDs\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
The PriorityQueue classIntroductionA 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.
Class synopsisDs\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
|