I have 2 dictionnary with similaire struture
Dictionary<string, List<int>> Origins
and
Dictionary<string, List<int>> Changes
I have create Origins in the beginning. It is a copy of the begining state.
Example :
Origins["toto"] = new List<int>(){1,2,3};
Origins["tata"] = new List<int>();
Origins["titi"] = new List<int>(){1,2};
After an user action, I keep the changes in the Changes dictionnary.
Basicly the user can add or remove some number linked to the string. So i keep all the trace of any change like this:
example: if the user add a 1 in “tata”
in the dictionary Changes a have
“tata” have 1
if the user add a 4 in “toto”
in the dictionary Changes a have
“toto” have 1,2,3,4
if the user remove 1 in “titi”
in the dictionary Changes a have
“titi” have 2
I need the Changes dictionnary for know when the user return to original state, with a simple comparaison.
If no change is done for a string, the Changes dictionnary do not have any entry for this string.
After many changes, the user can Save the changes.
So now i need to find all the add and remove operations.
My first idea is to compare the two dictionnary , to see the operation add and the operation remove. But how ?
If I compare the lists for the same string, i may know the difference,but i’m stuck here.
And maybe there are a better way to do this ? Any suggestion ?
You have the following cases:
Changes, along with some associated values.In case (1) you’ll have an entry in
Changesthat does not exist inOrigins. In case (2) you’ll have an entry in both but with a different list of values.(The values I’m going to assume are a mathematical set, i.e. that a particular value can appear in there only once and that the ordering insignificant. If that’s not the case then you will have to modify the approach somewhat.)
To detect case (1), you can find the keys that are unique:
Obviously every value in
Changesfor a new key will need to be ‘added’. You can simply iterate thenewKeysenumerable and retrieve the values fromChanges:To detect case (2), you will need to iterate the dictionary and compare the set of values in Changes with Origins. To do that we’ll iterate
Originsto get the key and original values and then retrieve the item using the key fromChanges. (We will do it this way around as, if we iteratedChangesinstead we would potentially end up with new, added keys that do not exist inOriginsand that is another case we would have to deal with.)