The goog.structs.LinkedMap Class

goog.structs.LinkedMap(opt_maxCount, opt_cache)

Class for a LinkedMap datastructure, which combines O(1) map access for key/value pairs with a linked list for a consistent iteration order. Sample usage:

 var m = new LinkedMap();
 m.set('param1', 'A');
 m.set('param2', 'B');
 m.set('param3', 'C');
 alert(m.getKeys()); // param1, param2, param3

 var c = new LinkedMap(5, true);
 for (var i = 0; i < 10; i++) {
   c.set('entry' + i, false);
 }
 alert(c.getKeys()); // entry9, entry8, entry7, entry6, entry5

 c.set('entry5', true);
 c.set('entry1', false);
 alert(c.getKeys()); // entry1, entry5, entry9, entry8, entry7
 
opt_maxCount {number=}
The maximum number of objects to store in the LinkedMap. If unspecified or 0, there is no maximum.
opt_cache {boolean=}
When set, the LinkedMap stores items in order from most recently used to least recently used, instead of insertion order.

.clear()

Removes all entries in this object.

.contains(value)

Tests whether a provided value is currently in the LinkedMap. This does not affect item ordering in cache-style LinkedMaps.

value {Object}
The value to check for.
returns {boolean}
Whether the value is in the LinkedMap.

.containsKey(key)

Tests whether a provided key is currently in the LinkedMap. This does not affect item ordering in cache-style LinkedMaps.

key {string}
The key to check for.
returns {boolean}
Whether the key is in the LinkedMap.

.every(f, opt_obj)

Calls a function on each item in the LinkedMap and returns true only if every function call returns a true-like value.

f {Function}
The function to call for each item. The function takes three arguments: the value, the key, and the Cache, and returns a boolean.
opt_obj {Object=}
The object context to use as "this" for the function.
returns {boolean}
Whether f evaluates to true for every item in the Cache.
@see
goog.structs.some

.forEach(f, opt_obj)

Calls a function on each item in the LinkedMap.

f {Function}
The function to call for each item. The function takes three arguments: the value, the key, and the LinkedMap.
opt_obj {Object=}
The object context to use as "this" for the function.
@see
goog.structs.forEach

.get(key, opt_val)

Retrieves the value for a given key. If this is a caching LinkedMap, the entry will become the most recently used.

key {string}
The key to retrieve the value for.
opt_val {*=}
A default value that will be returned if the key is not found, defaults to undefined.
returns {*}
The retrieved value.

.getCount()

returns {number}
The number of items currently in the LinkedMap.

.getKeys()

returns {Array.<string>}
The list of the keys in the appropriate order for this LinkedMap.

.getValues()

returns {!Array}
The list of the values in the appropriate order for this LinkedMap.

.isEmpty()

returns {boolean}
True if the cache is empty, false if it contains any items.

.map(f, opt_obj)

Calls a function on each item in the LinkedMap and returns the results of those calls in an array.

f {!Function}
The function to call for each item. The function takes three arguments: the value, the key, and the LinkedMap.
opt_obj {Object=}
The object context to use as "this" for the function.
returns {!Array}
The results of the function calls for each item in the LinkedMap.
@see
goog.structs.map

.peek()

Returns the value of the first node without making any modifications.

returns {*}
The value of the first node or undefined if the map is empty.

.peekLast()

Returns the value of the last node without making any modifications.

returns {*}
The value of the last node or undefined if the map is empty.

.peekValue(key, opt_val)

Retrieves the value for a given key without updating the entry to be the most recently used.

key {string}
The key to retrieve the value for.
opt_val {*=}
A default value that will be returned if the key is not found.
returns {*}
The retrieved value.

.pop()

Removes the last node from the list and returns its value.

returns {*}
The value of the popped node, or undefined if the map was empty.

.remove(key)

Removes a value from the LinkedMap based on its key.

key {string}
The key to remove.
returns {boolean}
True if the entry was removed, false if the key was not found.

.set(key, value)

Sets a value for a given key. If this is a caching LinkedMap, this entry will become the most recently used.

key {string}
The key to retrieve the value for.
value {*}
A default value that will be returned if the key is not found.

.setMaxCount(maxCount)

Sets the maximum number of entries allowed in this object, truncating any excess objects if necessary.

maxCount {number}
The new maximum number of entries to allow.

.shift()

Removes the first node from the list and returns its value.

returns {*}
The value of the popped node, or undefined if the map was empty.

.some(f, opt_obj)

Calls a function on each item in the LinkedMap and returns true if any of those function calls returns a true-like value.

f {Function}
The function to call for each item. The function takes three arguments: the value, the key, and the LinkedMap, and returns a boolean.
opt_obj {Object=}
The object context to use as "this" for the function.
returns {boolean}
Whether f evaluates to true for at least one item in the LinkedMap.
@see
goog.structs.some