set

Mathematical Set with union, intersection, difference, subset and equality ops.

v1.0.0Data structuresbundledimport "set";
Overview Installation Documentation Examples

Overview

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 offline zero dependencies union intersection

Quick start

import "set";

$a = set_of([1, 2, 3, 2]);
$b = set_of([3, 4, 5]);

println($a->size());                         // 3
println($a->has(2));                         // true
println($a->union($b)->toArray());           // [1, 2, 3, 4, 5]
println($a->intersection($b)->toArray());    // [3]
println($a->difference($b)->toArray());      // [1, 2]
println($a->symmetricDifference($b)->toArray()); // [1, 2, 4, 5]
println(set_of([1, 2])->isSubsetOf($a));     // true
println($a->equals(set_of([3, 1, 2])));      // true

Installation

In a project

Inside a project (a folder with hphp.yaml, see Projects & packages):

hphp install set

This adds the library to hphp.yaml, installs it into .hphp/packages/set/ and pins the exact version in hphp.lock:

require:
  set: "^1.0"

Use it

import "set";

set is bundled with the compiler, so import "set"; also works in a single file without installing anything, and hphp install works offline.

Outside a project

hphp install set          # for your user, shared by all scripts
hphp create myapp                 # or start a project, then: hphp install set
Packageset
Version1.0.0 (follows the compiler)
Entry fileset.hphp
Sourcepackages/lib/set ↗

Documentation

API Reference

Helper Functions

class HashSet

(Named HashSet because Set is a reserved built-in generic type in HolyPHP.)

Error Handling

Invalid inputs throw an Exception with a descriptive set:-prefixed message:

Examples

01Quick start

Bundled with the compiler — import "set"; works offline in any file, or add it to a project with hphp install set.

import "set";

$a = set_of([1, 2, 3, 2]);
$b = set_of([3, 4, 5]);

println($a->size());                         // 3
println($a->has(2));                         // true
println($a->union($b)->toArray());           // [1, 2, 3, 4, 5]
println($a->intersection($b)->toArray());    // [3]
println($a->difference($b)->toArray());      // [1, 2]
println($a->symmetricDifference($b)->toArray()); // [1, 2, 4, 5]
println(set_of([1, 2])->isSubsetOf($a));     // true
println($a->equals(set_of([3, 1, 2])));      // true
Try it
hphp install set
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_set.hphp.