HasSet
A simple hash set in JavaScript
Create a new HashSet
var HashSet = ; //Create an empty hash setvar hashset = ; //Create a hash set an initialize it with a value 'a'var hashset = 'a'; //Create a hash set an initialize it with a set of valuesvar hashset = 'a' 'b' 'c'; //Create a hash set an initialize it with 3 different valuesvar hashset = 1 '1' 1;
Properties
length
Get the length of the hash set
hashsetlength;
Methods
equals
Test whether current hash set equals to another
var hashset1 = 1 2 3 4 5;var hashset2 = 1 2 3 4 5;var result = hashset1; //true
contains
Check whether a value is in the hash set
hashset;
add
Add a new value into the hash set
hashset;
remove
Remove a value from the hash set
hashset;
Removing an non-existing value will not trigger any error
toArray
Convert and output all values into an array
var hashset = 1 2 3;hashset; // [1, 2, 3]
There is no gurantee the values of the output array the same sequence as value added to the hash set.
isSubSetOf
Test whether a hash set is a sub set of another. Empty hash set is a sub set of any other hash set including another empty hash set.
var hashset1 = ;var hashset2 = ;var result = hashset1;
isSuperSetOf
Test whether a hash set is a super set of another. Any hash set is a super set of empty hahs set including an empty hash set.
var hashset1 = 1 2 3;var hashset2 = 1 2 3;var hashset3 = ;var result1 = hashset1; //truevar result2 = hashset2; //truevar result3 = hashset1; //truevar result3 = hashset3; //false
unionWith
Union current hash set with another
var hashset1 = 1 2;var hashset2 = 2 3;hashset1;hashset1; //truehashset1; //truehashset1; //true
intersectWith
Intersect current hash set with another
var hashset1 = 1 2 3;var hashset2 = 2 3 4;hashset1;hashset1length; //2hashset1; //truehashset1; //truehashset1; //false
Test
Make sure mocha
is installed globally
npm install mocha -g
Run npm test
to run unit test
Dependencies
License
MIT