jshashset 3.0
Contents
Introduction
jshashset is a JavaScript implementation of HashSet, as found in Java or C#'s standard libraries. It depends on jshashtable and uses the keys of a jshashtable hash table as the underlying set.
jshashset was first included as a separate download for version 2.1 of jshashtable and is included with jshashtable from version 3.0.
Set-up
-
Download the code
Download jshashset. You can download a compressed or uncompressed version of
jshashset.jswhich are functionally identical or a zip containing compressed and uncompressed code for both jshashtable and jshashset plus documentation. -
Include jshashtable and jshashset in your page
Include
jshashtable.jsandjshashset.jsin that order in script tags in your page. These files create two objects in the global scope, which areHashtableandHashSet. -
Create your hash set
Create your hash set, as in the example below. Any non-null, non-undefined JavaScript object can be used as member of the set.
<script type="text/javascript" src="jshashtable.js"></script> <script type="text/javascript" src="jshashset.js"></script> <script type="text/javascript"> var dinosaurs = new HashSet(); dinosaurs.add("Triceratops"); dinosaurs.add("Diplodocus"); dinosaurs.add("Stegosaurus"); alert( dinosaurs.values.join(",") ); /* Triceratops,Diplodocus,Stegosaurus */ </script>
Public API
Constructors
-
HashSet()Creates a new, empty hash set.
-
HashSet(Object options)New in version 3.0
Creates a new, empty hash set with the supplied options.
Option properties:-
hashCodeA function that provides hash codes for objects placed in the set. It is passed the object to be hashed as its only parameter. If not provided, the set checks whether the object has ahashCode()method, and if not, callstoString()on the object. -
equalsA function that checks for equality between two objects with the same hash code. If two objects that are considered equal then only one can be in the set at any one time.
This function is passed the two objects to be compared as its parameters. If not provided, the set checks whether either object being compared has an
equals()method, and if not, compares the objects using the===operator. -
replaceDuplicateKeyNew in version 3.0
Controls what happens when
add()is called with an object that is equal to an existing object in the set. IfreplaceDuplicateKeyistrue, the existing key is always replaced by the new key. Otherwise, the existing key is left in place (which is what always happened prior to version 3.0). The default istrue; prior to version 3.0.
-
-
HashSet(Function hashingFunction, Function equalityFunction)Creates a new, empty hash set with the supplied hashing function and equality function. This form maintains backwards compatibility with versions prior to 3.0. The following line
var set = new HashSet(hashingFunction, equalityFunction);
... is equivalent to
var set = new HashSet( { hashCode: hashingFunction, equals: equalityFunction } );Parameters:-
hashingFunctionSee above.
-
equalityFunctionSee above.
-
Methods
-
void add(mixed value)Adds the specified object or primitive to the set.valuereplaces any member of the set equal to it, unlessreplaceDuplicateKeywas set tofalsein the constructor. -
void addAll(Array arr)Adds all members of an arrayarrto the set in order. Each member ofarrreplaces any member of the set equal to it, unlessreplaceDuplicateKeywas set tofalsein the constructor, in which case the original value is left in place. -
Array values()Returns an array containing all the members of the set in unspecified order.
-
void remove(mixed key)Removes the specified value from the set.
-
Boolean contains(mixed value)Returns whether the set contains the specified value.
-
void clear()Removes all members from the set.
-
Number size()Returns the number of members contained in the set.
-
Boolean isEmpty()Returns
trueif the set contains no members,falseotherwise. -
Boolean isSubsetOf(HashSet set)Returns
trueif every member this set is also a member ofset,falseotherwise. -
HashSet clone()Creates and returns a shallow copy of the set, using the same options provided to the set when it was constructed.
-
HashSet intersection(HashSet set)Creates and returns a new
HashSetcontaining only those elements that are contained in both this set andset. -
HashSet union(HashSet set)Creates and returns a new
HashSetcontaining those elements that are contained either of or both this set andset. -
HashSet complement(HashSet set)Creates and returns a new
HashSetcontaining those elements that are contained in this set but notset.