Class: HashSet

js_cols. HashSet

new HashSet(opt_values)

This file contains an implementation of a Hash Set structure. It is based on goog.structs.Set from the google closure library. The assymptotic running time for important operations are below:

  Method                 big-O
----------------------------------------------------------------------------
- clear                  O(1)
- clone                  O(n logn)
- contains               O(1)
- containsAll            O(m) m is the cardinality of the supplied collection
- every                  O(n * O(f)) f is the function supplied as argument
- filter                 O(n * O(f)) f is the function supplied as argument
- forEach                O(n * O(f)) f is the function supplied as argument
- getValues              O(n)
- insert                 O(1)
- insertAll              O(m) m is the cardinality of the supplied collection
- map                    O(n * O(f)) f is the function supplied as argument
- remove                 O(1)
- removeAll              O(m logn) m is the cardinality of the supplied collection
- some                   O(n * O(f)) f is the function supplied as argument
- contains               O(n * O(f)) f is the function supplied as argument

This class implements a set data structure. Inserting and removing is O(1). It supports both object and primitive values. Be careful because you can insert both 1 and new Number(1), because these are not the same. You can even insert multiple new Number(1) because these are not equal.

A set that can contain both primitives and objects. Inserting and removing elements is O(1). Primitives are treated as identical if they have the same type and convert to the same string. Objects are treated as identical only if they are references to the same object. WARNING: A js_cols.Set can contain both 1 and (new Number(1)), because they are not the same. WARNING: inserting (new Number(1)) twice will yield two distinct elements, because they are two different objects. WARNING: Any object that is inserted to a js_cols.Set will be modified! Because js_cols.getUid() is used to identify objects, every object in the set will be mutated. This liberates the user of implementing a getHashCode function, and improves performance as hashing collisions are avoided.

Parameters:
Name Type Description
opt_values Array | Object

Initial values to start with.

Methods

clear()

Removes all elements from this set.

clone() → {js_cols.Set}

Creates a shallow clone of this set.

Returns:

A new set containing all the same elements as this set.

Type
js_cols.Set

contains(element) → {boolean}

Tests whether this set contains the given element.

Parameters:
Name Type Description
element *

The primitive or object to test for.

Returns:

True if this set contains the given element.

Type
boolean

containsAll(col) → {boolean}

Tests whether this set contains all the values in a given collection. Checks that all keys contained in the collection are also contained as keys in the map. If the collection has a notion of keys (a Map), the keys of the collection will be interpreted as values in this set. Repeated elements in the collection are ignored, e.g. (new js_cols.Set([1, 2])).containsAll([1, 1]) is True.

Parameters:
Name Type Description
col Object

A collection-like object.

Returns:

True if the set contains all elements.

Type
boolean

equals(col) → {boolean}

Tests whether the given collection consists of the same elements as this set, regardless of order, without repetition. Primitives are treated as equal if they have the same type and convert to the same string; objects are treated as equal if they are references to the same object. This operation is O(n).

Parameters:
Name Type Description
col Object

A collection.

Returns:

True if the given collection consists of the same elements as this set, regardless of order, without repetition.

Type
boolean

every(f, opt_obj) → {boolean}

Calls a function on each item in the HashSet and returns true only if every function call returns a true-like value.

Parameters:
Name Type Argument Description
f function

The function to call for each item. The function takes three arguments: the value, the key, and the HashSet, and returns a boolean.

opt_obj Object <optional>

The object context to use as "this" for the function.

Returns:

Whether f evaluates to true for every item in the HashSet.

Type
boolean

filter(f, opt_obj) → {Array}

Calls a function on each element in the HashSet, if the function returns true, the element is inserted into an Array that is returned when the tree is fully traversed

Parameters:
Name Type Argument Description
f function

The function to call for each item. The function takes three arguments: the value, the key, and the HashSet.

opt_obj Object <optional>

The object context to use as "this" for the function.

Returns:

The elements that evaluated to true in the function calls

Type
Array

forEach(f, opt_obj)

Calls a function on each item in the HashSet.

Parameters:
Name Type Argument Description
f function

The function to call for each item. The function takes three arguments: the value, the key, and the HashSet.

opt_obj Object <optional>

The object context to use as "this" for the function.

getCount() → {number}

Returns:

The number of elements in the set.

Type
number

getValues() → {Array}

Returns an array containing all the elements in this set.

Returns:

An array containing all the elements in this set.

Type
Array

insert(element)

insert a primitive or an object to the set.

Parameters:
Name Type Description
element *

The primitive or object to insert.

insertAll(col)

inserts all the values in the given collection to this set.

Parameters:
Name Type Description
col Array | Object

A collection containing the elements to insert.

intersection(col) → {js_cols.Set}

Finds all values that are present in both this set and the given collection. WARNING: This operation is not guaranteed to work correctly if col is a Map. This operation is O(n).

Parameters:
Name Type Description
col js_cols.Set | Array | Object

A collection.

Returns:

A new set containing all the values (primitives or objects) present in both this set and the given collection.

Type
js_cols.Set

isEmpty() → {boolean}

Tests whether this set is empty.

Returns:

True if there are no elements in this set.

Type
boolean

isSubsetOf(col) → {boolean}

Tests whether the given collection contains all the elements in this set. WARNING: This operation is not guaranteed to work correctly if col is a Map. Primitives are treated as equal if they have the same type and convert to the same string; objects are treated as equal if they are references to the same object. This operation is O(n).

Parameters:
Name Type Description
col Object

A collection.

Returns:

True if this set is a subset of the given collection.

Type
boolean

map(f, opt_obj) → {Array}

Calls a function on each item in the HashSet and returns the results of those calls in an array.

Parameters:
Name Type Argument Description
f function

The function to call for each item. The function takes three arguments: the value, the key, and the ABTreeMap.

opt_obj Object <optional>

The object context to use as "this" for the function.

Returns:

The results of the function calls for each item in the HashSet.

Type
Array

remove(element) → {boolean}

Removes the given element from this set.

Parameters:
Name Type Description
element *

The primitive or object to remove.

Returns:

Whether the element was found and removed.

Type
boolean

removeAll(col)

Removes all values in the given collection from this set. If the collection has a notion of keys (a Map), the keys will be treated as values in this set.

Parameters:
Name Type Description
col Array | Object

A collection containing the elements to remove.

some(f, opt_obj) → {boolean}

Calls a function on each item in the HashSet and returns true if any of those function calls returns a true-like value.

Parameters:
Name Type Argument Description
f function

The function to call for each item. The function takes three arguments: the value, the key and the HashSet, and returns a boolean.

opt_obj Object <optional>

The object context to use as "this" for the function.

Returns:

Whether f evaluates to true for at least one item in the RedBlackSet.

Type
boolean
js_cols - powerful collection classes for JavaScript
js_cols Copyright © 2010-2015 Thomas Stjernegaard Jeppesen.
Documentation generated by JSDoc 3.3.0-alpha13 on Sat Dec 27th 2014 using the DocStrap template.