"OrderedHashTable" (Data Structure)
"OrderedHashTable"
represents a hash table where the keys and values are general expressions and the order in which keys are inserted is preserved.
Details
- An ordered hash table is useful for storing individual values that can be retrieved by use of a key where it is important to preserve the order of insertion:
-
CreateDataStructure["OrderedHashTable"] create a new empty "OrderedHashTable" CreateDataStructure["OrderedHashTable",assoc] create a new "OrderedHashTable" containing the rules from assoc Typed[x,"OrderedHashTable"] give x the type "OrderedHashTable" - For a data structure of type "OrderedHashTable", the following operations can be used:
-
ds["Copy"] return a copy of ds time: O(n) ds["Elements"] return a list of the elements of ds time: O(n) ds["EmptyQ"] True, if ds has no elements time: O(1) ds["Insert",keyvalue] add key to ds with the associated value and return True if the addition succeeded time: O(1) ds["KeyDrop",key] drop key and its value from ds time: O(1) ds["KeyDropAll"] drop all the keys and their values from ds time: O(n) ds["KeyExistsQ",key] True, if the key exists in ds time: O(1) ds["Keys"] return the keys in ds as a list time: O(n) ds["Length"] the number of key-value pairs stored in ds time: O(1) ds["Lookup",key] return the value stored with key in ds; if the key is not found, return a Missing object time: O(1) ds["Lookup",key,defFun] return the value stored with key in ds; if the key is not found, return defFun[key] time: O(1) ds["Values"] return the values in ds as a list time: O(n) ds["Visualization"] return a visualization of ds time: O(n) - The following functions are also supported:
-
dsi===dsj True, if dsi equals dsj FullForm[ds] full form of ds Information[ds] information about ds InputForm[ds] input form of ds Normal[ds] convert ds to a normal expression
Examples
open all close allBasic Examples (2)
A new "OrderedHashTable" can be created with CreateDataStructure:
ds = CreateDataStructure["OrderedHashTable"]ds["Insert", f[1] -> g[2]]ds["Length"]ds["KeyExistsQ", f[1]]ds["Lookup", f[1]]When a key is not found, a Missing object is returned:
ds["Lookup", f[2]]Return an expression version of ds:
Normal[ds]A visualization of the data structure can be generated:
ds = CreateDataStructure["OrderedHashTable"];
Do[ds["Insert", i -> OddQ[i]], {i, 30}]
ds["Visualization", MaxItems -> 9]The order of insertions is preserved:
ds["Keys"]Scope (1)
Information (1)
A new "OrderedHashTable" can be created with CreateDataStructure:
ds = CreateDataStructure["OrderedHashTable"]Information about the data structure ds:
Information[ds]See Also
Association Dispatch Hash DownValues CreateDataStructure DataStructure
Data Structures: HashTable HashSet OrderedHashTable LeastRecentlyUsedCache
Related Guides
History
Introduced in 2020 (12.1)