/* * Copyright 2020 Magnus Madsen * * Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE.md file. */pubmod MutSet {////// The Mutable Set type.///pubstructMutSet[t: Type, r: Region] { rc: Region[r],mut inner: Set[t] }instanceIterable[MutSet[a, r]] {typeElm = atypeAef = rpubdefiterator(rc: Region[r1], s: MutSet[a, r]): Iterator[a, r + r1, r1] \ (r + r1) = MutSet.iterator(rc, s) }instanceForEach[MutSet[a, r]] {typeElm = atypeAef = rpubdefforEach(f: a -> Unit \ ef, s: MutSet[a, r]): Unit \ ef + r = MutSet.forEach(f, s) }instanceFormattable[MutSet[t, r]] withFormattable[t] {typeAef = Formattable.Aef[t] + rpubdefformat(x: MutSet[t, r]): RichString \ (Formattable.Aef[t] + r) =use RichString.{fromString, joinWith};fromString("MutSet#{") +joinWith(Formattable.format, fromString(", "), MutSet.toList(x)) +fromString("}") }////// Returns a string representation of the given mutable set `s`.///pubdeftoString(s: MutSet[a, r]): String \ rwithToString[a] = regionrc {"MutSet#{"+ (MutSet.iterator(rc, s) |> Iterator.join(", ")) +"}" }////// Returns a fresh empty set.///pubdefempty(rc: Region[r]): MutSet[a, r] \ r =new MutSet @ rc {rc = rc, inner = Set.empty()}////// Returns the singleton set containing `x`.///pubdefsingleton(rc: Region[r], x: a): MutSet[a, r] \ rwithOrder[a] =new MutSet @ rc {rc = rc, inner = Set.singleton(x)}////// Adds the element `x` to the mutable set `s`.///pubdefadd(x: a, s: MutSet[a, r]): Unit \ rwithOrder[a] =s->inner = Set.insert(x, s->inner)////// Adds all elements in the collection `m` to the mutable set `s`.///pubdefaddAll(m: m[a], s: MutSet[a, r]): Unit \ (r + Foldable.Aef[m]) withOrder[a], Foldable[m] =Foldable.forEach(x -> add(x, s), m)////// Removes all elements from the mutable set `s`.///pubdefclear(s: MutSet[a, r]): Unit \ r =s->inner = Set.empty()////// Removes the element `x` from the mutable set `s`.///pubdefremove(x: a, s: MutSet[a, r]): Unit \ rwithOrder[a] =s->inner = Set.remove(x, s->inner)////// Removes all elements in the collection `m` from the mutable set `s`.///pubdefremoveAll(m: m[a], s: MutSet[a, r]): Unit \ (r + Foldable.Aef[m]) withOrder[a], Foldable[m] =lets2 = Foldable.toSet(m);s->inner = Set.difference(s->inner, s2)////// Removes all elements from the mutable set `s` that are not in collection `m`.///pubdefretainAll(m: m[a], s: MutSet[a, r]): Unit \ (r + Foldable.Aef[m]) withOrder[a], Foldable[m] =lets2 = Foldable.toSet(m);s->inner = Set.intersection(s2, s->inner)////// Removes all elements from the mutable set `s` that do not satisfy the predicate function `f`.////// The function `f` must be pure.///pubdefrefine(f: a -> Bool, s: MutSet[a, r]): Unit \ rwithOrder[a] =s->inner = Set.filter(f, s->inner)////// Replaces the element `src` with the element `dst` if `from` is in the mutable set `s`.////// The mutable set `s` is unchanged if the element `from` is not in it.///pubdefreplace(src: {src = a}, dst: {dst = a}, s: MutSet[a, r]): Unit \ rwithOrder[a] =s->inner = Set.replace(src = src#src, dst = dst#dst, s->inner)////// Returns true if and only if `s` is the empty set.///pubdefisEmpty(s: MutSet[a, r]): Bool \ r =Set.isEmpty(s->inner)////// Returns true if and only if `s` is a non-empty set.///pubdefnonEmpty(s: MutSet[a, r]): Bool \ r = notisEmpty(s)////// Returns true if and only if `x` is a member of the mutable set `s`.///pubdefmemberOf(x: a, s: MutSet[a, r]): Bool \ rwithOrder[a] =Set.memberOf(x, s->inner)////// Optionally finds the smallest element of `s` according to the `Order` on `a`.////// Returns `None` if `s` is empty.///pubdefminimum(s: MutSet[a, r]): Option[a] \ r =Set.minimum(s->inner)////// Optionally finds the smallest element of `s` according to the given comparator `cmp`.////// Returns `None` if `s` is empty.////// Purity reflective: Runs in parallel when given a pure function `f`.///pubdefminimumBy(cmp: (a, a) -> Comparison \ ef, s: MutSet[a, r]): Option[a] \ { ef, r } =Set.minimumBy(cmp, s->inner)////// Optionally finds the largest element of `s` according to the `Order` on `a`.////// Returns `None` if `s` is empty.///pubdefmaximum(s: MutSet[a, r]): Option[a] \ r =Set.maximum(s->inner)////// Optionally finds the largest element of `s` according to the given comparator `cmp`.////// Returns `None` if `s` is empty.////// Purity reflective: Runs in parallel when given a pure function `f`.///pubdefmaximumBy(cmp: (a, a) -> Comparison \ ef, s: MutSet[a, r]): Option[a] \ { ef, r } =Set.maximumBy(cmp, s->inner)////// Returns the number of elements in the mutable set `s`.///pubdefsize(s: MutSet[a, r]): Int32 \ r =Set.size(s->inner)////// Alias for `findLeft`.////// The function `f` must be pure.///pubdeffind(f: a -> Bool, s: MutSet[a, r]): Option[a] \ r =findLeft(f, s)////// Optionally returns the first element of the mutable set `s` that satisfies the predicate function `f` when searching from left to right.////// The function `f` must be pure.///pubdeffindLeft(f: a -> Bool, s: MutSet[a, r]): Option[a] \ r =Set.findLeft(f, s->inner)////// Optionally returns the first element of the mutable set `s` that satisfies the predicate function `f` when searching from right to left.////// The function `f` must be pure.///pubdeffindRight(f: a -> Bool, s: MutSet[a, r]): Option[a] \ r =Set.findRight(f, s->inner)////// Applies `f` to a start value `i` and all elements in the mutable set `s` going from left to right.////// That is, the result is of the form: `f(...f(f(i, x1), x2)..., xn)`.///pubdeffoldLeft(f: (b, a) -> b \ ef, i: b, s: MutSet[a, r]): b \ { ef, r } =Set.foldLeft(f, i, s->inner)////// Applies `f` to a start value `z` and all elements in the mutable set `s` going from right to left.////// That is, the result is of the form: `f(x1, ...f(xn-1, f(xn, z))...)`.///pubdeffoldRight(f: (a, b) -> b \ ef, z: b, s: MutSet[a, r]): b \ { ef, r } =Set.foldRight(f, z, s->inner)////// Returns the result of mapping each element and combining the results.///pubdeffoldMap(f: a -> b \ ef, s: MutSet[a, r]): b \ { ef, r } withMonoid[b] =foldLeft((acc, x) -> Monoid.combine(acc, f(x)), Monoid.empty(), s)////// Returns the number of elements in the mutable set `s` that satisfy the predicate function `f`.////// Purity reflective: Runs in parallel when given a pure function `f`.///pubdefcount(f: a -> Bool \ ef, s: MutSet[a, r]): Int32 \ { ef, r } =Set.count(f, s->inner)////// Returns `true` if and only if at least one element in the mutable set `s` satisfies the predicate function `f`.////// Returns `false` if `s` is the empty set.///pubdefexists(f: a -> Bool \ ef, s: MutSet[a, r]): Bool \ { ef, r } =Set.exists(f, s->inner)////// Returns `true` if and only if all elements in the mutable set `s` satisfy the predicate function `f`.////// Returns `true` if `s` is the empty set.///pubdefforAll(f: a -> Bool \ ef, s: MutSet[a, r]): Bool \ { ef, r } =Set.forAll(f, s->inner)////// Returns a shallow copy of the mutable set `s`.///pubdefcopy(rc1: Region[r1], s: MutSet[a, r]): MutSet[a, r1] \ { r, r1 } =new MutSet @ rc1 {rc = rc1, inner = s->inner}////// Returns the mutable set `s` as an immutable set.///pubdeftoSet(s: MutSet[a, r]): Set[a] \ r =s->inner////// Returns the mutable set `s` as a list.///pubdeftoList(s: MutSet[a, r]): List[a] \ r =Set.toList(s->inner)////// Returns the association set `s` as a map.////// If `s` contains multiple mappings with the same key, `toMap` does not/// make any guarantees about which mapping will be in the resulting map.///pubdeftoMap(s: MutSet[(a, b), r]): Map[a, b] \ rwithOrder[a] =Set.toMap(s->inner)////// Returns the mutable set `s` as an array.///pubdeftoArray(rc: Region[r1], s: MutSet[a, r2]): Array[a, r1] \ { r1, r2 } =Set.toArray(rc, s->inner)////// Returns the mutable set `s` as a vector.///pubdeftoVector(s: MutSet[a, r]): Vector[a] \ r =Set.toVector(s->inner)////// Applies `f` to every element of the mutable set `s`.///pubdefforEach(f: a -> Unit \ ef, s: MutSet[a, r]): Unit \ { ef, r } =Set.forEach(f, s->inner)////// Applies `f` to every element of the mutable set `s` along with that element's index.///pubdefforEachWithIndex(f: (Int32, a) -> Unit \ ef, s: MutSet[a, r]): Unit \ { ef, r } = regionrc {letix = Ref.fresh(rc, 0);forEach(x -> { leti = Ref.get(ix); f(i, x); Ref.put(i+1, ix) }, s) }////// Returns an iterator over `s`.///pubdefiterator(rc: Region[r1], s: MutSet[a, r2]): Iterator[a, r1 + r2, r1] \ { r1, r2 } =Set.iterator(rc, s->inner) |> Iterator.map(x -> checked_ecast(x))////// Returns `true` if MutSets `a` and `b` have the same elements, i.e. are structurally equal.///pubdefsameElements(a: MutSet[a, r], b: MutSet[a, r]): Bool \ rwithOrder[a] =Set.isSubsetOf(a->inner, b->inner) andSet.isSubsetOf(b->inner, a->inner)////// Returns the concatenation of the string representation/// of each element in `s` with `sep` inserted between each element.///pubdefjoin(sep: String, s: MutSet[a, r]): String \ rwithToString[a] =Set.join(sep, s->inner)////// Returns the concatenation of the string representation/// of each element in `s` according to `f` with `sep` inserted between each element.///pubdefjoinWith(f: a -> String \ ef, sep: String, s: MutSet[a, r]): String \ { ef, r } =Set.joinWith(f, sep, s->inner)}