import { IMap, IEqualityComparer, IDisposable, disposeSafe, defaultOf, ISet, MutableArray, toIterator, IEnumerator, getEnumerator } from "./Util.ts"; import { iterate, map, iterateIndexed, concat } from "./Seq.ts"; import { item as item_1, setItem } from "./Array.ts"; import { int32 } from "./Int32.ts"; import { some, Option } from "./Option.ts"; import { FSharpRef } from "./Types.ts"; import { class_type, TypeInfo } from "./Reflection.ts"; import { getItemFromDict, tryGetValue } from "./MapUtil.ts"; export class HashSet implements ISet, Iterable, Iterable { readonly comparer: IEqualityComparer; readonly hashMap: IMap; "init@9": int32; constructor(items: Iterable, comparer: IEqualityComparer) { const this$: FSharpRef> = new FSharpRef>(defaultOf()); this.comparer = comparer; this$.contents = this; this.hashMap = (new Map([])); this["init@9"] = 1; const enumerator: IEnumerator = getEnumerator(items); try { while (enumerator["System.Collections.IEnumerator.MoveNext"]()) { const item: T = enumerator["System.Collections.Generic.IEnumerator`1.get_Current"](); HashSet__Add_2B595(this$.contents, item); } } finally { disposeSafe(enumerator as IDisposable); } } get [Symbol.toStringTag](): string { return "HashSet"; } toJSON(): any { const this$: HashSet = this; return Array.from(this$); } "System.Collections.IEnumerable.GetEnumerator"(): IEnumerator { const this$: HashSet = this; return getEnumerator(this$); } GetEnumerator(): IEnumerator { const this$: HashSet = this; return getEnumerator(concat(this$.hashMap.values())); } [Symbol.iterator](): Iterator { return toIterator(getEnumerator(this)); } "System.Collections.Generic.ICollection`1.Add2B595"(item: T): void { const this$: HashSet = this; HashSet__Add_2B595(this$, item); } "System.Collections.Generic.ICollection`1.Clear"(): void { const this$: HashSet = this; HashSet__Clear(this$); } "System.Collections.Generic.ICollection`1.Contains2B595"(item: T): boolean { const this$: HashSet = this; return HashSet__Contains_2B595(this$, item); } "System.Collections.Generic.ICollection`1.CopyToZ3B4C077E"(array: MutableArray, arrayIndex: int32): void { const this$: HashSet = this; iterateIndexed((i: int32, e: T): void => { setItem(array, arrayIndex + i, e); }, this$); } "System.Collections.Generic.ICollection`1.get_Count"(): int32 { const this$: HashSet = this; return HashSet__get_Count(this$) | 0; } "System.Collections.Generic.ICollection`1.get_IsReadOnly"(): boolean { return false; } "System.Collections.Generic.ICollection`1.Remove2B595"(item: T): boolean { const this$: HashSet = this; return HashSet__Remove_2B595(this$, item); } get size(): int32 { const this$: HashSet = this; return HashSet__get_Count(this$) | 0; } add(k: T): ISet { const this$: HashSet = this; HashSet__Add_2B595(this$, k); return this$; } clear(): void { const this$: HashSet = this; HashSet__Clear(this$); } delete(k: T): boolean { const this$: HashSet = this; return HashSet__Remove_2B595(this$, k); } has(k: T): boolean { const this$: HashSet = this; return HashSet__Contains_2B595(this$, k); } keys(): Iterable { const this$: HashSet = this; return map((x: T): T => x, this$); } values(): Iterable { const this$: HashSet = this; return map((x: T): T => x, this$); } entries(): Iterable<[T, T]> { const this$: HashSet = this; return map((v: T): [T, T] => ([v, v] as [T, T]), this$); } forEach(f: ((arg0: T, arg1: T, arg2: ISet) => void), thisArg?: Option): void { const this$: HashSet = this; iterate((x: T): void => { f(x, x, this$); }, this$); } } export function HashSet_$reflection(gen0: TypeInfo): TypeInfo { return class_type("Fable.Collections.HashSet", [gen0], HashSet); } export function HashSet_$ctor_Z6150332D(items: Iterable, comparer: IEqualityComparer): HashSet { return new HashSet(items, comparer); } function HashSet__TryFindIndex_2B595(this$: HashSet, k: T): [boolean, int32, int32] { const h: int32 = this$.comparer.GetHashCode(k) | 0; let matchValue: [boolean, T[]]; let outArg: T[] = defaultOf(); matchValue = ([tryGetValue(this$.hashMap, h, new FSharpRef((): T[] => outArg, (v: T[]): void => { outArg = v; })), outArg] as [boolean, T[]]); if (matchValue[0]) { return [true, h, matchValue[1].findIndex((v_1: T): boolean => this$.comparer.Equals(k, v_1))] as [boolean, int32, int32]; } else { return [false, h, -1] as [boolean, int32, int32]; } } function HashSet__TryFind_2B595(this$: HashSet, k: T): Option { const matchValue: [boolean, int32, int32] = HashSet__TryFindIndex_2B595(this$, k); let matchResult: int32 = undefined as any; if (matchValue[0]) { if (matchValue[2] > -1) { matchResult = 0; } else { matchResult = 1; } } else { matchResult = 1; } switch (matchResult) { case 0: return some(item_1(matchValue[2], getItemFromDict(this$.hashMap, matchValue[1]))); default: return undefined; } } export function HashSet__get_Comparer(this$: HashSet): IEqualityComparer { return this$.comparer; } export function HashSet__Clear(this$: HashSet): void { this$.hashMap.clear(); } export function HashSet__get_Count(this$: HashSet): int32 { let count = 0; let enumerator: any = getEnumerator(this$.hashMap.values()); try { while (enumerator["System.Collections.IEnumerator.MoveNext"]()) { const items: T[] = enumerator["System.Collections.Generic.IEnumerator`1.get_Current"](); count = ((count + items.length) | 0); } } finally { disposeSafe(enumerator); } return count | 0; } export function HashSet__Add_2B595(this$: HashSet, k: T): boolean { const matchValue: [boolean, int32, int32] = HashSet__TryFindIndex_2B595(this$, k); if (matchValue[0]) { if (matchValue[2] > -1) { return false; } else { const value: any = void (getItemFromDict(this$.hashMap, matchValue[1]).push(k)); return true; } } else { this$.hashMap.set(matchValue[1], [k]); return true; } } export function HashSet__Contains_2B595(this$: HashSet, k: T): boolean { const matchValue: [boolean, int32, int32] = HashSet__TryFindIndex_2B595(this$, k); let matchResult: int32 = undefined as any; if (matchValue[0]) { if (matchValue[2] > -1) { matchResult = 0; } else { matchResult = 1; } } else { matchResult = 1; } switch (matchResult) { case 0: return true; default: return false; } } export function HashSet__Remove_2B595(this$: HashSet, k: T): boolean { const matchValue: [boolean, int32, int32] = HashSet__TryFindIndex_2B595(this$, k); let matchResult: int32 = undefined as any; if (matchValue[0]) { if (matchValue[2] > -1) { matchResult = 0; } else { matchResult = 1; } } else { matchResult = 1; } switch (matchResult) { case 0: { getItemFromDict(this$.hashMap, matchValue[1]).splice(matchValue[2], 1); return true; } default: return false; } }