Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have a List of a custom structure (myStruct) which has two ranges stored in it. I'm trying to write a function where I can figure out if a range is completely enclosed in any of the ranges. Is this optimal and if not how can I improve it in terms of speed and memory efficiency?

class myStruct {
    private int start_one;
    private int stop_one;
    private int start_two;
    private int stop_two;

    public myStruct(int input_start_one, int input_stop_one, int input_start_two, int input_stop_two)
    {
        start_one = input_start_one;
        stop_one = input_stop_one;
        start_two = input_start_two;
        stop_two = input_stop_two;      
    }
}

public void run() 
{
    List<Struct> myStructs = new List<Struct>();
    myStructs.Add(new myStruct(86,724,725,1364));
    myStructs.Add(new myStruct(86,724,2068,2707));
    myStructs.Add(new myStruct(725,1364,2068,2707));

    if(is_in_range(88,100))
    {
        // in range
    }
}

public bool is_in_range(int in_start, int in_stop, BindingList<myStruct> input_list)
{
    myStruct current_item;
    for (int i = 0; i < input_list.Count; i++)
    {
        current_item = input_list[i];

        if (   (in_start >= current_item.start_one && in_stop <= current_item.stop_one)
            || (in_start >= current_item.start_two && in_stop <= current_item.stop_two))
                return true;
    }

    return false;
}
share|improve this question

migrated from stackoverflow.com Aug 23 '12 at 13:07

This question came from our site for professional and enthusiast programmers.

1  
You could improve the design by introducing a Range type, and following .NET naming conventions. –  Jon Skeet Aug 22 '12 at 20:58

2 Answers 2

Just an idea

public struct Range
{
    public readonly int From;
    public readonly int To;

    public Range(int from, int to)
    {
        From = from;
        To = to;
    }

    public bool Contains(Range other)
    {
        return Contains(other.From, other.To);
    }

    public bool Contains(int from, int to)
    {
        return From <= from && To >= to;
    }
}


public class MyStruct
{
    public readonly Range Range1;
    public readonly Range Range2;

    public MyStruct(Range range1, Range range2)
    {
        Range1 = range1;
        Range2 = range2;
    }

    public MyStruct(int from1, int to1, int from2, int to2)
    {
        Range1 = new Range(from1, to1);
        Range2 = new Range(from2, to2);
    }

    public bool Contains(Range r)
    {
        return Range1.Contains(r) || Range2.Contains(r);
    }

    public bool Contains(int from, int to)
    {
        return Range1.Contains(from, to) || Range2.Contains(from, to);
    }
}

-

List<MyStruct> myStructs = new List<MyStruct>()
    {
        new MyStruct(86, 724, 725, 1364),
        new MyStruct(86, 724, 2068, 2707),
        new MyStruct(725, 1364, 2068, 2707)
    };

bool b = myStructs.Any(r => r.Contains(88, 100));
share|improve this answer

Apart from some typos (f.e. List<Struct> instead of List<myStruct>), the conditions in is_in_range are wrong. You want to return false if any of the items are not in the range and not true.

But instead of this method you can also use Enumerable.All:

if (myStructs.All(s => 88 >= s.start_one &&  88 >= s.start_two 
                   && 100 <= s.stop_one  && 100 <= s.stop_two))
{
    // all in range
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.