A zero-dependency mathematical Set data structure for HolyPHP. Stores unique values with strict type distinction (e.g., 1, "1", and true are distinct elements) and provides standard set theory operations.
bundled — works offlinezero dependenciesunionintersection
set_of(array $items = []): HashSet Creates a new HashSet initialized with the elements of $items. Duplicates are automatically removed.
set_key($value): string Returns a type-tagged canonical string key for $value (null, bool, int, float, string, or array).
class HashSet
(Named HashSet because Set is a reserved built-in generic type in HolyPHP.)
new HashSet(array $items = []) Constructs a new HashSet optionally populated with $items.
add($value): HashSet Adds $value to the set if not already present. Returns $this for method chaining.
remove($value): bool Removes $value from the set. Returns true if the element was present and removed, or false otherwise.
has($value): bool Returns true if $value is in the set, false otherwise.
size(): int Returns the number of unique elements in the set.
isEmpty(): bool Returns true if the set contains no elements.
clear(): HashSet Removes all elements from the set. Returns $this.
toArray(): array Returns a 0-indexed array of all elements in insertion order.
union(HashSet $other): HashSet Returns a new HashSet containing all elements present in either $this or $other.
intersection(HashSet $other): HashSet Returns a new HashSet containing only elements present in both $this and $other.
difference(HashSet $other): HashSet Returns a new HashSet containing elements present in $this but not in $other.
symmetricDifference(HashSet $other): HashSet Returns a new HashSet containing elements present in either $this or $other, but not both.
isSubsetOf(HashSet $other): bool Returns true if every element of $this is also in $other.
isSupersetOf(HashSet $other): bool Returns true if every element of $other is also in $this.
equals(HashSet $other): bool Returns true if $this and $other contain the exact same elements.
Error Handling
Invalid inputs throw an Exception with a descriptive set:-prefixed message:
Passing a non-array to new HashSet(...) throws: set: constructor expects an array of items
Passing a non-array to set_of(...) throws: set: set_of() expects an array of items
Passing an unsupported value type (such as an object) to add(), remove(), or has() throws: set: unsupported value type; expected null, bool, int, float, string, or array
Examples
01Quick start
Bundled with the compiler — import "set"; works offline in any file, or add it to a project with hphp install set.