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<K, V>(compareKeys: OrderedComparator<K, K>, cloneKey: CloneFunction<K> = shallowClone, cloneValue: CloneFunction<V> = shallowClone): Dictionary<K, V> Construct a new Dictionary<K, V>.  
[iterator](): Iterator<DictionaryEntry<K, V>, any, undefined> 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(): { keys: K[], values: V[] } Extracts the contents of this dictionary as a pair of { keys, values } arrays, and empties this dictionary.  
extractPairs(): { key: K, value: V }[] Extracts the contents of this dictionary as an array of { key, value } pairs, and empties this dictionary.  
findOrInsert(key: K, value: V): { inserted: boolean, value: V } Obtains the value associated with the specified key, or inserts it if the specified key does not yet exist.  
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): undefined | V 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.  
keys(): Iterable<K> Provides iteration over the keys in this Dictionary, in sorted order.  
lowerBound(key: K): { equal: boolean, index: number } 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.  
values(): Iterable<V> Provides iteration over the values in this Dictionary, in sorted order by the corresponding keys.  

Properties

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

Defined in

Last Updated: 12 June, 2024