// ets_tracing: off import "../../../Operator/index.js" import type { Equal } from "../../../Equal/index.js" import type { Predicate, Refinement } from "../../../Function/index.js" import { not } from "../../../Function/index.js" import * as I from "../../../Iterable/index.js" import * as St from "../../../Structural/index.js" import * as HM from "../HashMap/core.js" import type { Next } from "../Map/index.js" import * as Tp from "../Tuple/index.js" export class HashSet implements Iterable, St.HasHash, St.HasEquals { constructor(readonly keyMap: HM.HashMap) {} [Symbol.iterator](): Iterator { return HM.keys(this.keyMap) } get [St.hashSym](): number { return St.hashIterator(this[Symbol.iterator]()) } [St.equalsSym](that: unknown): boolean { return ( that instanceof HashSet && that.keyMap.size === this.keyMap.size && I.corresponds(this, that, St.equals) ) } } export function make() { return new HashSet(HM.make()) } /** * Creates a new set from an Iterable */ export function from(xs: Iterable): HashSet { return I.reduce_(xs, make(), (s, v) => add_(s, v)) } export function add_(set: HashSet, v: V) { return set.keyMap.editable ? (HM.set_(set.keyMap, v, true), set) : new HashSet(HM.set_(set.keyMap, v, true)) } export function add(v: V) { return (set: HashSet) => add_(set, v) } export function remove_(set: HashSet, v: V) { return set.keyMap.editable ? (HM.remove_(set.keyMap, v), set) : new HashSet(HM.remove_(set.keyMap, v)) } export function remove(v: V) { return (set: HashSet) => remove_(set, v) } export function values(set: HashSet) { return HM.keys(set.keyMap) } export function has_(set: HashSet, v: V) { return HM.has_(set.keyMap, v) } /** * Apply f to each element */ export function forEach_(map: HashSet, f: (v: V) => void) { HM.forEachWithIndex_(map.keyMap, (k) => { f(k) }) } /** * Mutate `set` within the context of `f`. */ export function mutate_(set: HashSet, transient: (set: HashSet) => void) { const s = beginMutation(set) transient(s) return endMutation(s) } /** * The set of elements which are in both the first and second set, * * the hash and equal of the 2 sets has to be the same */ export function intersection_(l: HashSet, r: Iterable): HashSet { const x = make() return mutate_(x, (y) => { for (const k of r) { if (has_(l, k)) { add_(y, k) } } }) } /** * The set of elements which are in both the first and second set * * @ets_data_first intersection_ */ export function intersection(r: Iterable) { return (l: HashSet) => intersection_(l, r) } /** * Projects a Set through a function */ export function map_(set: HashSet, f: (x: A) => B): HashSet { const r = make() return mutate_(r, (r) => { forEach_(set, (e) => { const v = f(e) if (!has_(r, v)) { add_(r, v) } }) }) } /** * Projects a Set through a function * * @ets_data_first map_ */ export function map(f: (x: A) => B): (set: HashSet) => HashSet { return (set) => map_(set, f) } /** * true if one or more elements match predicate * * @ets_data_first some_ */ export function some(predicate: Predicate): (set: HashSet) => boolean { return (set) => some_(set, predicate) } /** * true if one or more elements match predicate */ export function some_(set: HashSet, predicate: Predicate): boolean { let found = false for (const e of set) { found = predicate(e) if (found) { break } } return found } /** * Calculate the number of keys pairs in a set */ export function size(set: HashSet) { return HM.size(set.keyMap) } /** * Creates an equal for a set */ export function equal(): Equal> { return { equals: (x, y) => { if (y === x) { return true } if (size(x) !== size(y)) { return false } let eq = true for (const vx of x) { if (!has_(y, vx)) { eq = false break } } return eq } } } /** * true if all elements match predicate * * @ets_data_first every_ */ export function every(predicate: Predicate): (set: HashSet) => boolean { return (set) => every_(set, predicate) } /** * true if all elements match predicate */ export function every_(set: HashSet, predicate: Predicate): boolean { return not(some(not(predicate)))(set) } /** * Map + Flatten * * @ets_data_first chain_ */ export function chain(f: (x: A) => Iterable): (set: HashSet) => HashSet { return (set) => chain_(set, f) } /** * Map + Flatten */ export function chain_(set: HashSet, f: (x: A) => Iterable): HashSet { const r = make() mutate_(r, (r) => { forEach_(set, (e) => { for (const a of f(e)) { if (!has_(r, a)) { add_(r, a) } } }) }) return r } /** * `true` if and only if every element in the first set is an element of the second set, * * the hash and equal of the 2 sets has to be the same * * @ets_data_first isSubset_ */ export function isSubset(y: HashSet): (x: HashSet) => boolean { return (x) => isSubset_(y, x) } /** * `true` if and only if every element in the first set is an element of the second set, * * the hash and equal of the 2 sets has to be the same */ export function isSubset_(x: HashSet, y: HashSet): boolean { return every_(x, (a: A) => has_(y, a)) } /** * Filter set values using predicate * * @ets_data_first filter_ */ export function filter( refinement: Refinement ): (set: HashSet) => HashSet export function filter(predicate: Predicate): (set: HashSet) => HashSet export function filter(predicate: Predicate): (set: HashSet) => HashSet { return (set) => filter_(set, predicate) } /** * Filter set values using predicate */ export function filter_( set: HashSet, refinement: Refinement ): HashSet export function filter_(set: HashSet, predicate: Predicate): HashSet export function filter_(set: HashSet, predicate: Predicate): HashSet { const r = make() return mutate_(r, (r) => { const values_ = values(set) let e: Next while (!(e = values_.next()).done) { const value = e.value if (predicate(value)) { add_(r, value) } } return r }) } /** * Partition set values using predicate * * @ets_data_first partition_ */ export function partition( refinement: Refinement ): (set: HashSet) => Tp.Tuple<[HashSet, HashSet]> export function partition( predicate: Predicate ): (set: HashSet) => Tp.Tuple<[HashSet, HashSet]> export function partition( predicate: Predicate ): (set: HashSet) => Tp.Tuple<[HashSet, HashSet]> { return (set) => partition_(set, predicate) } /** * Partition set values using predicate */ export function partition_( set: HashSet, refinement: Refinement ): Tp.Tuple<[HashSet, HashSet]> export function partition_( set: HashSet, predicate: Predicate ): Tp.Tuple<[HashSet, HashSet]> export function partition_( set: HashSet, predicate: Predicate ): Tp.Tuple<[HashSet, HashSet]> { const values_ = values(set) let e: Next const right = beginMutation(make()) const left = beginMutation(make()) while (!(e = values_.next()).done) { const value = e.value if (predicate(value)) { add_(right, value) } else { add_(left, value) } } return Tp.tuple(endMutation(left), endMutation(right)) } /** * Mark `set` as mutable. */ export function beginMutation(set: HashSet) { return new HashSet(HM.beginMutation(set.keyMap)) } /** * Mark `set` as immutable. */ export function endMutation(set: HashSet) { set.keyMap.editable = false return set } /** * Form the set difference (`x` - `y`) */ export function difference_(x: HashSet, y: Iterable): HashSet { return mutate_(x, (s) => { for (const k of y) { remove_(s, k) } }) } /** * Form the set difference (`x` - `y`) * * @ets_data_first difference_ */ export function difference(y: Iterable): (x: HashSet) => HashSet { return (x) => difference_(x, y) } /** * Reduce a state over the map entries */ export function reduce_(set: HashSet, z: Z, f: (z: Z, v: V) => Z): Z { return HM.reduceWithIndex_(set.keyMap, z, (z, v) => f(z, v)) } /** * Reduce a state over the map entries * * @ets_data_first reduce_ */ export function reduce(z: Z, f: (z: Z, v: V) => Z) { return (set: HashSet) => reduce_(set, z, f) } /** * If element is present remove it, if not add it * * @ets_data_first toggle_ */ export function toggle(a: A): (set: HashSet) => HashSet { return (set) => toggle_(set, a) } /** * If element is present remove it, if not add it */ export function toggle_(set: HashSet, a: A): HashSet { return (has_(set, a) ? remove : add)(a)(set) } /** * Form the union of two sets, * * the hash and equal of the 2 sets has to be the same */ export function union_(l: HashSet, r: Iterable): HashSet { const x = make() return mutate_(x, (x) => { forEach_(l, (a) => { add_(x, a) }) for (const a of r) { add_(x, a) } }) } /** * Form the union of two sets, * * the hash and equal of the 2 sets has to be the same * * @ets_data_first union_ */ export function union(y: Iterable): (x: HashSet) => HashSet { return (x) => union_(x, y) }