Data Structures

目次

The Collection interface

はじめに

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.

インターフェイス概要

Ds\Collection extends Countable IteratorAggregate JsonSerializable
/* メソッド */
public void Ds\Collection::clear()
public Ds\Collection Ds\Collection::copy()
public bool Ds\Collection::isEmpty()
public array Ds\Collection::toArray()
/* 継承したメソッド */
public int Countable::count()
public Traversable IteratorAggregate::getIterator()
public mixed JsonSerializable::jsonSerialize()

変更履歴

バージョン 説明
PECL ds 1.4.0 Collection implements IteratorAggregate now instead of just Traversable. (This change came to the polyfill in 1.4.1.)

The Hashable interface

はじめに

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.

インターフェイス概要

Ds\Hashable
/* メソッド */
abstract public bool Ds\Hashable::equals(object $obj)
abstract public mixed Ds\Hashable::hash()

The Sequence interface

はじめに

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.

インターフェイス概要

Ds\Sequence extends Ds\Collection ArrayAccess
/* メソッド */
abstract public void Ds\Sequence::allocate(int $capacity)
abstract public void Ds\Sequence::apply(callable $callback)
abstract public int Ds\Sequence::capacity()
abstract public bool Ds\Sequence::contains(mixed ...$values)
abstract public Ds\Sequence Ds\Sequence::filter(callable $callback = ?)
abstract public mixed Ds\Sequence::find(mixed $value)
abstract public mixed Ds\Sequence::first()
abstract public mixed Ds\Sequence::get(int $index)
abstract public void Ds\Sequence::insert(int $index, mixed ...$values)
abstract public string Ds\Sequence::join(string $glue = ?)
abstract public mixed Ds\Sequence::last()
abstract public Ds\Sequence Ds\Sequence::map(callable $callback)
abstract public Ds\Sequence Ds\Sequence::merge(mixed $values)
abstract public mixed Ds\Sequence::pop()
abstract public void Ds\Sequence::push(mixed ...$values)
abstract public mixed Ds\Sequence::reduce(callable $callback, mixed $initial = ?)
abstract public mixed Ds\Sequence::remove(int $index)
abstract public void Ds\Sequence::reverse()
abstract public Ds\Sequence Ds\Sequence::reversed()
abstract public void Ds\Sequence::rotate(int $rotations)
abstract public void Ds\Sequence::set(int $index, mixed $value)
abstract public mixed Ds\Sequence::shift()
abstract public Ds\Sequence Ds\Sequence::slice(int $index, int $length = ?)
abstract public void Ds\Sequence::sort(callable $comparator = ?)
abstract public Ds\Sequence Ds\Sequence::sorted(callable $comparator = ?)
abstract public intfloat Ds\Sequence::sum()
abstract public void Ds\Sequence::unshift(mixed $values = ?)
/* 継承したメソッド */
public void Ds\Collection::clear()
public Ds\Collection Ds\Collection::copy()
public bool Ds\Collection::isEmpty()
public array Ds\Collection::toArray()
public int Countable::count()
public Traversable IteratorAggregate::getIterator()
public mixed JsonSerializable::jsonSerialize()
public bool ArrayAccess::offsetExists(mixed $offset)
public mixed ArrayAccess::offsetGet(mixed $offset)
public void ArrayAccess::offsetSet(mixed $offset, mixed $value)
public void ArrayAccess::offsetUnset(mixed $offset)

変更履歴

バージョン 説明
PECL ds 1.3.0 The interface now extends ArrayAccess.

The Vector class

はじめに

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).

クラス概要

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

定義済み定数

Ds\Vector::MIN_CAPACITY

変更履歴

バージョン 説明
PECL ds 1.3.0 The class now implements ArrayAccess.

The Deque class

はじめに

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).

クラス概要

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

定義済み定数

Ds\Deque::MIN_CAPACITY

変更履歴

バージョン 説明
PECL ds 1.3.0 The class now implements ArrayAccess.

The Map class

はじめに

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.

クラス概要

Ds\Map
class Ds\Map implements Ds\Collection, ArrayAccess {
/* 定数 */
const int Ds\Map::MIN_CAPACITY = 16;
/* メソッド */
public void allocate(int $capacity)
public void apply(callable $callback)
public int capacity()
public void clear()
public Ds\Map copy()
public Ds\Map diff(Ds\Map $map)
public Ds\Map filter(callable $callback = ?)
public Ds\Pair first()
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()
public Ds\Set keys()
public void ksort(callable $comparator = ?)
public Ds\Map ksorted(callable $comparator = ?)
public Ds\Pair last()
public Ds\Map map(callable $callback)
public Ds\Map merge(mixed $values)
public Ds\Sequence pairs()
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()
public Ds\Map reversed()
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 intfloat sum()
public array toArray()
public Ds\Map union(Ds\Map $map)
public Ds\Sequence values()
public Ds\Map xor(Ds\Map $map)
}

定義済み定数

Ds\Map::MIN_CAPACITY

変更履歴

バージョン 説明
PECL ds 1.3.0 The class now implements ArrayAccess.

The Pair class

はじめに

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

クラス概要

Ds\Pair
class Ds\Pair implements JsonSerializable {
/* メソッド */
public __construct(mixed $key = ?, mixed $value = ?)
public void clear()
public Ds\Pair copy()
public bool isEmpty()
public array toArray()
}

The Set class

はじめに

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.

クラス概要

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

定義済み定数

Ds\Set::MIN_CAPACITY

変更履歴

バージョン 説明
PECL ds 1.3.0 The class now implements ArrayAccess.

The Stack class

はじめに

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.

クラス概要

Ds\Stack
class Ds\Stack implements Ds\Collection, ArrayAccess {
/* メソッド */
public void allocate(int $capacity)
public int capacity()
public void clear()
public Ds\Stack copy()
public bool isEmpty()
public mixed peek()
public mixed pop()
public void push(mixed ...$values)
public array toArray()
}

変更履歴

バージョン 説明
PECL ds 1.3.0 The class now implements ArrayAccess.

The Queue class

はじめに

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.

クラス概要

Ds\Queue
class Ds\Queue implements Ds\Collection, ArrayAccess {
/* 定数 */
const int Ds\Queue::MIN_CAPACITY = 8;
/* メソッド */
public void allocate(int $capacity)
public int capacity()
public void clear()
public Ds\Queue copy()
public bool isEmpty()
public mixed peek()
public mixed pop()
public void push(mixed ...$values)
public array toArray()
}

定義済み定数

Ds\Queue::MIN_CAPACITY

変更履歴

バージョン 説明
PECL ds 1.3.0 The class now implements ArrayAccess.

The PriorityQueue class

はじめに

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.

注意:

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

注意:

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

クラス概要

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

定義済み定数

Ds\PriorityQueue::MIN_CAPACITY