I have a requirement for a data structure which satisfies the following use cases:
- stores items by key
- functionality to retrieve the exact value by key
- functionality to retrieve the closest prior-value by key
- functionality to retrieve the closest subsequent-value by key
1 and 2 are obvious. For 3 (and 4), assuming the key is a custom Date object, the closest prior-value, on say a search for December 1, 2014, would return the value with the date closest to December 1, 2014. So if the prior values had keys of November 10, 11, 14, 15, and 29, then it would return November 29. For 4, the functionality is the same, however the closest date would be after December 1.
Currently I am using a std::map<Date, T>
, but the requirements for 3 and 4 are new. Things I have considered:
- keep using a
std::map<Date, T>
(log-n store and exact retrieve. closest match would be brute-force, so approximately n in terms of speed) - use a
std::vector<T>
, with my own sort routine (constant store, log-n retrieve for exact and closest; I can roll my own fetch routine and access members of thestd::vector<T>
by index for fast binary search)
I am leaning towards the std::vector<T>
, since my T object has the Date built in (for the purposes of a std::map<Date, T>
the Date is just a copy of the T.date()
property).
The behaviour of these datastores is a mass-load up front, and occasional throughout the life of the object, but constant fetches on contents. So any data structure which is heavy on the front-end, but light on lookups, would work.
Thoughts?