c# - SortedSet.Contains gives error "at least one object must implement ICombarable" -
i have 2 sortedsets:
sortedset<sortedset<int>> sset1 = new sortedset<sortedset<int>>(); sortedset<sortedset<int>> sset2 = new sortedset<sortedset<int>>();
later on check make new sorted set:
sortedset<int> newsset = methodthatreturnssortedset();
now want check if sset1 , sset2 contain newsset:
if (!sset1.contains(newsset) && !sset2.contains(newsset)) <--error on line { sset1.add(next); //some more code }
so error argument exception, "at least 1 of these objects must implement icomparable.
i have looked @ other questions same problem, in case want compare own classes. checking if item in set. yea..i have no idea how solve this, pointers?
you can't have sortedset
of sortedset
s unless specify custom comparer, because sortedset
not implement icomparable
in itself.
whenever use type sortedset<x>
, set organized in increasing order based on x
, x
must icomparable<x>
or icomparable
, or else sortedset<x>
must created constructor overload allows give custom object of type icomparer<x>
.
which of these 2 sortedset<int>
comes first:
{ 3, 8, 25, }
or:
{ 3, 7, 9, 58, 12345678, }
addition: no answer above, assumed wanted lexicographic comparison, seems natural. wrote class:
class lexicographiccomparer : comparer<sortedset<int>> { public override int compare(sortedset<int> x, sortedset<int> y) { if (x == null || y == null) return default.compare(x, y); int firstdifference = x.zip(y, comparer<int>.default.compare) .where(n => n != 0).firstordefault(); if (firstdifference != 0) return firstdifference; return comparer<int>.default.compare(x.count, y.count); } }
that class inherits comparer<>
class , implements icomparer<>
interface because of that. use when construct "nested" sortedset
, example:
lexicographiccomparer lc = new lexicographiccomparer(); sortedset<sortedset<int>> sset1 = new sortedset<sortedset<int>>(lc); sortedset<sortedset<int>> sset2 = new sortedset<sortedset<int>>(lc);
Comments
Post a Comment