-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathset.js
More file actions
35 lines (30 loc) · 741 Bytes
/
set.js
File metadata and controls
35 lines (30 loc) · 741 Bytes
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
function Set(hashFunction) {
this._hashFunction = hashFunction || JSON.stringify;
this._values = {};
this._size = 0;
}
Set.prototype = {
add: function add(value) {
if (!this.contains(value)) {
this._values[this._hashFunction(value)] = value;
this._size++;
}
},
remove: function remove(value) {
if (this.contains(value)) {
delete this._values[this._hashFunction(value)];
this._size--;
}
},
contains: function contains(value) {
return typeof this._values[this._hashFunction(value)] !== "undefined";
},
size: function size() {
return this._size;
},
each: function each(iteratorFunction, thisObj) {
for (var value in this._values) {
iteratorFunction.call(thisObj, this._values[value]);
}
}
};