treediff-rs icon indicating copy to clipboard operation
treediff-rs copied to clipboard

Allow hook for compare function + Ignore order

Open flip111 opened this issue 6 years ago • 2 comments

Would be nice if a comparison function could be passed. And also be able to ignore the order of things.

In my program i read in json ["hello", "world"] and ["world", "hello"] this will be picked up as two changes, but i don't really care for the order in the array. Would be nice to be able to set per array if it needs to compare indices (the default) or not.

Another thing is that instead of array of strings i will have an array of objects and one property of an object should be ignored during comparison. [{ignore_this:"timestamp", compare_this: "important data"}, { .. another object ..}]

I think to design this in the library will not be so easy because the passed in function probably needs knowledge of what it is looking at (some path from the root struct) + access to parents + access to children (and therefor implicit access to siblings).

I can not fix this by manipulating the data i have up front. For example sorting the data and removing that one property from an object. Consider this these two arrays (string example, not with object example): ["hello", "world"] and ["hello", "lol", "world"]. The diff is as follows:

Unchanged([Index(0)], String("hello"))
Modified([Index(1)], String("world"), String("lol"))
Added([Index(2)], String("world"))

So i would need index insensitive diff and it would change to:

Unchanged(String("hello"))
Added(String("lol"))
Unchanged(String("world"))

Maybe there are already facilities in the library that have some of the things i asked for. But i'm not sure what to look for.

flip111 avatar Oct 30 '18 18:10 flip111

Thanks for the detailed write-up, I believe this makes it easy for others to pick up if there is demand.

Indeed the algorithm itself makes certain assumptions and can't be affected by the passed in delegate.

The only way to fix this would be to have entirely different diff-functions, which I believe you could implement yourself based on your needs.

Byron avatar Dec 21 '18 10:12 Byron

Well i have the suspicion it's not so much the algorithm as it's the chosen data structure. I read here:

https://doc.rust-lang.org/std/collections/struct.BTreeSet.html

It is a logic error for an item to be modified in such a way that the item's ordering relative to any other item, as determined by the Ord trait, changes while it is in the set.

Maybe it's possible to change the diff function to make it generic over a type so people can choose a structure that cares about order or not. I don't know if there is a candidate data structure that doesn't care about order, but i could look or ask around.

flip111 avatar Dec 21 '18 23:12 flip111