StephenCleary / Comparers

The last comparison library you'll ever need!
MIT License
427 stars 33 forks source link

Collection comparer without order #35

Closed StephenCleary closed 3 years ago

StephenCleary commented 3 years ago

Currently EquateSequence requires elements to be in corresponding order. This issue is to add some kind of support for a collection equality comparer that ignores ordering of elements. This will require some kind of state, so this will be an expensive comparer to use.

The comparer will need to allow a custom element equality comparer, one that may have special null handling.

High-level algorithm:

Not sure how to handle GetHashCode, since there's no order defined for the items nor their equivalence classes. Any hash code combination would have to use a commutative combiner, which is not currently in the library.

StephenCleary commented 3 years ago

Alternative:

StephenCleary commented 3 years ago

Alternative:

StephenCleary commented 3 years ago

The commutative combiner could just add hash codes along with a constant value like 65536 to act as a quasi element counter.

StephenCleary commented 3 years ago

The equivalence-class-count approach is fastest (O(N)), but is complicated by the fact that nulls can be items in the same equivalence class as non-null items. Wondering if we can use Dictionary<Wrapper<T>> for some kind of struct Wrapper<T> type.