File size: 1,210 Bytes
bc20498 |
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 |
var UniqueIDGeneretor = require('./UniqueIDGeneretor');
function HashSet() {
this.set = {};
}
;
HashSet.prototype.add = function (obj) {
var theId = UniqueIDGeneretor.createID(obj);
if (!this.contains(theId))
this.set[theId] = obj;
};
HashSet.prototype.remove = function (obj) {
delete this.set[UniqueIDGeneretor.createID(obj)];
};
HashSet.prototype.clear = function () {
this.set = {};
};
HashSet.prototype.contains = function (obj) {
return this.set[UniqueIDGeneretor.createID(obj)] == obj;
};
HashSet.prototype.isEmpty = function () {
return this.size() === 0;
};
HashSet.prototype.size = function () {
return Object.keys(this.set).length;
};
//concats this.set to the given list
HashSet.prototype.addAllTo = function (list) {
var keys = Object.keys(this.set);
var length = keys.length;
for (var i = 0; i < length; i++) {
list.push(this.set[keys[i]]);
}
};
HashSet.prototype.size = function () {
return Object.keys(this.set).length;
};
HashSet.prototype.addAll = function (list) {
var s = list.length;
for (var i = 0; i < s; i++) {
var v = list[i];
this.add(v);
}
};
module.exports = HashSet;
|