Refactoring calculation caches
Suppose I have a class Group, like so:
class Group {
...
mutable std::map<Input, Output> complexCalculationCache;
public:
...
bool operator==(const Group& rhs); // excludes
complexCalculationCache in comparison since it's irrelevant
Output complexCalculation(const Input& input); // does the
complex calculation if it's not in the cache, uses the cache otherwise
};
What I would like to do is to separate out the
complexCalculationCache, since it's really only used in
Group::complexCalculation() and nowhere else, and is ultimately
hindering my efforts to make compositions involving Group (ie. keeping
G1's cache and G2's caches in sync if G1 == G2). Firstly, is this a
good idea? Secondly, if it is, then I'd probably need to have a
singleton cache manager of sorts (perhaps something backed by
std::unordered_map<Group, std::map<Input, Output>> with a hash
function for Group). Can someone point me to the best way to do this?
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]