Skip to content

Commit 978417a

Browse files
committed
Initial rough map implementation
1 parent 0a9b905 commit 978417a

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

src/lualib/Map.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
class Map<TKey, TValue> {
2+
public size: number;
3+
4+
private items: {[key: string]: TValue};
5+
6+
public Map(other: any) {
7+
this.items = {};
8+
this.size = 0;
9+
10+
if (other) {
11+
this.size = other.size;
12+
other.forEach((value, key) => this.items[key] = value);
13+
}
14+
}
15+
16+
public clear(): void {
17+
this.items = {};
18+
this.size = 0;
19+
}
20+
21+
public delete(key: TKey): boolean {
22+
const contains = this.has(key);
23+
this.items[key as any] = undefined;
24+
this.size = this.size - 1;
25+
return contains;
26+
}
27+
28+
public entries(): Array<[TKey, TValue]> {
29+
const out = [];
30+
for (const key in this.items) {
31+
out[out.length + 1] = [key, this.items[key]];
32+
}
33+
return out;
34+
}
35+
36+
public forEach(callback: (value: TValue, key: TKey, map: Map<TKey, TValue>) => any): void {
37+
for (const key in this.items) {
38+
callback(this.items[key], key as any, this);
39+
}
40+
}
41+
42+
public get(key: TKey): TValue {
43+
return this.items[key as any];
44+
}
45+
46+
public has(key: TKey): boolean {
47+
return this.items[key as any] != undefined;
48+
}
49+
50+
public keys(): TKey[] {
51+
const out = [];
52+
for (const key in this.items) {
53+
out[out.length + 1] = key;
54+
}
55+
return out;
56+
}
57+
58+
public set(key: TKey, value: TValue): Map<TKey, TValue> {
59+
if (!this.has(key)) {
60+
this.size = this.size + 1;
61+
}
62+
this.items[key as any] = value;
63+
return this;
64+
}
65+
66+
public values(): TValue[] {
67+
const out = [];
68+
for (const key in this.items) {
69+
out[out.length + 1] = this.items[key];
70+
}
71+
return out;
72+
}
73+
}

0 commit comments

Comments
 (0)