Direct Question: With three (or more) nearly identical copies of a class object, how can I best (or most efficiently) store the differences between them?
Background: I have an algorithm which requires a set of parameters:
struct params
{
std::string A;
std::string B;
double C;
double D;
userDefinedTypeDef S;
};
And I want to call my algorithm three times. The first time, C = 3 & S = ‘foo’. The second time, C = 4 & S = ‘foo’. And the third time, C = 4 & S = ‘bar’.
Now the example given is merely for illustration, the true object is a class, not a structure, and has a few hundred members. I also want to call the algorithm many more than three times, so I don’t want to have extra copies of this object running around. So what is the best pattern / implementation / method for storing only the difference between two class objects?
In other words, how can I store “Object 2 is the same as &(Object 1) except that C = 4”??
Edit: As noted in the comments below, I’d really like to avoid altering the function call as well as arrays of either the parameter class object or the member objects. My purpose in doing so is to keep one copy, ala “currentParam” and then keep a list of the differences between successive calls. That way, I can simply update the values which changed, leaving all else fixed, and use the function given to me.
That said, what’s the easiest way to do this? Create a class with boolean flags ‘member1_changed’? And then use a union? I’m fairly stuck — been puzzling a bit about sparse matrix storage and how that might be related — hence why I asked the question.
You might find the Flyweight design pattern to be of interest. It is designed specifically to solve your problem.
Here’s one way to store the differences:
Step 1: Replace all your members with pointers to the members. Yes, this adds a little memory overhead.
Step 2: Have classes store shallow copies of shared members instead of deep copies.
If your classes are not immutable, this technique becomes more complicated, though the basic idea is still doable.
Note that this does reduce memory footprint, but will also reduce spatial locality of memory references. This may slow down your program significantly. It also adds an extra layer of indirection.