-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashmap.js
48 lines (41 loc) · 1012 Bytes
/
hashmap.js
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
// Generic hash map
function HashMap(opt_json) {
this.internal = {};
this.numEntries = 0;
if (opt_json) {
for (var key in opt_json) {
this.put(key, opt_json[key]);
}
}
}
HashMap.prototype.put = function(key, value) {
if (!this.has(key)) this.numEntries++;
this.internal[key] = value;
}
HashMap.prototype.get = function(key) {
if (this.isEmpty()) return undefined;
return this.internal[key];
}
HashMap.prototype.has = function(key) {
return !(this.internal[key] === undefined);
}
HashMap.prototype.remove = function(key) {
if (this.has(key)) {
delete this.internal[key];
this.numEntries--;
}
}
HashMap.prototype.size = function() {
return this.numEntries;
}
HashMap.prototype.isEmpty = function() {
return this.size() == 0;
}
HashMap.prototype.toString = function() {
if (this.isEmpty()) return 'HashMap is empty';
var out = '';
for (var key in this.internal) {
out += key + ' ' + this.internal[key] + '<br/>';
}
return out;
}