jshashset 3.0

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.

Top

Set-up

  1. Download the code

    Download jshashset. You can download a compressed or uncompressed version of jshashset.js which are functionally identical or a zip containing compressed and uncompressed code for both jshashtable and jshashset plus documentation.

  2. Include jshashtable and jshashset in your page

    Include jshashtable.js and jshashset.js in that order in script tags in your page. These files create two objects in the global scope, which are Hashtable and HashSet.

  3. 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>
    

Top

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:
    • hashCode
      A 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 a hashCode() method, and if not, calls toString() on the object.
    • equals

      A 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.

    • replaceDuplicateKey

      New in version 3.0

      Controls what happens when add() is called with an object that is equal to an existing object in the set. If replaceDuplicateKey is true, 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 is true; 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:
    • hashingFunction

      See above.

    • equalityFunction

      See above.

Methods

  • void add(mixed value)
    Adds the specified object or primitive to the set. value replaces any member of the set equal to it, unless replaceDuplicateKey was set to false in the constructor.
  • void addAll(Array arr)
    Adds all members of an array arr to the set in order. Each member of arr replaces any member of the set equal to it, unless replaceDuplicateKey was set to false in 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 true if the set contains no members, false otherwise.

  • Boolean isSubsetOf(HashSet set)

    Returns true if every member this set is also a member of set, false otherwise.

  • 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 HashSet containing only those elements that are contained in both this set and set.

  • HashSet union(HashSet set)

    Creates and returns a new HashSet containing those elements that are contained either of or both this set and set.

  • HashSet complement(HashSet set)

    Creates and returns a new HashSet containing those elements that are contained in this set but not set.

Top