Class buckets.MultiDictionary
A multi dictionary is a special kind of dictionary that holds multiple values against each key. Setting a value into the dictionary will add the value to an array at that key. Getting a key will return an array, holding all the values set to that key. This implementation accepts any kind of objects as keys.
If the keys are custom objects a function which converts keys to strings must be provided. Example:
function petToString(pet) { return pet.name; }
If the values are custom objects a function to check equality between values must be provided. Example:
function petsAreEqualByAge(pet1,pet2) { return pet1.age===pet2.age; }
Defined in: <../buckets.js>.
Constructor Attributes | Constructor Name and Description |
---|---|
buckets.MultiDictionary(toStrFunction, valuesEqualsFunction)
Creates an empty multi dictionary.
|
Method Attributes | Method Name and Description |
---|---|
clear()
Removes all mappings from this dictionary.
|
|
containsKey(key)
Returns true if this dictionary at least one value associatted the specified key.
|
|
get(key)
Returns an array holding the values to which this dictionary maps
the specified key.
|
|
isEmpty()
Returns true if this dictionary contains no mappings.
|
|
keys()
Returns an array containing all of the keys in this dictionary.
|
|
remove(key, value)
Removes the specified values from the array of values associated with the
specified key.
|
|
set(key, value)
Adds the value to the array associated with the specified key, if
it is not already present.
|
|
size()
Returns the number of keys in this dictionary.
|
|
values()
Returns an array containing all of the values in this dictionary.
|
Class Detail
buckets.MultiDictionary(toStrFunction, valuesEqualsFunction)
Creates an empty multi dictionary.
- Parameters:
- {function(Object):string=} toStrFunction
- optional function to convert keys to strings. If the keys aren't strings or if toString() is not appropriate, a custom function which receives a key and returns a unique string must be provided.
- {function(Object|Object):boolean=} valuesEqualsFunction
- optional function to check if two values are equal.
Method Detail
clear()
Removes all mappings from this dictionary.
{boolean}
containsKey(key)
Returns true if this dictionary at least one value associatted the specified key.
- Parameters:
- {Object} key
- key whose presence in this dictionary is to be tested.
- Returns:
- {boolean} true if this dictionary at least one value associatted the specified key.
{Array}
get(key)
Returns an array holding the values to which this dictionary maps
the specified key.
Returns an empty array if this dictionary contains no mappings for this key.
- Parameters:
- {Object} key
- key whose associated values are to be returned.
- Returns:
- {Array} an array holding the values to which this dictionary maps the specified key.
{boolean}
isEmpty()
Returns true if this dictionary contains no mappings.
- Returns:
- {boolean} true if this dictionary contains no mappings.
{Array}
keys()
Returns an array containing all of the keys in this dictionary.
- Returns:
- {Array} an array containing all of the keys in this dictionary.
{*}
remove(key, value)
Removes the specified values from the array of values associated with the
specified key. If a value isn't given, all values associated with the specified
key are removed.
- Parameters:
- {Object} key
- key whose mapping is to be removed from the dictionary.
- {Object=} value
- optional argument to specify the value to remove from the array associated with the specified key.
- Returns:
- {*} true if the dictionary changed, false if the key doesn't exist or if the specified value isn't associated with the specified key.
{boolean}
set(key, value)
Adds the value to the array associated with the specified key, if
it is not already present.
- Parameters:
- {Object} key
- key with which the specified value is to be associated.
- {Object} value
- the value to add to the array at the key
- Returns:
- {boolean} true if the value was not already associated with that key.
{number}
size()
Returns the number of keys in this dictionary.
- Returns:
- {number} the number of key-value mappings in this dictionary.
{Array}
values()
Returns an array containing all of the values in this dictionary.
- Returns:
- {Array} an array containing all of the values in this dictionary.