Class: HashBag

js_cols. HashBag

new HashBag(opt_map, var_args)

This file contains an implementation of a Hash Bag 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 multi set data structure. Adding and removing is O(1). It supports both object and primitive values. Be careful because you can add both 1 and new Number(1), because these are not the same. You can even add multiple new Number(1) because these are not equal.

A set that can contain both primitives and objects. Adding 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: Adding (new Number(1)) twice will yield two distinct elements, because they are two different objects. WARNING: Any object that is added 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 Argument Description
opt_map * <optional>

Map or Object to initialize the map with.

var_args * <repeatable>

If 2 or more arguments are present then they will be used as key-value pairs.

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) → {Number}

Tests whether this set contains the given element.

Parameters:
Name Type Description
element *

The primitive or object to test for.

Returns:

Number entries of the given element.

Type
Number

containsAll(col) → {boolean}

Tests whether this set contains all the values in a given collection. 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 HashBag 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 HashBag, 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 HashBag.

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

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

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)

Add a primitive or an object to the set.

Parameters:
Name Type Description
element *

The primitive or object to add.

insertAll(col)

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

Parameters:
Name Type Description
col Array | Object

A collection containing the elements to add.

intersection(col) → {js_cols.HashBag}

Finds all values that are present in both this bag and the given collection. NOTICE: intersection with bags does not deal with multiple entries of the same element. Example: intersection of HashBags [ 1, 2, 3, 3, 3, 4, 5] and [ 1, 2, 3, 3, 4, 5] would be [1, 2, 3, 4, 5]

Parameters:
Name Type Description
col
Returns:

A new set containing all the key/value pairs (primitives or objects) present in both this set and the given collection.

Type
js_cols.HashBag

isEmpty() → {boolean}

Tests whether this set is empty.

Returns:

True if there are no elements in this set.

Type
boolean

isSubsetOf(col) → {Boolean}

Detects wheter all values present in this bag are also present in the given collection. NOTICE: this operation does not deal with multiple entries of the same element. Example: the HashBag [ 1, 2, 3, 3, 3, 4, 5] would be a subset of the HashBag [ 1, 2, 3, 3, 4, 5]

Parameters:
Name Type Description
col
Returns:

wheter this map is a submap of col

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 Bag. 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 HashBag 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 HashBag, 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.