-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathMap.ts
More file actions
177 lines (157 loc) · 5.38 KB
/
Map.ts
File metadata and controls
177 lines (157 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
export class Map<K extends AnyNotNil, V> {
public static [Symbol.species] = Map;
public [Symbol.toStringTag] = "Map";
private items = new LuaTable<K, V>();
public size = 0;
// Key-order variables
private firstKey: K | undefined;
private lastKey: K | undefined;
private nextKey = new LuaTable<K, K>();
private previousKey = new LuaTable<K, K>();
constructor(entries?: Iterable<readonly [K, V]> | Array<readonly [K, V]>) {
if (entries === undefined) return;
const iterable = entries as Iterable<[K, V]>;
if (iterable[Symbol.iterator]) {
// Iterate manually because Map is compiled with ES5 which doesn't support Iterables in for...of
const iterator = iterable[Symbol.iterator]();
while (true) {
const result = iterator.next();
if (result.done) {
break;
}
const value: [K, V] = result.value; // Ensures index is offset when tuple is accessed
this.set(value[0], value[1]);
}
} else {
const array = entries as Array<[K, V]>;
for (const kvp of array) {
this.set(kvp[0], kvp[1]);
}
}
}
public clear(): void {
this.items = new LuaTable();
this.nextKey = new LuaTable();
this.previousKey = new LuaTable();
this.firstKey = undefined;
this.lastKey = undefined;
this.size = 0;
}
public delete(key: K): boolean {
const contains = this.has(key);
if (contains) {
this.size--;
// Do order bookkeeping
const next = this.nextKey.get(key);
const previous = this.previousKey.get(key);
if (next !== undefined && previous !== undefined) {
this.nextKey.set(previous, next);
this.previousKey.set(next, previous);
} else if (next !== undefined) {
this.firstKey = next;
this.previousKey.set(next, undefined!);
} else if (previous !== undefined) {
this.lastKey = previous;
this.nextKey.set(previous, undefined!);
} else {
this.firstKey = undefined;
this.lastKey = undefined;
}
this.nextKey.set(key, undefined!);
this.previousKey.set(key, undefined!);
}
this.items.set(key, undefined!);
return contains;
}
public forEach(callback: (value: V, key: K, map: Map<K, V>) => any): void {
for (const key of this.keys()) {
callback(this.items.get(key), key, this);
}
}
public get(key: K): V | undefined {
return this.items.get(key);
}
public has(key: K): boolean {
return this.nextKey.get(key) !== undefined || this.lastKey === key;
}
public set(key: K, value: V): this {
const isNewValue = !this.has(key);
if (isNewValue) {
this.size++;
}
this.items.set(key, value);
// Do order bookkeeping
if (this.firstKey === undefined) {
this.firstKey = key;
this.lastKey = key;
} else if (isNewValue) {
this.nextKey.set(this.lastKey!, key);
this.previousKey.set(key, this.lastKey!);
this.lastKey = key;
}
return this;
}
public [Symbol.iterator](): IterableIterator<[K, V]> {
return this.entries();
}
public entries(): IterableIterator<[K, V]> {
const getFirstKey = () => this.firstKey;
const { items, nextKey } = this;
let key: K | undefined;
let started = false;
return {
[Symbol.iterator](): IterableIterator<[K, V]> {
return this;
},
next(): IteratorResult<[K, V]> {
if (!started) {
started = true;
key = getFirstKey();
} else {
key = nextKey.get(key!);
}
return { done: !key, value: [key!, items.get(key!)] as [K, V] };
},
};
}
public keys(): IterableIterator<K> {
const getFirstKey = () => this.firstKey;
const nextKey = this.nextKey;
let key: K | undefined;
let started = false;
return {
[Symbol.iterator](): IterableIterator<K> {
return this;
},
next(): IteratorResult<K> {
if (!started) {
started = true;
key = getFirstKey();
} else {
key = nextKey.get(key!);
}
return { done: !key, value: key! };
},
};
}
public values(): IterableIterator<V> {
const getFirstKey = () => this.firstKey;
const { items, nextKey } = this;
let key: K | undefined;
let started = false;
return {
[Symbol.iterator](): IterableIterator<V> {
return this;
},
next(): IteratorResult<V> {
if (!started) {
started = true;
key = getFirstKey();
} else {
key = nextKey.get(key!);
}
return { done: !key, value: items.get(key!) };
},
};
}
}