(std mutable set)

Reference

Overview

In-place hash set backed directly by .NET HashSet<T>.

(import (std mutable set))

(def seen (mutableset))
(mutableset-add! seen "a")       ; #t: newly added
(mutableset-add! seen "a")       ; #f: already existed
(mutableset-contains? seen "a")  ; #t

(def other (list->mutableset (list "b" "c")))
(mutableset-union! seen other)   ; seen now contains "a", "b", and "c"
(mutableset-length seen)         ; 3

(MutableSet a) directly aliases .NET HashSet<T>. Adding, removing, and membership tests run in expected O(1) time. Foreign methods accepting HashSet<T> accept MutableSet directly.

Mutability. Operations such as mutableset-add! modify the set in place. All references observe modifications immediately. Unlike TransientHashSet (which is frozen back into an immutable HashSet), a MutableSet remains mutable indefinitely.

In-place set algebra. Set algebraic operations (mutableset-union!, mutableset-intersect!, mutableset-difference!, mutableset-symmetric-difference!) mutate the first argument in place without modifying the second argument. To produce a new independent set, use persistent hashset-union or clone the set first with mutableset-copy. Relational predicates (mutableset-subset?, mutableset-superset?, mutableset-overlaps?) do not modify their arguments.

Element comparison. Elements are compared using .NET’s default equality comparer (EqualityComparer<T>.Default). For types defined in Bjolang, this resolves to the type’s Eq instance. Modifying elements after insertion leads to undefined lookup behavior.

Iteration order. Sets are unordered; iteration order reflects internal bucket layout.

Iteration safety. Iterating over a set (mutableset->seq, for-each, or cursor loops) reads the live table directly. Mutating the set during traversal raises System.InvalidOperationException. To mutate during iteration, take a snapshot via mutableset->list or use mutableset-remove-where!.

Equality and hashing. Two sets are = if they contain identical elements according to .NET equality. eq-hash raises an exception to prevent using mutable sets as hash table keys.

Thread safety. Mutable sets are not thread-safe. Concurrent mutations require external locking.

Traits. Implements Refable (where (ref s x) tests membership), Iterable, Collection, Addable!, Eq, Written and Displayed. Note that add! from Addable! returns unit and does not report whether the element was new.

See also: MutableSet, mutableset, mutableset-add!, mutableset-contains?, mutableset-union!

Reference

Types

Functions

Types

MutableSet

type alias ↑ Types

In-place hash set of %a (aliases .NET HashSet<T>).

%a
The element type (compared with .NET equality).

See also: mutableset, hashset->mutableset, HashSet

MutableSetCursor

type alias ↑ Types

Stateful cursor for iterating over a MutableSet.

%a
The element type.

Wraps .NET’s hash set enumerator struct in an object to provide step-by-step cursor iteration with zero allocations per element.

See also: mutableset-cursor, mutableset-cursor-done?, mutableset-cursor-current

Functions

mutableset

function ↑ Functions

(: mutableset (-> (MutableSet %a)))

Creates an empty mutable set.

returns
A new empty set.

See also: list->mutableset

mutableset-length

function ↑ Functions

(: mutableset-length (-> (MutableSet %a) int))

Returns the number of elements in the set.

s
The set.
returns
The count of elements.

See also: mutableset-empty?

mutableset-empty?

function ↑ Functions

(: mutableset-empty? (-> (MutableSet %a) bool))

Tests if the set contains no elements.

s
The set.
returns
#t if empty, otherwise #f.

See also: mutableset-length

mutableset-add!

function ↑ Functions

(: mutableset-add! (-> (MutableSet %a) %a bool))

Adds an element to the set, returning #t if it was newly added.

s
The set to modify.
x
The element to add.
returns
#t if x was newly added, #f if x was already present.

Indicates whether an insertion occurred in a single hash table lookup.

See also: mutableset-add-all!, mutableset-remove!

mutableset-add-all!

function ↑ Functions

(: mutableset-add-all! (-> (MutableSet %a) (Seq %a) void))

Adds all elements from a sequence into the set.

s
The set to modify.
xs
Sequence of elements to add.

See also: mutableset-add!, mutableset-union!

mutableset-remove!

function ↑ Functions

(: mutableset-remove! (-> (MutableSet %a) %a bool))

Removes an element from the set, returning #t if found.

s
The set to modify.
x
The element to remove.
returns
#t if x was present and removed, #f otherwise.

See also: mutableset-add!, mutableset-remove-all!, mutableset-remove-where!

mutableset-remove-all!

function ↑ Functions

(: mutableset-remove-all! (-> (MutableSet %a) (Seq %a) void))

Removes all elements in a sequence from the set.

s
The set to modify.
xs
Sequence of elements to remove.

See also: mutableset-remove!, mutableset-difference!

mutableset-remove-where!

function ↑ Functions

(: mutableset-remove-where! (-> (MutableSet %a) (-> %a bool) int))

Removes all elements satisfying a predicate.

s
The set to modify.
pred
Predicate function returning #t to remove an element.
returns
The number of elements removed.
(mutableset-remove-where! s (fun (x) (< x 0)))   ; remove negative numbers

See also: mutableset-remove!

mutableset-clear!

function ↑ Functions

(: mutableset-clear! (-> (MutableSet %a) void))

Removes all elements from the set.

s
The set to clear.

mutableset-contains?

function ↑ Functions

(: mutableset-contains? (-> (MutableSet %a) %a bool))

Tests if an element is in the set.

s
The set.
x
The element to locate.
returns
#t if x is present, otherwise #f.

See also: ref

mutableset-union!

function ↑ Functions

(: mutableset-union! (-> (MutableSet %a) (MutableSet %a) void))

Adds all elements from b into a, mutating a in place.

a
The target set to mutate.
b
The source set (unmodified).

See also: mutableset-add-all!, mutableset-intersect!

mutableset-intersect!

function ↑ Functions

(: mutableset-intersect! (-> (MutableSet %a) (MutableSet %a) void))

Retains only elements in a that are also in b, mutating a in place.

a
The target set to mutate.
b
The reference set (unmodified).

See also: mutableset-union!, mutableset-difference!

mutableset-difference!

function ↑ Functions

(: mutableset-difference! (-> (MutableSet %a) (MutableSet %a) void))

Removes all elements in b from a, mutating a in place.

a
The target set to mutate.
b
The elements to remove (unmodified).

See also: mutableset-remove-all!, mutableset-symmetric-difference!

mutableset-symmetric-difference!

function ↑ Functions

(: mutableset-symmetric-difference! (-> (MutableSet %a) (MutableSet %a) void))

Mutates a to contain elements present in either a or b, but not both.

a
The target set to mutate.
b
The other set (unmodified).

See also: mutableset-difference!, mutableset-union!

mutableset-subset?

function ↑ Functions

(: mutableset-subset? (-> (MutableSet %a) (MutableSet %a) bool))

Tests if a is a subset of b.

a
The candidate subset.
b
The target set.
returns
#t if all elements in a are present in b, otherwise #f.

See also: mutableset-superset?

mutableset-superset?

function ↑ Functions

(: mutableset-superset? (-> (MutableSet %a) (MutableSet %a) bool))

Tests if a is a superset of b.

a
The candidate superset.
b
The target set.
returns
#t if all elements in b are present in a, otherwise #f.

See also: mutableset-subset?

mutableset-overlaps?

function ↑ Functions

(: mutableset-overlaps? (-> (MutableSet %a) (MutableSet %a) bool))

Tests if a and b share at least one common element.

a
First set.
b
Second set.
returns
#t if a and b intersect, otherwise #f.

See also: mutableset-intersect!

mutableset-copy

function ↑ Functions

(: mutableset-copy (-> (MutableSet %a) (MutableSet %a)))

Creates a shallow copy of the set.

s
The set to copy.
returns
A new mutable set with identical elements. Elements are not cloned.

See also: mutableset->hashset

mutableset-cursor

function ↑ Functions

(: mutableset-cursor (-> (MutableSet %a) (MutableSetCursor %a)))

Creates an iteration cursor positioned before the first element.

s
The set to traverse.
returns
A new cursor positioned before the start.
(def c (mutableset-cursor s))
(let loop ()
  (cond
    ((mutableset-cursor-done? c) unit)
    (else
     (println (mutableset-cursor-current c))
     (loop))))

Call mutableset-cursor-done? before each element, then read with mutableset-cursor-current. Standard for-each and :for loops handle this automatically.

See also: mutableset-cursor-done?, mutableset-cursor-current, mutableset-for-each

mutableset-cursor-done?

function ↑ Functions

(: mutableset-cursor-done? (-> (MutableSetCursor %a) bool))

Advances the cursor to the next element; returns #t if finished.

c
The cursor to advance.
returns
#t if iteration is finished, #f if positioned on an element.

Advances cursor position. Call once before reading each element with mutableset-cursor-current.

See also: mutableset-cursor-current

mutableset-cursor-current

function ↑ Functions

(: mutableset-cursor-current (-> (MutableSetCursor %a) %a))

Returns the element at the current cursor position.

c
The cursor.
returns
The element at the current position.

Call only when mutableset-cursor-done? has returned #f.

See also: mutableset-cursor-done?

mutableset-for-each

function ↑ Functions

(: mutableset-for-each (-> (-?-> %a void) (MutableSet %a) void))
(mutableset-for-each f s)

Applies a function to each element in the set.

f
Function to call on each element.
s
The set to iterate over.

Applies f to each element in arbitrary order. Mutating the set inside f raises System.InvalidOperationException.

See also: mutableset-cursor, for-each

list->mutableset

function ↑ Functions

(: list->mutableset (-> (List %a) (MutableSet %a)))
(list->mutableset xs)

Constructs a new mutable set from a list.

xs
Source list. Duplicate elements are added once.
returns
A new mutable set.

See also: mutableset->list

mutableset->list

function ↑ Functions

(: mutableset->list (-> (MutableSet %a) (List %a)))
(mutableset->list s)

Converts set elements to a list.

s
The source set.
returns
A new list containing all elements in traversal order.

See also: list->mutableset

vec->mutableset

function ↑ Functions

(: vec->mutableset (-> (Vec %a) (MutableSet %a)))
(vec->mutableset v)

Constructs a new mutable set from a vector.

v
Source vector. Duplicate elements are added once.
returns
A new mutable set.

See also: mutableset->vec

mutableset->vec

function ↑ Functions

(: mutableset->vec (-> (MutableSet %a) (Vec %a)))
(mutableset->vec s)

Converts set elements to a vector.

s
The source set.
returns
A new vector containing all elements in traversal order.

See also: vec->mutableset

seq->mutableset

function ↑ Functions

(: seq->mutableset (-> (Seq %a) (MutableSet %a)))

Constructs a new mutable set from a sequence.

xs
Source sequence. Duplicate elements are added once.
returns
A new mutable set.

See also: mutableset->seq

mutableset->seq

function ↑ Functions

(: mutableset->seq (-> (MutableSet %a) (Seq %a)))

Views set elements as a sequence.

s
The source set.
returns
A Seq iterating over the set.

Views the live set. Modifying the set during sequence traversal raises System.InvalidOperationException. To take an immutable snapshot, use mutableset->list or mutableset->vec.

See also: seq->mutableset, mutableset->list

hashset->mutableset

function ↑ Functions

(: hashset->mutableset (-> (HashSet %a) (MutableSet %a)))
(hashset->mutableset s)

Constructs a new mutable set from an immutable persistent HashSet.

s
The source persistent set.
returns
A new mutable set containing all elements.

See also: mutableset->hashset

mutableset->hashset

function ↑ Functions

(: mutableset->hashset (-> (MutableSet %a) (HashSet %a)))
(mutableset->hashset s)

Converts a mutable set into an immutable persistent HashSet.

s
The source mutable set.
returns
A new persistent HashSet. Subsequent modifications to s do not affect it.

See also: hashset->mutableset

mutableset->str

function ↑ Functions

(: mutableset->str (-> (MutableSet %a) string))
(mutableset->str s)

Deprecated: use displayed. Formats the set as text in the form #mset(a b c).

s
The set to format.
returns
Formatted string representation using displayed for each element.
(displayed (list->mutableset (list 1)))   ; "#mset(1)"

The same text as displayed gives for a mutable set.