Alright as a preface I have a need to cache a relatively small subset of rarely modified data to avoid querying the database as frequently for performance reasons. This data
Not an answer per se, but be sure to use a typedef to refer to the container type you do use, something like typedef std::vector< itemtype > data_table_cache;
Then use your typedef type everywhere.
A sorted vector using std::lower_bound
can be just as fast as std::set
if you're not updating very often; they're both O(log n). It's worth trying both to see which is better for your own situation.
If you only want to search for distinct values, one specific column in the table, then std::hash
is fastest.
If you want to be able to search using several different predicates, you will need some kind of index structure. It can be implemented by extending your current vector based approach with several hash tables or maps, one for each field to search for, where the value is either an index into the vector, or a direct pointer to the element in the vector.
Going further, if you want to be able to search for ranges, such as all occasions having a date in July you need an ordered data structure, where you can extract a range.
Since from your (extended) requirements you need to search on multiple fields, I would point you to Boost.MultiIndex.
This Boost library lets you build one container (with only one exemplary of each element it contains) and index it over an arbitrary number of indices. It also lets you precise which indices to use.
To determine the kind of index to use, you'll need extensive benchmarks. 500
is a relatively low number of entries, so constant factors won't play nicely. Furthermore, there can be a noticeable difference between single-thread and multi-thread usage (most hash-table implementations can collapse on MT usage because they do not use linear-rehashing, and thus a single thread ends up rehashing the table, blocking all others).
I would recommend a sorted index (skip-list like, if possible) to accomodate range requests (all names beginning by Abc
?) if the performance difference is either unnoticeable or simply does not matter.
For searching a particular value, with std::set
and std::map
it takes O(log N) time, while with the other two it takes O(N) time; So, std::set
or std::map
are probably better. Since you have access to C++0x, you could also use std::unordered_set
or std::unordered_map
which take constant time on average.
For find_if
, there's little difference between them, because it takes an arbitrary predicate and containers cannot optimize arbitrarily, of course.
However if you will be calling find_if
frequently with a certain predicate, you can optimize yourself: use a std::map
or std::set
with a custom comparator or special keys and use find
instead.