0

I have my class

MyClass<MyTriple<FirstG, SecondG, ThirdG>> : ICollection<MyTriple<FirstG, SecondG, ThirdG>>

I have data stored in:

Dictionary<FirstG, Dictionary<SecondG, ThirdG>> Data

and I want to implement IEqualityComparer for my Data. Constructor of MyClass has to take as argument comparer of MyTriple:

public MyClass(IEqualityComparer<MyTriple<FirstG, SecondG, ThirdG>> comparer) {...}

and I want (somehow) pass this comparer to Data and create it in constructor like:

Data = new Dictionary<FirstG, new Dictionary<SecondG, ThirdG>(SecondAndThirdGComparer)>(FirstGComparer);

I am really hopeless, I tried creating my comparer that implements IEqualityComparer, but I can't figure out, how to get FirstGComparer<FirstG> comparer. Thanks for any advices.

Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
Miloš Lukačka
  • 842
  • 1
  • 11
  • 25
  • Note that .NET 4 includes the [Tuple Class](http://msdn.microsoft.com/en-us/library/system.tuple.aspx), which might make some of this easier (or at least more standard). – Robert Harvey Apr 09 '13 at 17:40
  • how would it help? instead of MyTriple I would have Touple class, problem with comparator remains – Miloš Lukačka Apr 09 '13 at 17:50
  • 2
    I see that you're hyper-focused on your specific problem, but since the Framework already contains something you're trying to build yourself, might it be possible that the Framework Tuple is friendlier about comparing things than yours? It might already have a comparator built-in; in fact, [I know it does](http://msdn.microsoft.com/en-us/library/dd990083.aspx). – Robert Harvey Apr 09 '13 at 17:54
  • ...What is the functionality you are looking to get out of Data? Because you absolutely cannot have a constructed object as a generic argument. – IdeaHat Apr 09 '13 at 17:57
  • I'm trying here to make Dictionary, that has primary key, secondary key and a value. Making my storage dictionary of dictionaries works perfecly, except this one case, when user can define his own comparer – Miloš Lukačka Apr 09 '13 at 18:00

1 Answers1

3

OK, so the user comes in with a comparer parameter (to the constructor) of type:

IEqualityComparer<MyTriple<FirstG, SecondG, ThirdG>>

Now, that could be any crazy comparer in "3D". No-one can guarantte that this comparer works in a coordinate-wise or lexicographic way. Therefore there's absolutely no way you can "factor" out a comparer of only FirstG, for example.

Suppose you have two instances firstG_X and firstG_Y, say. Then from comparer you cannot tell whether these two are equal. You can ask comparer to compare two triples only. If you "extend" firstG_X to some triple, and firstG_Y to some other triple, you have to make an arbitrary choice, and the answer from the oraculous comparer might very well depend on that choice.

Hoping what I'm saying makes sense.

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
  • yes, this is what I needed, so now I know, I have to get it done differently – Miloš Lukačka Apr 09 '13 at 22:08
  • It may be helpful to offer a particular practical form of comparer which would have problems. A common one is a comparator which takes a one data type and a function that converts to another, and maps things as equal if they convert to the same thing. For example, one may have a function that converts a 2d point to the color that appears there, and a comparator that regards points as being equal if they're same color. If x1!=x2 and y1!=y2, then whether (x1,y1) matches (x2,y2) is independent of whether there exists any q such that (x1,q) matches (x2,q) or (q,y1) matches (q,y2). – supercat Apr 11 '13 at 15:44
  • @supercat Yeah, any partition of the `xy` plane into equivalence classes corresponds to an `IEquialityComparer` in 2D. Another example if `x` is the name of a person and `y` is the year of birth of that person, and the user gave a comparer that considers two persons to be "equal" if they have the same name, disregarding birth year. Then from two birth years (`y1` and `y2` known), it's impossible to tell if the two persons `(?, y1)` and `(?, y2)` are equal (we can't guess their names from the years, to check if the names match). – Jeppe Stig Nielsen Apr 11 '13 at 15:53
  • @JeppeStigNielsen: In the latter situation, if one had something that was known to be a (name,date) comparer that simply examined names, it might be possible to factor it to a name comparator, though not a date comparer. The plane-equivalence example can't be factored either way. – supercat Apr 11 '13 at 16:17