Presentation is loading. Please wait.

Presentation is loading. Please wait.

Efficient data structures in Delphi – reimplementing a dictionary

Similar presentations


Presentation on theme: "Efficient data structures in Delphi – reimplementing a dictionary"— Presentation transcript:

1 Efficient data structures in Delphi – reimplementing a dictionary
Stefan Glienke

2 About me Experience in Turbo Pascal and Delphi since 1997
Education as software developer Participation in several open source projects Embarcadero MVP since 2014 and „MVP of the year“ 2015 Specialized in following areas Development of logic and data layers Software design and architecture „Clean code“ Lead developer of Spring4D

3 What is a hash table? Key and Value mapping Values go into an array
Hash function to get an array index out of the Key – used to do insertions and lookup Dealing with hash collisions – when more than one item would go into the same index Creating array or linked list at that index to store more than one item Probing – looking for the next free slot Order of items is based on the hash function – which is not easily predictable 1 2 3 4 Marco Stefan Paolo 1 2 3 4 Marco Paolo 1 2 3 4 1 2 3 4 Paolo

4 TDictionary<TKey,TValue> from RTL
Storing an array of Key/Value pairs plus the HashCode Doing linear probing if slot is occupied Removal is a bit tricky as gaps need to be filled in order to not break probing (see comment in DoRemove)

5 Structure of the RTL Hash table
Index Key Value '1' 1 <empty> 2 3 '3' 4 '2' 5 6 7 '0'

6 Making the dictionary ordered
Keep items in insertion order (like in a list where you add/append to the end) Possibilities: Maintain ordered key List (this was done in Spring4D 1.2 in the TOrderedDictionary) Add previous and next pointers to the buckets to maintain order (LinkedHashMap<K,V> from Java) Split content into two arrays (dict in Python 3.6) item array holding the key/value pairs bucket array containing the indexes of entries in the item array

7 Structure of the new dictionary
Bucket Index Item Index 1 <empty> 2 3 4 5 6 7 Item Index Key Value '0' 1 '1' 2 '2' 3 '3'

8 Performance considerations
Store the hash in the items array If hash values differ items cannot be equal (comparing hashes it faster than item comparison) Indirection is affecting performance Solution: using some bytes from the ItemIndex for the HashCode

9 Thank you very much for your attention!
Questions?


Download ppt "Efficient data structures in Delphi – reimplementing a dictionary"

Similar presentations


Ads by Google