In a program I need to evaluate lots of objects. The result of evaluation is a double.
for example
Object myObject = new Object(x,y,z);
double a = eval(myObject);
after this lots of other objects should be evaluated.
I want to avoid reevaluating same objects. So I need to add evaluated objects and the evaluation result to a hash structure.
for example something like this after first evaluation: ——-> this is a pseudo code
myHash.add(myObject, a);
Object anotherObject = new Object(x,y,z);
if (myHash.find(anotherObject))
double evaluationForAnotherObject = myHash.get(anotherObject);
any help would be highly welcomed
A
Dictionary<TKey,TValue>can be used for such lookups:It’s important to use the correct equality comparer. For example on objects it uses referential equality by default. If your
TKeytype doesn’t use the desired equality comparison you can supply anIEqualityComparer<TKey>to the constructor of the dictionary.As an alternative you can pass your function into a memoizer. It returns a new function which caches the result of earlier computations. AFAIK the
MiscUtillibrary contains one.and then use
memoizingEval(obj)