API Docs for: 1.1.0
Show:

MultiSet Class

Defined in: src\MultiSet.js:21

Implements a multi-set, a set with possibly repeated elements.

This container allows insertion and removal of elements in constant time assuming the implementation of the underlying JavaScript engine manages in amortizing insertion and removal of properties in objects to constant time.

The arguments you specify to the MultiSet constructor are immediately inserted into the container. For example:

var ms = new MultiSet(1, 2, 3); // ms contains 1, 2 and 3

The previous snippet is equivalent to:

var ms = new MultiSet();
ms.add(1);
ms.add(2);
ms.add(3);

Constructor

MultiSet

(
  • [elements]
)

Defined in src\MultiSet.js:21

Parameters:

  • [elements] Object optional multiple

    The elements to add initially to the MultiSet.

Methods

add

(
  • element
)
Function

Defined in src\MultiSet.js:53

Inserts an element into the container in amortized constant time. The element can be of any type (numbers, strings, objects, etc.) and can be inserted many times.

The add method returns a function that removes the element. The returned function is idempotent: it does not have any effect when called again after the first time.

Example:

var ms = new MultiSet(1, 2);
var remove = ms.add(3); // ms now contains three elements: 1, 2 and 3
remove(); // ms now contains two elements: 1 and 2
remove(); // no effect, ms still contains 1 and 2

The returned function returns a boolean value indicating whether the element was present and could be removed or not. false indicates the element was not present because it had already been removed by a previous call.

Example:

var ms = new MultiSet();
var remove = ms.add(3);
if (remove()) {
    alert('removed!');
}
if (remove()) {
    alert('this is never alerted');
}

Parameters:

  • element Any

    The element to be inserted in the MultiSet.

Returns:

Function:

A function that removes the inserted element.

clear

()

Defined in src\MultiSet.js:292

Empties the container: every element is removed and the count is reset to zero.

This method operates in constant time.

Example:

var ms = new MultiSet(1, 2, 3, 4, 5);
ms.clear();
alert(ms.count()); // alerts 0

count

() Number

Defined in src\MultiSet.js:262

Returns the number of elements currently contained.

If an element is inserted more than once, it counts as many times as it is inserted.

This method operates in constant time.

Returns:

Number:

The number of contained elements.

Example:

var ms = new MultiSet(1, 2, 2, 3, 3);
alert(ms.count()); // alerts 5

fastAdd

(
  • [elements...]
)

Defined in src\MultiSet.js:110

Inserts zero or more elements into the container in amortized constant time. The elements can be of any type (numbers, strings, objects, etc.) and can be inserted many times.

This method is faster than add because it doesn't generate any closures; infact it doesn't return anything.

Parameters:

  • [elements...] Any optional

    Zero or more elements to insert in the MultiSet.

Example:

var ms = new MultiSet(1, 2);
ms.fastAdd(3, 4); // ms now contains four elements: 1, 2, 3 and 4

fastForEach

(
  • action
)

Defined in src\MultiSet.js:219

Iterates over the container and calls the specified function action for each iterated element.

The action function receives one argument, the current element. Any return value is ignored.

This method is similar to the forEach method except it can be faster on some browsers because it does not generate a closure (the element's removal function) at each iterated element and does not analyze the return value of the callback function. Infact, the iterated elements cannot be removed and the iteration cannot be interrupted.

You usually use the forEach method, but you may also use fastForEach if your callback function does not use its second argument (the removal function) and never returns false.

Note that the order of iteration is undefined as it depends on the order of iteration over object properties implemented by the underlying JavaScript engine. This is typically the insertion order, which means fastForEach enumerates the elements in the same order they are inserted by add, but you must not rely on that assumption.

Parameters:

  • action Function

    The callback function that gets called for each

    • element Any

      The current element of the iteration. element of the multiset. It receives the current element as an argument. The return value is ignored.

forEach

(
  • action
)
Boolean

Defined in src\MultiSet.js:133

Iterates over the container and calls the specified function action for each iterated element.

The action function receives two arguments: the element and a function that removes it if called. The removing function stays valid forever, even after the whole forEach call is over, and is idempotent: it does not have any effects after it is called once.

The following example inserts some numbers into the container and then removes only the numbers equal to 3:

var ms = new MultiSet(1, 3, 7, 6, 3, 4, 3, 3, 5);
ms.forEach(function (element, remove) {
    if (element === 3) {
        remove();
    }
});
// ms now contains 1, 7, 6, 4, 5

Elements with repetitions are iterated as many times as they are repeated. For example, in the previous snippet the number 3 is iterated (and removed) four times.

Note that the order of iteration is undefined as it depends on the order of iteration over object properties implemented by the underlying JavaScript engine. This is typically the insertion order, which means forEach enumerates the elements in the same order they are inserted by add, but you must not depend on that assumption.

The iteration is interrupted if the action function returns false. The following example adds some numbers to the container, then iterates over it and interrupts when it encounters the number 3:

var ms = new MultiSet(1, 2, 3, 4);
ms.forEach(function (element) {
    if (element === 3) {
        return false;
    }
});

The number 4 is not enumerated.

forEach returns false if the iteration completed and true if it was interrupted, which is suitable for implementing finding algorithms.

Parameters:

  • action Function

    The callback function that gets called for each element of the multiset. It receives the current element and a callback function suitable for deleting it from the MultiSet.

    • element Any

      The current element of the iteration.

    • remove Function

      A function that removes the current element.

Returns:

Boolean:

true if action returned false, false if it did not and all the elements were enumerated.

isEmpty

() Boolean

Defined in src\MultiSet.js:281

Indicates whether the container is empty or not.

Returns:

Boolean:

true if the container is empty, false otherwise.