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 of 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 unique strings must be provided. Example:
function petToString(pet) { return pet.type + ' ' + 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 has at least one value associatted with 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 value from the array of values associated with the
specified key.
|
|
set(key, value)
Adds the value to an 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 has at least one value associatted with the specified key.
- Parameters:
- {Object} key
- Key whose presence in this dictionary is to be tested.
- Returns:
- {boolean} True if this dictionary has 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 value 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 an array associated with the specified key if
it is not already present.
- Parameters:
- {Object} key
- Key to which the specified value is to be associated.
- {Object} value
- The value to associate.
- 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.