views:

216

answers:

1

Ok, I have these intersection methods to work with ranges, and they work well as long as the range endpoints are not null:

public static bool Intersects<T>(this Range<T> first, Range<T> second, IComparer<T> comparer)
{
    return comparer.Compare(first.Start, second.End) <= 0 
        && comparer.Compare(first.End, second.Start) >= 0;
}

public static Range<T> GetIntersectionWith<T>(this Range<T> first, Range<T> second,
    IComparer<T> comparer)
{
    // Return null, if any range is null or if they don't  intersect at all
    if (first == null || second == null || !Intersects(first, second, comparer))
        return null;

    var start = comparer.Compare(first.Start, second.Start) < 0 
                    ? second.Start 
                    : first.Start;
    var end = comparer.Compare(first.End, second.End) > 0 
                    ? second.End 
                    : first.End;

    return Range.Create(start, end);
}

My problem now is that I would like them to support exactly that, null endpoints. A null endpoint would mean that the range goes to infinity in that direction. Two tests that I would like to pass, that doesn't, are for example these:

[Test]
public void Intersects_Intersecting_OneEndsWithNull()
{
    var a = Range.Create("a", "k");
    var b = Range.Create("c", null);

    Assert.That(a.Intersects(b), Is.True);
    Assert.That(b.Intersects(a), Is.True);
}

[Test]
public void GetIntersectionWith_Intersecting_OneStartingAndOneEndingWithNull()
{
    var a = Range.Create(null, "k");
    var b = Range.Create("f", null);
    var expected = Range.Create("f", "k");

    Assert.That(a.GetIntersectionWith(b), Is.EqualTo(expected));
    Assert.That(b.GetIntersectionWith(a), Is.EqualTo(expected));
}

The reason it doesn't work right away is that null is considered less than everything. But here null have to be sometimes considered to be greater than everything.

Any idea how this may be solved in a good way?

I'm thinking I will either have to check for null first and do something special or to make some sort of IComparer<T> wrapper... but I am not able to figure out which or how they would have to work. Have to remember that it could be given any kind of comparer too, so technically the ranges could be in the opposite direction, as long as the given comparer takes that into account of course (In the real code I throw an exception if the start comes after the end according to the given comparer). Anyways, I'm a bit lost here :P

+1  A: 

I think you need to take into account the nulls in the comparison.

Would this not help?

public static bool Intersects<T>(this Range<T> first, Range<T> second, IComparer<T> comparer)
        {    
            return (ReferenceEquals(first.Start, null) || ReferenceEquals(second.End, null) || comparer.Compare(first.Start, second.End) <= 0)
                && (ReferenceEquals(first.End, null) || ReferenceEquals(second.Start, null) || comparer.Compare(first.End, second.Start) >= 0);
        }

OK, for the second part. set start and end to null, and only set to start/end values if both is not null.

public static Range<T> GetIntersectionWith<T>(this Range<T> first, Range<T> second, IComparer<T> comparer)
    {
        // Return null, if any range is null or if they don't  intersect at all
        if (first == null || second == null || !Intersects(first, second, comparer))
            return null;

        T start;
        if (ReferenceEquals(first.Start, null))
            start = second.Start;
        else if (ReferenceEquals(second.Start, null))
            start = first.Start;
        else
            start = comparer.Compare(first.Start, second.Start) < 0 
                        ? second.Start 
                        : first.Start;        
        T end;
        if (ReferenceEquals(first.End, null))
            end = second.End;
        else if (ReferenceEquals(second.End, null))
            end = first.End;
        else
            end = comparer.Compare(first.End, second.End) > 0 
                        ? second.End 
                        : first.End;

        return Range.Create(start, end);
    }
astander
For the Intersects method that seems to do it, yes! But how would you use that in the GetIntersectionWith method?
Svish
Check edit answer
astander
Hm, I would have to set them to `default(T)` though, since `T` can be a value type. Would it still work, or do you think that could be a problem?
Svish
(Also, you can't use var when setting to null I think :)
Svish
Hm, this won't work... cause it is finding the intersection. Which means that if one is null and not the other, the intersection will start at the one that is not null (unless I am mistaken). But in this code it will start on null unless both does not start on null?
Svish
I saw that and fixed the issue, please check again.
astander
Nice! Seems to work. Will do more testing, but yeah. Seems like that did it actually :D Will edit your code to make it generic-acceptable.
Svish
Glad i could help X-)
astander