This repository has been archived by the owner on Feb 15, 2021. It is now read-only.
/
Hashmap.ts
76 lines (60 loc) · 1.75 KB
/
Hashmap.ts
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
module Sef {
export class Hashmap {
private _keys: any[];
private _values: any[];
private _index: any;
constructor() {
this._keys = [];
this._values = [];
this._index = {};
}
public set(key, value): void {
var keyIndex = this._getIndex(key);
if (keyIndex >= 0) {
this._values[keyIndex] = value;
} else {
this._values.push(value);
this._index[key] = this._keys.push(key) - 1;
}
}
public remove(key): any {
var keyIndex = this._getIndex(key);
if (keyIndex >= 0) {
delete this._index[key];
this._values.splice(keyIndex, 1);
return this._keys.splice(keyIndex, 1);
}
}
public get(key): any {
var keyIndex = this._getIndex(key);
if (keyIndex >= 0) {
return this._values[keyIndex];
}
return null;
}
private _getIndex(testKey): number {
return this._keys.indexOf(testKey);
}
public keys(): any[] {
return this._keys;
}
public values(): any[] {
return this._values;
}
public has(testKey): boolean {
var keyIndex = this._getIndex(testKey);
if (keyIndex >= 0) {
return true;
}
return false;
}
public forEach(action): boolean {
var i = 0,
len = this._keys.length;
for (; i < len; i++) {
action(this._keys[i], this._values[i]);
}
return true;
}
}
}