I'm dealing with the fact that I need to init a HashSet with a set of elements but without any kind of comparation class.
After the init, any element added to the HashSet need to be passed with a comparator.
How can I accomplish it?
Now I have this:
HashSet<Keyword> set = new HashSet<Keyword>(new KeyWordComparer());
The problem is that the init takes to long and there's no necessity in applying the comparation.
KeywordComparer Class:
class KeyWordComparer : EqualityComparer<Keyword>
{
public override bool Equals(Keyword k1, Keyword k2)
{
int equals = 0;
int i = 0;
int j = 0;
// based on sorted ids
while (i < k1._lista_modelos.Count && j < k2._lista_modelos.Count)
{
if (k1._lista_modelos[i] < k2._lista_modelos[j])
{
i++;
}
else if (k1._lista_modelos[i] > k2._lista_modelos[j])
{
j++;
}
else
{
equals++;
i++;
j++;
}
}
return equals >= 8;
}
public override int GetHashCode(Keyword keyword)
{
return 0;//notice that using the same hash for all keywords gives you an O(n^2) time complexity though.
}
}
Note: This is a follow-up question to c# comparing list of IDs.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…