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;
}
Range
type, and following .NET naming conventions. – Jon Skeet Aug 22 '12 at 20:58