Overview
In-place hash set backed directly by .NET HashSet<T>.
(import (std mutable set))
(def seen (mutableset))
(mutableset-add! seen "a")
(mutableset-add! seen "a") ; already a member: nothing changes
(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 an enumerator) 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
hashset->mutablesetlist->mutablesetmutablesetmutableset->hashsetmutableset->listmutableset->seqmutableset->strmutableset->vecmutableset-add!mutableset-add-all!mutableset-add-new!mutableset-clear!mutableset-contains?mutableset-copymutableset-difference!mutableset-empty?mutableset-enumeratormutableset-enumerator-currentmutableset-enumerator-next!mutableset-for-eachmutableset-intersect!mutableset-lengthmutableset-overlaps?mutableset-remove!mutableset-remove-all!mutableset-remove-where!mutableset-subset?mutableset-superset?mutableset-symmetric-difference!mutableset-try-add-new!mutableset-union!seq->mutablesetvec->mutableset
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
MutableSetEnumerator
type alias ↑ Types
One walk of a MutableSet, with .NET’s enumerator protocol.
%a- The element type.
.NET’s hash set enumerator, a struct, in a heap cell, so that it can be held and stepped: one allocation for the walk, none per element.
See also: mutableset-enumerator, mutableset-enumerator-next!, mutableset-enumerator-current
Functions
mutableset
function ↑ Functions
(: mutableset (-> (MutableSet %a)))
(mutableset)
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))
(mutableset-empty? s)
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 void))
(mutableset-add! s x)
Makes an element a member of the set.
s- The set to modify.
x- The element to add.
Adding an element that is already a member is not a failure:
afterwards it is a member, which is all this says. It is the
add! of the Addable! trait. To learn whether x was new,
use mutableset-try-add-new!.
See also: mutableset-add-new!, mutableset-try-add-new!, mutableset-add-all!, mutableset-remove!
mutableset-add-new!
function ↑ Functions
(: mutableset-add-new! (-> (MutableSet %a) %a void))
(mutableset-add-new! s x)
Adds an element that must be new.
s- The set to modify.
x- The element to add.
- raises
System.ArgumentException - When an element equal to x is already in s. s is left as it was.
See also: mutableset-try-add-new!, mutableset-add!
mutableset-try-add-new!
function ↑ Functions
(: mutableset-try-add-new! (-> (MutableSet %a) %a (Result %a Unit)))
(mutableset-try-add-new! s x)
Adds an element that must be new, or answers the equal element already there.
s- The set to modify.
x- The element to add.
- returns
- (Ok unit) when x was added; (Err y) when s already holds y, equal to x, and s is left as it was.
(match (mutableset-try-add-new! seen url)
((Ok _) (spawn (fetch url))) ; first time: fetch it
((Err _) unit)) ; seen before
mutableset-add-new! with the failure as a value. The Err
carries the element the set holds, which may be a different object
from x: interning a value is a try-add-new! that keeps what the
Err answers.
See also: mutableset-add-new!, mutableset-add!
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))
(mutableset-remove-where! s pred)
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))
(mutableset-union! a b)
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))
(mutableset-intersect! a b)
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))
(mutableset-difference! a b)
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))
(mutableset-symmetric-difference! a b)
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))
(mutableset-subset? a b)
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))
(mutableset-superset? a b)
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))
(mutableset-overlaps? a b)
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)))
(mutableset-copy s)
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-enumerator
function ↑ Functions
(: mutableset-enumerator (-> (MutableSet %a) (MutableSetEnumerator %a)))
Starts one walk of the set, with .NET’s enumerator protocol.
s- The set to walk.
- returns
- A new enumerator, before the first element.
(def e (mutableset-enumerator s))
(let go ()
(when (mutableset-enumerator-next! e)
(println (mutableset-enumerator-current e))
(go)))
mutableset-enumerator-next! is .NET’s MoveNext, and
mutableset-enumerator-current is Current. It is not a
cursor: stepping it moves it for everyone who holds it, where a cursor
such as vec-cursor is a position that does not change. A
for-each, a loop or a :for does the stepping, so
most programs never hold one.
See also: mutableset-enumerator-next!, mutableset-enumerator-current, mutableset-for-each
mutableset-enumerator-next!
function ↑ Functions
(: mutableset-enumerator-next! (-> (MutableSetEnumerator %a) bool))
Steps the enumerator to the next element, and answers whether there is one.
e- The enumerator.
- returns
- #t when e is on an element; #f when the walk is over.
.NET’s MoveNext: call it once before each
mutableset-enumerator-current, the first time too.
See also: mutableset-enumerator-current
mutableset-enumerator-current
function ↑ Functions
(: mutableset-enumerator-current (-> (MutableSetEnumerator %a) %a))
The element the enumerator is on.
e- The enumerator, after mutableset-enumerator-next! returned #t.
- returns
- The element.
See also: mutableset-enumerator-next!
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-enumerator, 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)))
(seq->mutableset xs)
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)))
(mutableset->seq s)
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.