views:

210

answers:

2

As a follow up to the method that collapses overlapping ranges I thought I would try to create a method that combines adjacent ranges.

Basically, after running the Collapse method you may end up with for example 1 to 5 and 6 to 10. I would like to combine those into one range, 1 to 10.

This is what I have come up with so far, but it doesn't really work very well. Does anyone spot my problem or have good alternative solution?

    public static IEnumerable<Range<T>> MergeAdjacent<T>(this IEnumerable<Range<T>> source, Func<T, T, bool> isAdjacent)
    {
        using (var sourceIterator = source.GetEnumerator())
        {
            if (!sourceIterator.MoveNext())
                yield break;

            var first = sourceIterator.Current;

            while (sourceIterator.MoveNext())
            {
                var second = sourceIterator.Current;

                if (isAdjacent(first.End, second.Start))
                {
                    yield return Range.Create(first.Start, second.End);
                }
                else
                    yield return first;

                first = second;
            }

            yield return first;
        }
    }
+1  A: 

It will only merge two adjacent ranges, not three or more. Keep the last one until you find a gap or the end of the list.

public static IEnumerable<Range<T>> MergeAdjacent<T>(this IEnumerable<Range<T>> source, Func<T, T, bool> isAdjacent)
{
    Range<T> current = null;

    foreach (Range<T> item in source)
    {
        if (current == null)
        {
            current = item;
        }
        else
        {
            if (isAdjacent(current.End, item.Start))
            {
                current = Range.Create(current.Start, item.End);
            }
            else 
            {
                yield return current;
                current = item;
            }
        }
    }

    if (current != null) 
        yield return current;
}
Guffa
A: 

I've reached this solution. One prerequisite is that ranges are ordered ascending/descending depending on the Func. It will merge adjacent ranges and it's still deferred execution. I didn't perform a lot of tests so there might be edge cases that break this. Be gentile :-)

Edit: Shortened down the code a bit. As far as I can see it works. Left out null checks though.

    public static IEnumerable<Range<T>> MergeAdjacent<T>(this IEnumerable<Range<T>> source, Func<T, T, bool> isAdjacent)
    {
        using (var it = source.GetEnumerator())
        {
            if (!it.MoveNext())
                yield break;

            var item = it.Current;

            while (it.MoveNext())
                if (isAdjacent(item.End, it.Current.Start))
                {
                    item = Range.Create(item.Start, it.Current.End);
                }
                else
                {
                    yield return item;
                    item = it.Current;
                }

            yield return item;
        }
    }


    static void Main(string[] args)
    {
        var ranges = new List<Range<int>>
        {
            Range.Create(1,3), Range.Create(4,5), Range.Create(7,10), 
            Range.Create(11,17), Range.Create(20,32), Range.Create(33,80), 
            Range.Create(90,100), 
        };

        foreach (var range in ranges.MergeAdjacent((r1, r2) => r1 + 1 == r2))
            Console.WriteLine(range);
    }

    // Result: 1-5, 7-20, 25-80, 90-100
bruno conde
is that last `else yield break;` really necessary?
Svish
ended up using this one, but shortened it down slightly.
Svish