This commit is contained in:
Schluffe
2026-08-26 18:03:33 +02:00
parent 275e571bda
commit 2384c4de1c
2 changed files with 42 additions and 4 deletions
+9 -4
View File
@@ -6,10 +6,15 @@ export type Dictionary<K, V> = {
setQueue: Array<[K, V]>;
};
export function createDictionary<K, V>(): Dictionary<K, V> {
export function createDictionary<K, V>(
iterable?: Iterable<[K, V]>,
): Dictionary<K, V> {
const entries = iterable ? [...iterable] : [];
const indices = new Map(entries.map(([k], i) => [k, i]));
return {
entries: [],
indices: new Map(),
entries,
indices,
isIterating: false,
deleteQueue: [],
setQueue: [],
@@ -46,8 +51,8 @@ export function setEntry<K, V>(
): void {
const index = dict.indices.get(key);
if (index === undefined) {
dict.entries.push([key, value]);
dict.indices.set(key, dict.entries.length);
dict.entries.push([key, value]);
} else {
dict.entries[index][1] = value;
}