Created
March 22, 2011 08:42
-
-
Save netroy/880942 to your computer and use it in GitHub Desktop.
a HashSet implementation in JS (for NodeJS)... no prototype involved to prevent inheritence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
** This is broken... working on a Hashtable implementation first & then use that instead of plain Objects to implement this | |
*/ | |
HashSet = function HashSet(){ | |
if(!(this instanceof HashSet)) return; | |
var _data = {}; | |
var _length = 0; | |
var _DEFAULT = new Date(); | |
this.contains = function(val){ | |
val = val.toString(); | |
return (!!_data[val] && _data.hasOwnProperty(val)); | |
}; | |
this.add = function(val){ | |
if(!this.contains(val.toString())){ | |
_length++; | |
} | |
_data[val.toString()] = val; | |
}; | |
this.remove = function(val){ | |
val = val.toString(); | |
if(!this.contains(val)){ | |
return false; | |
}else{ | |
delete _data[val.toString()]; | |
_length--; | |
return true; | |
} | |
}; | |
this.clear = function(){ | |
for(var val in _data){ | |
if(_data.hasOwnProperty(val)){ | |
delete _data[val]; | |
} | |
} | |
_length = 0; | |
}; | |
this.isEmpty = function(){ | |
return (_length === 0); | |
}; | |
this.size = function(){ | |
return _length; | |
}; | |
this.toArray = function(){ | |
_data.length = _length; | |
var arr = Array.prototype.slice.call(_data); | |
delete _data.length; | |
return arr; | |
}; | |
} | |
exports.HashSet = HashSet; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment