// ets_tracing: off
import * as I from "../../../Iterable/index.js"
import * as O from "../../../Option/index.js"
import * as MHM from "../HashMap/index.js"
export class HashSet {
private hashMap: MHM.HashMap
constructor() {
this.hashMap = MHM.make()
}
size(): number {
return this.hashMap.length.get
}
isEmpty(): boolean {
return this.size() === 0
}
contains(a: A): boolean {
return O.getOrElse_(this.hashMap.get(a), () => false)
}
add(a: A): boolean {
this.hashMap.set(a, true)
return this.contains(a)
}
remove(a: A): boolean {
this.hashMap.remove(a)
return !this.contains(a)
}
[Symbol.iterator](): Iterator {
return I.map_(this.hashMap, ([a]) => a)[Symbol.iterator]()
}
}
/**
* Creates a new set
*/
export function make(): HashSet {
return new HashSet()
}
/**
* Creates a new set from an Iterable
*/
export function from(xs: Iterable): HashSet {
const res = make()
for (const v of xs) {
res.add(v)
}
return res
}
/**
* Calculate the number of values in a set
*/
export function size(self: HashSet): number {
return self.size()
}
/**
* returns `true` if the set is empty
*/
export function isEmpty(self: HashSet): boolean {
return self.isEmpty()
}
/**
* Creates a new set
*
* @ets_data_first contains_
*/
export function contains_(self: HashSet, a: A): boolean {
return self.contains(a)
}
/**
* return true if the set contains `a`
*
* @ets_data_first contains_
*/
export function contains(a: A) {
return (self: HashSet) => contains_(self, a)
}
/**
* add `a` to the set
*/
export function add_(self: HashSet, a: A): boolean {
return self.add(a)
}
/**
* add `a` to the set
*
* @ets_data_first add_
*/
export function add(a: A) {
return (self: HashSet) => add_(self, a)
}
/**
* remove `a` from the set
*/
export function remove_(self: HashSet, a: A): boolean {
return self.remove(a)
}
/**
* remove `a` from the set
*
* @ets_data_first remove_
*/
export function remove(a: A) {
return (self: HashSet) => remove_(self, a)
}