Dictionary<K, V> Class

Maintains a mapping of keys to values. Unlike the standard Map<K, V>, a Dictionary<K, V> supports custom comparison logic for keys of object type (and for any other type). The user supplies a key comparison function to the constructor, that must meet the following criteria given 'lhs' and 'rhs' of type K:

  • If lhs is equal to rhs, returns 0
  • If lhs is less than rhs, returns a negative value
  • If lhs is greater than rhs, returns a positive value
  • If compare(lhs, rhs) returns 0, then compare(rhs, lhs) must also return 0
  • If compare(lhs, rhs) returns a negative value, then compare(rhs, lhs) must return a positive value, and vice versa.

Modifying a key in a way that affects the comparison function will produce unpredictable results, the most likely of which is that keys will cease to map to the values with which they were initially inserted.

Implements

Methods

Name Description
constructor(compareKeys: OrderedComparator<K>, cloneKey: CloneFunction<K> = shallowClone, cloneValue: CloneFunction<V> = shallowClone): Dictionary Construct a new Dictionary<K, V>.  
__@iterator(): Iterator<DictionaryEntry<K, V>> Returns an iterator over the key-value pairs in the Dictionary suitable for use in for-of loops.  
clear(): void Removes all entries from this dictionary  
delete(key: K): boolean Deletes a value using its key.  
extractArrays(): object Extracts the contents of this dictionary as a pair of { keys, values } arrays, and empties this dictionary.  
extractPairs(): Array<object> Extracts the contents of this dictionary as an array of { key, value } pairs, and empties this dictionary.  
forEach(func: (key: K, value: V) => void): void Apply a function to each (key, value) pair in the dictionary, in sorted order.  
get(key: K): V | undefined Looks up a value by its key.  
has(key: K): boolean Determines if an entry exists for the specified key  
insert(key: K, value: V): boolean Attempts to insert a new entry into the dictionary.  
lowerBound(key: K): object Protected Computes the position at which the specified key should be inserted to maintain sorted order.  
set(key: K, value: V): void Sets the value associated with the specified key in the dictionary.  

Properties

Name Type Description
_cloneKey Protected CloneFunction<K>    
_cloneValue Protected CloneFunction<V>    
_compareKeys Protected OrderedComparator<K>    
_keys Protected K[]    
_values Protected V[]    
size Accessor ReadOnly number The number of entries in the dictionary.  

Defined in

Last Updated: 10 September, 2019