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 which converts elements to unique strings must be provided. Example:
function petToString(pet) { return pet.name; }
Defined in: <../buckets.js>.
Constructor Attributes | Constructor Name and Description |
---|---|
buckets.Bag(toStringFunction)
Creates an empty bag.
|
Method Attributes | Method Name and Description |
---|---|
add(element, nCopies)
Adds nCopies of the specified object to this bag.
|
|
clear()
Removes all of 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 for each element
present in this bag, including multiple copies.
|
|
isEmpty()
Returns true if this bag contains no elements.
|
|
remove(element, nCopies)
Removes nCopies of the specified object to this bag.
|
|
size()
Returns the number of elements in this bag.
|
|
toArray()
Returns an array containing all of the elements in this big in arbitrary order,
including multiple copies.
|
|
toSet()
Returns a set of unique elements in this bag.
|
Class Detail
buckets.Bag(toStringFunction)
Creates an empty bag.
- Parameters:
- {function(Object):string=} toStringFunction
- 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 of 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 for each 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.
{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 to 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 big 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.