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;
}
}

You need to be a bit more precise in framing the problem...
and you do not cater for (say) [1..4) meaning 1 <= x < 4
Going with these as assumptions and a bit of a kludge that requires you to search for
Tuple<a,a>for the valuea(see exception inComp()method), here's an example of how to achieve what you're after...Giving result...