Ordered Maps and Insertion-Ordered Maps
An ordered map can support queries by key order. A dictionary preserving insertion order can replay the history of insertions.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Ask for the next larger key
Insert 20, 5, and 10. The smallest key larger than 7 is 10. An ordinary insertion-order traversal yields 20 first. Dictionary insertion order explains why that traversal cannot serve as a sorted index directly.
Choose required operations first
If a workload needs fast ranges and repeated nearest-key lookups, use an appropriate ordered structure. For exact-key lookups and source order, a hash-based dictionary may suffice. See balanced search trees for the former operation family.[1]
Choose an order that supports the intended query
Insert keys 30, 10 and 20 in that order. An insertion-ordered dictionary iterates 30, 10, 20. An ordered tree map can iterate 10, 20, 30 by key. If you need the first key at least fifteen and the following entries up to twenty-five, tree order gives a natural starting point. See sorted range queries.
Insertion order: 30, 10, 20
Key order: 10, 20, 30
Keys in [15,25): 20
Equal contents can still expose different iteration orders
Consider cost under updates and repeated queries
Sorting dictionary keys for one report may be entirely adequate. Frequent sorted-range operations can justify maintaining order through every insertion and deletion. Test values out of insertion order, replacement of an existing key, and range queries that select nothing.