Class buckets.Dictionary
Dictionaries map keys to values; each key can map to at most one value. 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.name; }
Defined in: <../buckets.js>.
Constructor Attributes | Constructor Name and Description |
---|---|
buckets.Dictionary(toStrFunction)
Creates an empty dictionary.
|
Method Attributes | Method Name and Description |
---|---|
clear()
Removes all mappings from this dictionary.
|
|
containsKey(key)
Returns true if this dictionary contains a mapping for the specified key.
|
|
forEach(callback)
Executes the provided function once for each key-value pair
present in this dictionary.
|
|
get(key)
Returns the value 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)
Removes the mapping for this key from this dictionary if it is present.
|
|
set(key, value)
Associates the specified value with the specified key in this dictionary.
|
|
size()
Returns the number of keys in this dictionary.
|
|
values()
Returns an array containing all of the values in this dictionary.
|
Class Detail
buckets.Dictionary(toStrFunction)
Creates an empty dictionary.
- Parameters:
- {function(Object):string=} toStrFunction
- optional function used 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.
Method Detail
clear()
Removes all mappings from this dictionary.
{boolean}
containsKey(key)
Returns true if this dictionary contains a mapping for the specified key.
- Parameters:
- {Object} key
- key whose presence in this dictionary is to be tested.
- Returns:
- {boolean} true if this dictionary contains a mapping for the specified key.
forEach(callback)
Executes the provided function once for each key-value pair
present in this dictionary.
- Parameters:
- {function(Object|Object):*} callback
- function to execute, it is invoked with two arguments: key and value. To break the iteration you can optionally return false.
{*}
get(key)
Returns the value to which this dictionary maps the specified key.
Returns undefined if this dictionary contains no mapping for this key.
- Parameters:
- {Object} key
- key whose associated value is to be returned.
- Returns:
- {*} the value to which this dictionary maps the specified key or undefined if the map contains no mapping for this 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)
Removes the mapping for this key from this dictionary if it is present.
- Parameters:
- {Object} key
- key whose mapping is to be removed from the dictionary.
- Returns:
- {*} previous value associated with specified key, or undefined if there was no mapping for key.
{*}
set(key, value)
Associates the specified value with the specified key in this dictionary.
If the dictionary previously contained a mapping for this key, the old
value is replaced by the specified value.
- Parameters:
- {Object} key
- key with which the specified value is to be associated.
- {Object} value
- value to be associated with the specified key.
- Returns:
- {*} previous value associated with the specified key, or undefined if there was no mapping for the key or if the key/value are undefined.
{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.