comparison - C# IComparable for X and Y co-ordinates -
suppose have list<point>
stores list of x , y points, want these ordered. suppose not know domain of x , y values in advance, i'll use long
large enough purposes.
at given moment, list can have large or small amount of points, , these points sparsely distributed, need able efficiently retrieve , insert particular points if exist in list.
in past, i've used icomparable<>
, list.binarysearch()
great success. unfortunately, need able insert , retrieve based on both x , y value.
if know domain of x , y in advance, , domain small enough, can add numbers 1 number raised next power of domain of other. ensures no collisions , can efficiently retrieve/insert values. example, if x , y both between 0 , 9, can compare on 10 * x + y
. because data types 64-bit integers, can't this.
without restricting domain, there other way can achieve functionality?
one method work compare on x.tostring("n19") + y.tostring("n19"), string comparison instead of numerical comparison , come huge performance penalty.
edit: need "n19" because otherwise (x = 1234, y = 5) resolve same (x = 1, y = 2345); , other collisions in between.
instead of combining multiple components of vector 1 scalar value comparison compare each component, e.g.
int compareto(point p) { if (this.x < p.x) { return -1; } else if (this.x > p.x) { return 1; } else { return this.y.compareto(p.y); } }
Comments
Post a Comment