| 1 | export class Map<K extends AnyNotNil, V> { |
| 2 | public static [Symbol.species] = Map; |
| 3 | public [Symbol.toStringTag] = "Map"; |
| 4 | |
| 5 | private items = new LuaTable<K, V>(); |
| 6 | public size = 0; |
| 7 | |
| 8 | // Key-order variables |
| 9 | private firstKey: K | undefined; |
| 10 | private lastKey: K | undefined; |
| 11 | private nextKey = new LuaTable<K, K>(); |
| 12 | private previousKey = new LuaTable<K, K>(); |
| 13 | |
| 14 | constructor(entries?: Iterable<readonly [K, V]> | Array<readonly [K, V]>) { |
| 15 | if (entries === undefined) return; |
| 16 | |
| 17 | const iterable = entries as Iterable<[K, V]>; |
| 18 | if (iterable[Symbol.iterator]) { |
| 19 | // Iterate manually because Map is compiled with ES5 which doesn't support Iterables in for...of |
| 20 | const iterator = iterable[Symbol.iterator](); |
| 21 | while (true) { |
| 22 | const result = iterator.next(); |
| 23 | if (result.done) { |
| 24 | break; |
| 25 | } |
| 26 | |
| 27 | const value: [K, V] = result.value; // Ensures index is offset when tuple is accessed |
| 28 | this.set(value[0], value[1]); |
| 29 | } |
| 30 | } else { |
| 31 | const array = entries as Array<[K, V]>; |
| 32 | for (const kvp of array) { |
| 33 | this.set(kvp[0], kvp[1]); |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | public clear(): void { |
| 39 | this.items = new LuaTable(); |
| 40 | this.nextKey = new LuaTable(); |
| 41 | this.previousKey = new LuaTable(); |
| 42 | this.firstKey = undefined; |
| 43 | this.lastKey = undefined; |
| 44 | this.size = 0; |
| 45 | } |
| 46 | |
| 47 | public delete(key: K): boolean { |
| 48 | const contains = this.has(key); |
| 49 | if (contains) { |
| 50 | this.size--; |
| 51 | |
| 52 | // Do order bookkeeping |
| 53 | const next = this.nextKey.get(key); |
| 54 | const previous = this.previousKey.get(key); |
| 55 | if (next !== undefined && previous !== undefined) { |
| 56 | this.nextKey.set(previous, next); |
| 57 | this.previousKey.set(next, previous); |
| 58 | } else if (next !== undefined) { |
| 59 | this.firstKey = next; |
| 60 | this.previousKey.set(next, undefined!); |
nothing calls this directly
no outgoing calls
no test coverage detected