import { IEqualityComparer, IDisposable, disposeSafe, defaultOf, IMap, MutableArray, equals, toIterator, IEnumerator, getEnumerator } from "./Util.ts"; import { iterate, map, delay, toArray, iterateIndexed, concat } from "./Seq.ts"; import { value as value_1, Option } from "./Option.ts"; import { int32 } from "./Int32.ts"; import { item as item_1, setItem } from "./Array.ts"; import { FSharpRef } from "./Types.ts"; import { class_type, TypeInfo } from "./Reflection.ts"; import { getItemFromDict, tryGetValue } from "./MapUtil.ts"; import { KeyNotFoundException_$ctor_Z721C83C5 } from "./System.Collections.Generic.ts"; import { ArgumentException_$ctor_Z721C83C5 } from "./System.ts"; import { format } from "./String.ts"; export class Dictionary implements IMap, Iterable<[Key, Value]>, Iterable<[Key, Value]> { readonly comparer: IEqualityComparer; readonly hashMap: IMap; "init@9": int32; constructor(pairs: Iterable<[Key, Value]>, 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<[Key, Value]> = getEnumerator(pairs); try { while (enumerator["System.Collections.IEnumerator.MoveNext"]()) { const pair: [Key, Value] = enumerator["System.Collections.Generic.IEnumerator`1.get_Current"](); Dictionary__Add_5BDDA1(this$.contents, pair[0], pair[1]); } } finally { disposeSafe(enumerator as IDisposable); } } get [Symbol.toStringTag](): string { return "Dictionary"; } toJSON(): any { const this$: Dictionary = this; return Array.from(this$); } "System.Collections.IEnumerable.GetEnumerator"(): IEnumerator { const this$: Dictionary = this; return getEnumerator(this$); } GetEnumerator(): IEnumerator<[Key, Value]> { const this$: Dictionary = this; return getEnumerator(concat<[Key, Value][], [Key, Value]>(this$.hashMap.values())); } [Symbol.iterator](): Iterator<[Key, Value]> { return toIterator(getEnumerator(this)); } "System.Collections.Generic.ICollection`1.Add2B595"(item: [Key, Value]): void { const this$: Dictionary = this; Dictionary__Add_5BDDA1(this$, item[0], item[1]); } "System.Collections.Generic.ICollection`1.Clear"(): void { const this$: Dictionary = this; Dictionary__Clear(this$); } "System.Collections.Generic.ICollection`1.Contains2B595"(item: [Key, Value]): boolean { const this$: Dictionary = this; const matchValue: Option<[Key, Value]> = Dictionary__TryFind_2B595(this$, item[0]); let matchResult: int32 = (undefined as any), pair_1: [Key, Value] = (undefined as any); if (matchValue != null) { if (equals(value_1(matchValue)[1], item[1])) { matchResult = 0; pair_1 = value_1(matchValue); } else { matchResult = 1; } } else { matchResult = 1; } switch (matchResult) { case 0: return true; default: return false; } } "System.Collections.Generic.ICollection`1.CopyToZ3B4C077E"(array: MutableArray<[Key, Value]>, arrayIndex: int32): void { const this$: Dictionary = this; iterateIndexed<[Key, Value]>((i: int32, e: [Key, Value]): void => { setItem(array, arrayIndex + i, e); }, this$); } "System.Collections.Generic.ICollection`1.get_Count"(): int32 { const this$: Dictionary = this; return Dictionary__get_Count(this$) | 0; } "System.Collections.Generic.ICollection`1.get_IsReadOnly"(): boolean { return false; } "System.Collections.Generic.ICollection`1.Remove2B595"(item: [Key, Value]): boolean { const this$: Dictionary = this; const matchValue: Option<[Key, Value]> = Dictionary__TryFind_2B595(this$, item[0]); let matchResult: int32 = (undefined as any), pair_1: [Key, Value] = (undefined as any); if (matchValue != null) { if (equals(value_1(matchValue)[1], item[1])) { matchResult = 0; pair_1 = value_1(matchValue); } else { matchResult = 1; } } else { matchResult = 1; } switch (matchResult) { case 0: return Dictionary__Remove_2B595(this$, item[0]); default: return false; } } "System.Collections.Generic.IDictionary`2.Add5BDDA1"(key: Key, value: Value): void { const this$: Dictionary = this; Dictionary__Add_5BDDA1(this$, key, value); } "System.Collections.Generic.IDictionary`2.ContainsKey2B595"(key: Key): boolean { const this$: Dictionary = this; return Dictionary__ContainsKey_2B595(this$, key); } "System.Collections.Generic.IDictionary`2.get_Item2B595"(key: Key): Value { const this$: Dictionary = this; return Dictionary__get_Item_2B595(this$, key); } "System.Collections.Generic.IDictionary`2.set_Item5BDDA1"(key: Key, v: Value): void { const this$: Dictionary = this; Dictionary__set_Item_5BDDA1(this$, key, v); } "System.Collections.Generic.IDictionary`2.get_Keys"(): Iterable { const this$: Dictionary = this; return toArray(delay((): Iterable => map<[Key, Value], Key>((pair: [Key, Value]): Key => pair[0], this$))); } "System.Collections.Generic.IDictionary`2.Remove2B595"(key: Key): boolean { const this$: Dictionary = this; return Dictionary__Remove_2B595(this$, key); } "System.Collections.Generic.IDictionary`2.TryGetValue6DC89625"(key: Key, value: FSharpRef): boolean { const this$: Dictionary = this; const matchValue: Option<[Key, Value]> = Dictionary__TryFind_2B595(this$, key); if (matchValue != null) { const pair: [Key, Value] = value_1(matchValue); value.contents = pair[1]; return true; } else { return false; } } "System.Collections.Generic.IDictionary`2.get_Values"(): Iterable { const this$: Dictionary = this; return toArray(delay((): Iterable => map<[Key, Value], Value>((pair: [Key, Value]): Value => pair[1], this$))); } get size(): int32 { const this$: Dictionary = this; return Dictionary__get_Count(this$) | 0; } clear(): void { const this$: Dictionary = this; Dictionary__Clear(this$); } delete(k: Key): boolean { const this$: Dictionary = this; return Dictionary__Remove_2B595(this$, k); } entries(): Iterable<[Key, Value]> { const this$: Dictionary = this; return map<[Key, Value], [Key, Value]>((p: [Key, Value]): [Key, Value] => ([p[0], p[1]] as [Key, Value]), this$); } get(k: Key): Value { const this$: Dictionary = this; return Dictionary__get_Item_2B595(this$, k); } has(k: Key): boolean { const this$: Dictionary = this; return Dictionary__ContainsKey_2B595(this$, k); } keys(): Iterable { const this$: Dictionary = this; return map<[Key, Value], Key>((p: [Key, Value]): Key => p[0], this$); } set(k: Key, v: Value): IMap { const this$: Dictionary = this; Dictionary__set_Item_5BDDA1(this$, k, v); return this$; } values(): Iterable { const this$: Dictionary = this; return map<[Key, Value], Value>((p: [Key, Value]): Value => p[1], this$); } forEach(f: ((arg0: Value, arg1: Key, arg2: IMap) => void), thisArg?: Option): void { const this$: Dictionary = this; iterate<[Key, Value]>((p: [Key, Value]): void => { f(p[1], p[0], this$); }, this$); } } export function Dictionary_$reflection(gen0: TypeInfo, gen1: TypeInfo): TypeInfo { return class_type("Fable.Collections.Dictionary", [gen0, gen1], Dictionary); } export function Dictionary_$ctor_6623D9B3(pairs: Iterable<[Key, Value]>, comparer: IEqualityComparer): Dictionary { return new Dictionary(pairs, comparer); } function Dictionary__TryFindIndex_2B595(this$: Dictionary, k: Key): [boolean, int32, int32] { const h: int32 = this$.comparer.GetHashCode(k) | 0; let matchValue: [boolean, [Key, Value][]]; let outArg: [Key, Value][] = defaultOf(); matchValue = ([tryGetValue(this$.hashMap, h, new FSharpRef<[Key, Value][]>((): [Key, Value][] => outArg, (v: [Key, Value][]): void => { outArg = v; })), outArg] as [boolean, [Key, Value][]]); if (matchValue[0]) { return [true, h, matchValue[1].findIndex((pair: [Key, Value]): boolean => this$.comparer.Equals(k, pair[0]))] as [boolean, int32, int32]; } else { return [false, h, -1] as [boolean, int32, int32]; } } export function Dictionary__TryFind_2B595(this$: Dictionary, k: Key): Option<[Key, Value]> { const matchValue: [boolean, int32, int32] = Dictionary__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 item_1(matchValue[2], getItemFromDict(this$.hashMap, matchValue[1])); default: return undefined; } } export function Dictionary__get_Comparer(this$: Dictionary): IEqualityComparer { return this$.comparer; } export function Dictionary__Clear(this$: Dictionary): void { this$.hashMap.clear(); } export function Dictionary__get_Count(this$: Dictionary): int32 { let count = 0; let enumerator: any = getEnumerator(this$.hashMap.values()); try { while (enumerator["System.Collections.IEnumerator.MoveNext"]()) { const pairs: [Key, Value][] = enumerator["System.Collections.Generic.IEnumerator`1.get_Current"](); count = ((count + pairs.length) | 0); } } finally { disposeSafe(enumerator); } return count | 0; } export function Dictionary__get_Item_2B595(this$: Dictionary, k: Key): Value { const matchValue: Option<[Key, Value]> = Dictionary__TryFind_2B595(this$, k); if (matchValue != null) { return value_1(matchValue)[1]; } else { throw KeyNotFoundException_$ctor_Z721C83C5("The item was not found in collection"); } } export function Dictionary__set_Item_5BDDA1(this$: Dictionary, k: Key, v: Value): void { const matchValue: [boolean, int32, int32] = Dictionary__TryFindIndex_2B595(this$, k); if (matchValue[0]) { if (matchValue[2] > -1) { setItem(getItemFromDict(this$.hashMap, matchValue[1]), matchValue[2], [k, v] as [Key, Value]); } else { const value: any = void (getItemFromDict(this$.hashMap, matchValue[1]).push([k, v] as [Key, Value])); } } else { this$.hashMap.set(matchValue[1], [[k, v] as [Key, Value]]); } } export function Dictionary__Add_5BDDA1(this$: Dictionary, k: Key, v: Value): void { const matchValue: [boolean, int32, int32] = Dictionary__TryFindIndex_2B595(this$, k); if (matchValue[0]) { if (matchValue[2] > -1) { throw ArgumentException_$ctor_Z721C83C5(format("An item with the same key has already been added. Key: {0}", k)); } else { const value: any = void (getItemFromDict(this$.hashMap, matchValue[1]).push([k, v] as [Key, Value])); } } else { this$.hashMap.set(matchValue[1], [[k, v] as [Key, Value]]); } } export function Dictionary__ContainsKey_2B595(this$: Dictionary, k: Key): boolean { const matchValue: [boolean, int32, int32] = Dictionary__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 Dictionary__Remove_2B595(this$: Dictionary, k: Key): boolean { const matchValue: [boolean, int32, int32] = Dictionary__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; } }