forked from smikes/fez
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset.js
More file actions
64 lines (51 loc) · 1.36 KB
/
set.js
File metadata and controls
64 lines (51 loc) · 1.36 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
//A very simple set. Fix this pretty quickly, as it will be a performance hit.
function Set(id) {
if(id === undefined)
id = function(el) {
return el.id;
};
this._id = id;
this._set = {};
Object.defineProperty(this._set, "length", {value : 0,
writable : true,
configurable : false,
enumerable : false});
}
Set.prototype.insert = function(el) {
if (this._id(el) === undefined) return false;
if(!this.exists(el)) {
this._set[this._id(el)] = el;
this._set.length += 1;
return true;
}
return false;
};
Set.prototype.remove = function(el) {
if(this.exists(el)) {
delete this._set[this._id(el)];
this._set.length -= 1;
return true;
}
return false;
};
Set.prototype.exists = function(el) {
// This works even if the value is explicitly undefined like {name: undefined}
return (this._id(el) in this._set);
};
Set.prototype.array = function() {
var array = [];
for (var key in this._set) {
array.push(this._set[key]);
}
return array;
};
Set.prototype.clone = function() {
var other = new Set(this._id);
for (var key in this._set)
other._set[key] = this._set[key];
return other;
};
Set.prototype.length = function () {
return this._set.length;
}
module.exports = Set;