I just can't resist joining the play on this one :)
using System.Collections.Generic;
namespace TestVNCRegions
{
public class VNCRegion
{
public string VNCRegionName;
int LoVelocity;
int HiVelocity;
int LoNote;
int HiNote;
int LoChannel;
int HiChannel;
public static List<VNCRegion> VNCRegions = new List<VNCRegion>();
public static VNCRegion NewVNCRegion;
public static void AddVNCRegion(string vncRegionName, int lVel, int hVel, int lNte, int hNte, int lChn, int hChn)
{
NewVNCRegion = new VNCRegion
{
VNCRegionName = vncRegionName,
LoVelocity = lVel + 1,
HiVelocity = hVel - 1,
LoNote = lNte + 1,
HiNote = hNte - 1,
LoChannel = lChn + 1,
HiChannel = hChn - 1
};
VNCRegions.Add(NewVNCRegion);
}
public static VNCRegion FindRegion(int velocity, int note, int channel)
{
foreach (VNCRegion theRegion in VNCRegions)
{
if
(
velocity > theRegion.LoVelocity
&& velocity < theRegion.HiVelocity
&& note > theRegion.LoNote
&& note < theRegion.HiNote
&& channel > theRegion.LoChannel
&& channel < theRegion.HiChannel
)
return theRegion;
}
return null;
}
}
}
And a test might look like this:
VNCRegion.AddVNCRegion("region1", 1, 20, 4, 43, 2, 99);
VNCRegion.AddVNCRegion("region2", 21, 32, 45, 66, 100, 145);
VNCRegion.AddVNCRegion("region3", 33, 55, 67, 89, 146, 200);
VNCRegion foundRegion1 = VNCRegion.FindRegion(18, 12, 34);
if (foundRegion1 != null) Console.WriteLine("test 1 returns: " + foundRegion1.VNCRegionName);
VNCRegion foundRegion2 = VNCRegion.FindRegion(24, 50, 102);
if (foundRegion2 != null) Console.WriteLine("test 2 returns: " + foundRegion2.VNCRegionName);
VNCRegion foundRegion3 = VNCRegion.FindRegion(44, 72, 198);
if (foundRegion3 != null) Console.WriteLine("test 3 returns: " + foundRegion3.VNCRegionName);
Discussion: I find this a very interesting question because I can 'envision' a wide gamut of problems in which similar discrimination of items which must match multiple range criteria is required.
While this may be simply 'fantasy,' I wonder if there might be a way to calculate a unique hash ... based on range values, for some sub-set of this type of problem ... that might result in a highly optimized search. Trying to conceptualize higher-order solutions to this type of problem, alas, I encounter my lack of formal computer science education. But, that limit just makes me more insatiably hungry for knowledge :)
And, a final aside: sometimes I think there's a curious lack of an object (an object encapsulating a struct ?) in .NET named 'Range.' That you might use like this:
Range<int> myIntRange = new Range<int> { 12, 24 };
if(myIntRange.Contains(22)) ...
Range<char> myCharRange = new Range<char> { 'k', 'r' };
if (! myCharRange.Contains('x')) ...</char></char></int></int>