std.set

Set operations over unique values.

Importuse std.set

Functions

fn from_array<T>(arr: [T]) -> {T: bool}#

The set of arr's elements; duplicates collapse, first occurrence wins the insertion-order slot.

  • arr: Elements to admit, in order.
let seen = std.set.from_array(["ada", "grace", "ada"])
println(std.set.size(seen))     // 2
println(std.set.to_array(seen)) // ["ada", "grace"]
fn add<T>(set: {T: bool}, value: T)#

Inserts value (no-op slot-wise when already present).

  • set: Set to insert into, mutated in place.
  • value: Member to insert.
let s = std.set.from_array([1, 2])
std.set.add(s, 3)
std.set.remove(s, 1)
println(std.set.has(s, 3))   // true
println(std.set.to_array(s)) // [2, 3]
fn remove<T>(set: {T: bool}, value: T)#

Removes value when present; no-op otherwise.

  • set: Set to remove from, mutated in place.
  • value: Member to drop.
fn has<T>(set: {T: bool}, value: T) -> bool#

Whether value is a member.

  • set: Set to query.
  • value: Candidate member.
fn size<T>(set: {T: bool}) -> int#

The number of members.

  • set: Set to measure.
fn to_array<T>(set: {T: bool}) -> [T]#

The members in insertion order.

  • set: Set to enumerate.
fn union_of<T>(a: {T: bool}, b: {T: bool}) -> {T: bool}#

Members of a or b; a's insertion order first, then b's new members.

  • a: Set whose members come first.
  • b: Set whose new members follow.
let a = std.set.from_array([1, 2, 3])
let b = std.set.from_array([3, 4])
println(std.set.to_array(std.set.union_of(a, b))) // [1, 2, 3, 4]
fn intersect<T>(a: {T: bool}, b: {T: bool}) -> {T: bool}#

Members of both a and b, in a's insertion order.

  • a: Set that supplies the order and the candidates.
  • b: Set every kept member must also belong to.
let a = std.set.from_array([1, 2, 3])
let b = std.set.from_array([3, 4])
println(std.set.to_array(std.set.intersect(a, b))) // [3]
fn difference<T>(a: {T: bool}, b: {T: bool}) -> {T: bool}#

Members of a not in b, in a's insertion order.

  • a: Set that supplies the order and the candidates.
  • b: Set whose members are excluded.
let a = std.set.from_array([1, 2, 3])
let b = std.set.from_array([3, 4])
println(std.set.to_array(std.set.difference(a, b))) // [1, 2]