Class Index | File Index

Classes


Class buckets.Bag

A bag is a special kind of set in which members are allowed to appear more than once.

If the inserted elements are custom objects a function that maps elements to unique strings must be provided at construction time. Example:

function petToUniqueString(pet) {
 return pet.type + ' ' + pet.name;
}

Defined in: <../buckets.js>.

Class Summary
Constructor Attributes Constructor Name and Description
 
buckets.Bag(toStrFunction)
Creates an empty bag.
Method Summary
Method Attributes Method Name and Description
 
add(element, nCopies)
Adds nCopies of the specified object to this bag.
 
Removes all the elements from this bag.
 
contains(element)
Returns true if this bag contains the specified element.
 
count(element)
Counts the number of copies of the specified object in this bag.
 
forEach(callback)
Executes the provided function once per element present in this bag, including multiple copies.
 
Returns true if this bag contains no elements.
 
remove(element, nCopies)
Removes nCopies of the specified object in this bag.
 
size()
Returns the number of elements in this bag.
 
Returns an array containing all of the elements in this bag in arbitrary order, including multiple copies.
 
Returns a set of unique elements in this bag.
Class Detail
buckets.Bag(toStrFunction)
Creates an empty bag.
Parameters:
{function(Object):string=} toStrFunction
Optional function used to convert elements to strings. If the elements aren't strings or if toString() is not appropriate, a custom function which receives an object and returns a unique string must be provided.
Method Detail
{boolean} add(element, nCopies)
Adds nCopies of the specified object to this bag.
Parameters:
{Object} element
Element to add.
{number=} nCopies
The number of copies to add, if this argument is undefined 1 copy is added.
Returns:
{boolean} True unless element is undefined.

clear()
Removes all the elements from this bag.

{boolean} contains(element)
Returns true if this bag contains the specified element.
Parameters:
{Object} element
Element to search for.
Returns:
{boolean} True if this bag contains the specified element, false otherwise.

{number} count(element)
Counts the number of copies of the specified object in this bag.
Parameters:
{Object} element
The object to search for.
Returns:
{number} The number of copies of the object, 0 if not found.

forEach(callback)
Executes the provided function once per element present in this bag, including multiple copies.
Parameters:
{function(Object):*} callback
Function to execute, it is invoked with one argument: the element. To break the iteration you can optionally return false inside the callback.

{boolean} isEmpty()
Returns true if this bag contains no elements.
Returns:
{boolean} true If this bag contains no elements.

{boolean} remove(element, nCopies)
Removes nCopies of the specified object in this bag. If the number of copies to remove is greater than the actual number of copies in the bag, all copies are removed.
Parameters:
{Object} element
Element to remove.
{number=} nCopies
The number of copies to remove, if this argument is undefined 1 copy is removed.
Returns:
{boolean} True if at least 1 element was removed.

{number} size()
Returns the number of elements in this bag.
Returns:
{number} The number of elements in this bag.

{Array} toArray()
Returns an array containing all of the elements in this bag in arbitrary order, including multiple copies.
Returns:
{Array} An array containing all of the elements in this bag.

{buckets.Set} toSet()
Returns a set of unique elements in this bag.
Returns:
{buckets.Set} A set of unique elements in this bag.

Documentation generated by JsDoc Toolkit 2.4.0 on Thu Mar 12 2015 00:38:48 GMT-0500 (COT)