Object 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;
}

Object Summary
Constructor Name and Description
buckets.Bag(toStrFunction)
Creates an empty bag.
Method Summary
Method Name and Description
buckets.Bag.add(element, nCopies)
Adds nCopies of the specified element to the bag.
buckets.Bag.clear()
Removes all the elements from the bag.
buckets.Bag.contains(element)
Returns true if the bag contains the specified element.
buckets.Bag.count(element)
Counts the number of copies of the specified element in the bag.
buckets.Bag.equals(other)
Returns true if the bag is equal to another bag.
buckets.Bag.forEach(callback)
Executes the provided function once per element present in the bag, including multiple copies.
buckets.Bag.isEmpty()
Returns true if the bag contains no elements.
buckets.Bag.remove(element, nCopies)
Removes nCopies of the specified element from the bag.
buckets.Bag.size()
Returns the number of elements in the bag, including duplicates.
buckets.Bag.toArray()
Returns an array containing all the elements in the bag in no particular order, including multiple copies.
buckets.Bag.toSet()
Returns a set of unique elements in the bag.
Object Detail
buckets.Bag(toStrFunction)
Creates an empty bag.
Parameters:
{function(Object):string=} toStrFunction
Optional function to convert elements to unique 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
<static> {boolean} buckets.Bag.add(element, nCopies)
Adds nCopies of the specified element to the 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.

<static> buckets.Bag.clear()
Removes all the elements from the bag.

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

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

<static> {boolean} buckets.Bag.equals(other)
Returns true if the bag is equal to another bag. Two bags are equal if they have the same elements and same number of copies per element.
Parameters:
{buckets.Bag} other
The other bag.
Returns:
{boolean} True if the bag is equal to the given bag.

<static> buckets.Bag.forEach(callback)
Executes the provided function once per element present in the bag, including multiple copies.
Parameters:
{function(Object):*} callback
Function to execute, it's invoked with an element as argument. To break the iteration you can optionally return false in the callback.

<static> {boolean} buckets.Bag.isEmpty()
Returns true if the bag contains no elements.
Returns:
{boolean} True if the bag contains no elements.

<static> {boolean} buckets.Bag.remove(element, nCopies)
Removes nCopies of the specified element from the 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 copy was removed.

<static> {number} buckets.Bag.size()
Returns the number of elements in the bag, including duplicates.
Returns:
{number} The number of elements in the bag.

<static> {Array} buckets.Bag.toArray()
Returns an array containing all the elements in the bag in no particular order, including multiple copies.
Returns:
{Array} An array containing all the elements in the bag.

<static> {buckets.Set} buckets.Bag.toSet()
Returns a set of unique elements in the bag.
Returns:
{buckets.Set} A set of unique elements in the bag.