forked from flesler/hashmap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashmap.js
More file actions
190 lines (159 loc) · 4.01 KB
/
hashmap.js
File metadata and controls
190 lines (159 loc) · 4.01 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
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* HashMap - HashMap Class for JavaScript
* @author Ariel Flesler <aflesler@gmail.com>
* @version 2.0.6
* Homepage: https://github.com/flesler/hashmap
*/
(function(factory) {
/* global define */
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof module === 'object') {
// Node js environment
var HashMap = module.exports = factory();
// Keep it backwards compatible
HashMap.HashMap = HashMap;
} else {
// Browser globals (this is window)
this.HashMap = factory();
}
}(function() {
function HashMap(other) {
this.clear();
switch (arguments.length) {
case 0: break;
case 1: this.copy(other); break;
default: multi(this, arguments); break;
}
}
var proto = HashMap.prototype = {
constructor:HashMap,
get:function(key) {
var data = this._data[this.hash(key)];
return data && data[1];
},
set:function(key, value) {
// Store original key as well (for iteration)
var hash = this.hash(key);
if ( !(hash in this._data) ) {
this._count++;
}
this._data[hash] = [key, value];
},
multi:function() {
multi(this, arguments);
},
copy:function(other) {
for (var hash in other._data) {
if ( !(hash in this._data) ) {
this._count++;
}
this._data[hash] = other._data[hash];
}
},
has:function(key) {
return this.hash(key) in this._data;
},
search:function(value) {
for (var key in this._data) {
if (this._data[key][1] === value) {
return this._data[key][0];
}
}
return null;
},
remove:function(key) {
var hash = this.hash(key);
if ( hash in this._data ) {
this._count--;
delete this._data[hash];
}
},
type:function(key) {
var str = Object.prototype.toString.call(key);
var type = str.slice(8, -1).toLowerCase();
// Some browsers yield DOMWindow for null and undefined, works fine on Node
if (type === 'domwindow' && !key) {
return key + '';
}
return type;
},
keys:function() {
var keys = [];
this.forEach(function(_, key) { keys.push(key); });
return keys;
},
values:function() {
var values = [];
this.forEach(function(value) { values.push(value); });
return values;
},
count:function() {
return this._count;
},
clear:function() {
// TODO: Would Object.create(null) make any difference
this._data = {};
this._count = 0;
},
clone:function() {
return new HashMap(this);
},
hash:function(key) {
switch (this.type(key)) {
case 'undefined':
case 'null':
case 'boolean':
case 'number':
case 'regexp':
return key + '';
case 'date':
return '♣' + key.getTime();
case 'string':
return '♠' + key;
case 'array':
var hashes = [];
for (var i = 0; i < key.length; i++) {
hashes[i] = this.hash(key[i]);
}
return '♥' + hashes.join('⁞');
default:
// TODO: Don't use expandos when Object.defineProperty is not available?
if (!key.hasOwnProperty('_hmuid_')) {
key._hmuid_ = ++HashMap.uid;
hide(key, '_hmuid_');
}
return '♦' + key._hmuid_;
}
},
forEach:function(func, ctx) {
for (var key in this._data) {
var data = this._data[key];
func.call(ctx || this, data[1], data[0]);
}
}
};
HashMap.uid = 0;
//- Add chaining to all methods that don't return something
['set','multi','copy','remove','clear','forEach'].forEach(function(method) {
var fn = proto[method];
proto[method] = function() {
fn.apply(this, arguments);
return this;
};
});
//- Utils
function multi(map, args) {
for (var i = 0; i < args.length; i += 2) {
map.set(args[i], args[i+1]);
}
}
function hide(obj, prop) {
// Make non iterable if supported
if (Object.defineProperty) {
Object.defineProperty(obj, prop, {enumerable:false});
}
}
return HashMap;
}));