Thursday, March 12, 2020
Using TDictionary for Hash Tables in Delphi
Using TDictionary for Hash Tables in Delphi Introduced in Delphi 2009, the TDictionary class, defined in the Generics.Collections unit, represents a generic hash table type collection of key-value pairs. Generic types, also introduced in Delphi 2009, allow you to define classes that dont specifically define the type of data members. A dictionary is, in a way, similar to an array. In an array you work with a series (collection) of values indexed by an integer value, which can be any ordinal type value. This index has a lower and an upper bound. In a dictionary, you can store keys and values where either can be of any type. The TDictionary Constructor Hence the declaration of the TDictionary constructor: In Delphi, the TDictionary is defined as a hash table. Hash tables representà a collection of key-and-value pairs that are organized based on the hash code of the key. Hash tables are optimized for lookups (speed). When a key-value pair is added to a hash table, the hash of the key is computed and stored along with the added pair. The TKey and TValue, because theyre generics, can be of any type. For example, if the information you are to store in the dictionary is coming from some database, your Key can be a GUID (or some other value presenting the unique index) value while the Value can be an object mapped to a row of data in your database tables. Using TDictionary For the sake of simplicity, the example below uses integers for TKeys and chars for TValues.à First, we declare our dictionary by specifying what the types of the TKey and TValue will be: Then the dictionary is filled using the Add method. Because a dictionary cannot have two pairs with the same Key value, you can use the ContainsKey method to check if some key-valued pair is already inside the dictionary. To remove a pair from the dictionary, use the Remove method. This method will not cause problems if a pair with a specified key is not a part of the dictionary. To go through all the pairs by looping through keys you can do a for in loop. Use the TryGetValue method to check if some key-value pair is included in the dictionary. Sorting The Dictionary Because a dictionary is a hash table it does not store items in a defined sort order. To iterate through the keys that are sorted to meet your specific need, take advantage of the TList a generic collection type that supports sorting. The code above sorts keys ascending and descending and grabs values as if they were stored in the sorted order in the dictionary. The descending sorting of integer-type Key values uses TComparer and an anonymous method. When Keys and Values Are of TObject Type The example listed above is a simple one because both the key and the value are simple types. You can have complex dictionaries where both the key and the value are complex types like records or objects. Heres another example: Here a custom record is used for the Key and a custom object/class is used for the value. Note the usage of a specialized TObjectDictionary class here. TObjectDictionary can handle objects lifetime automatically. The Key value cannot be nil, while the Value value can. When a TObjectDictionary is instantiated, an Ownerships parameter specifies whether the dictionary owns the keys, values or both and therefore helps you not have memory leaks.
Subscribe to:
Posts (Atom)