minor performance upgrade
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import type { Box } from "#/engine/Box.ts";
|
||||
|
||||
export default class MapArray<K, V> {
|
||||
map: Map<K, Box<V>>;
|
||||
array: Array<[K, Box<V>]>;
|
||||
|
||||
constructor(iterable?: Iterable<[K, Box<V>]>) {
|
||||
this.map = new Map(iterable);
|
||||
this.array = iterable ? [...iterable] : [];
|
||||
}
|
||||
|
||||
delete(key: K): void {
|
||||
if (this.map.delete(key)) {
|
||||
this.array[this.array.findIndex((e) => e[0] === key)] =
|
||||
this.array[this.array.length - 1];
|
||||
this.array.pop();
|
||||
}
|
||||
}
|
||||
|
||||
set(key: K, value: Box<V>): void {
|
||||
if (this.map.has(key)) {
|
||||
this.array.find((e) => e[0] === key)![1] = value;
|
||||
} else {
|
||||
this.array.push([key, value]);
|
||||
}
|
||||
|
||||
this.map.set(key, value);
|
||||
}
|
||||
|
||||
get(key: K): Box<V> | undefined {
|
||||
return this.map.get(key);
|
||||
}
|
||||
|
||||
entries(): Array<[K, Box<V>]> {
|
||||
return this.array;
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
import type { Box } from "#/engine/Box.ts";
|
||||
|
||||
export default class MapProxy<K, V> {
|
||||
map: Map<K, Box<V>>;
|
||||
|
||||
constructor(iterable?: Iterable<readonly [K, Box<V>]>) {
|
||||
this.map = new Map(iterable);
|
||||
}
|
||||
|
||||
get(key: K): Box<V> | undefined {
|
||||
return this.map.get(key);
|
||||
}
|
||||
|
||||
entries(): MapIterator<[K, Box<V>]> {
|
||||
return this.map.entries();
|
||||
}
|
||||
|
||||
keys(): MapIterator<K> {
|
||||
return this.map.keys();
|
||||
}
|
||||
|
||||
values(): MapIterator<Box<V>> {
|
||||
return this.map.values();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user