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, ArrayAccess {
/* Constants */
const int Ds\Set::MIN_CAPACITY = 16;
/* Methods */
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 map(callable $callback)
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)
}

Predefined Constants

Ds\Set::MIN_CAPACITY

Changelog

Version Description
PECL ds 1.3.0 The class now implements ArrayAccess.
PECL ds 1.2.7 Added the Ds\Set::map method.

Table of Contents