Can anyone help me ?
for C#
I need to implement a routine that tests if a point is within a range ( array ), C#
in the figure below the ranges are [1-3],[6-9], [11-15]
i would like to use something like binarysearch, i used it but there is a flaw, because it can only test 2 ranges in this case ... if i have 2 ranges, the binarysearch works well because gives the 2 events in the IComparer for the 2 ranges. ([1-3],[6-9])
after i add 3 ranges the binarysearch only give me 2 ranges, [6-9], [11-15]
I'm using List<Tuple<int, int>> range, and IComparer<Tuple<int, int>>
something like that ---
class RangeComparerPoint : IComparer<Tuple<int, int>>
{
public int Compare(Tuple<int, int> f1, Tuple<int, int> f2)
{
//for the sake of clarity
int boundary_1 = f1.Item1;
int boundary_2 = f1.Item2;
int pos = f2.Item1;
int currPos = f2.Item2;
//EndSection
if (pos > currPos)
{
if (pos >= boundary_1 && currPos < boundary_1)
{
//in the range
return 0;
}
}
else
{
if ( boundary_1 > currPos )
{
return -1;
}
if (pos <= boundary_1)
{
//in the range
return 0;
}
}
return -1;
}
}