c# - IEqualityComparer That Will Find The Next Closest Element -
is possible write iequalitycomparer
sortedlist <double, gameobject>
return 'next-closest' double
?
for example;
sortedlist <double, gameobject> list = new sortedlist <double, gameobject>(new mycomparer()); list[0.00] = go1; list[1.00] = go2; list[0.55]; // should return go2. ie, find next-closest key, value pair // , return
is possible this? use iequalitycomparer
achieve this?
#region comparator public class mycomparer : iequalitycomparer<double> // should pair<double, gameobject> instead? { public bool equals(double a, double b) { return (math.abs(a-b) <= 0.01); } } #endregion
ps: if add own custom comparer (iequalitycomparer
) - sorting , searching algorithm of sortedlist
still remain binary search? changing comparer have made sortedlist
less efficient? have made lookup
, insertion
less efficient?
please use following program fix. , yes there little overhead due rounding. if retrieve 0.45 return , 1.55 return c.
class sortedlisttest { public static void test() { var list = new sortedlist<double, string>(new mycomparer()); list[0.00] = "a"; list[1.00] = "b"; list[2.00] = "c"; console.writeline(list[0.55]); } private static void main() { sortedlisttest.test(); } } internal class mycomparer : icomparer<double> { public int compare(double x, double y) { return (int) (math.round(x) - math.round(y)); } }
Comments
Post a Comment