Fastest Way to compare if a value is in Range

Go To StackoverFlow.com

0

I have a class

public class VitalSign
{
    public double? Temprature { get; set; }
    public double? SystolicBP { get; set; }
    public double? DiastolicBP { get; set; }
    public double? PulseRate { get; set; }
    public double? Respirations { get; set; }
}

and a Range class

 public  class VitalRange
{
    public Tuple<double,double> TemperatureRange { get; set; }
    public Tuple<double, double> SystolicBPRange { get; set; }
    public Tuple<double, double> DiastolicBPRange { get; set; }
    public Tuple<double, double> RespirationsRange { get; set; }
    public Tuple<double, double> PulseRange { get; set; }
}

I have to compare if my property(say temperature) is out of range compared to VitalRange( Temperature). Can I use a Tuple in this case or can I have a KVP for ranges?. I will be comparing a huge List against VitalRange. What will be the fastest way to check if a property falls within the range.?

2012-04-05 22:27
by Greens
What language - Paul R 2012-04-05 22:29
C# 4.0 ....... - Greens 2012-04-05 22:30
OK - added C# ta - Paul R 2012-04-05 22:32


1

I don't know what's your problem, It's simple if, but you can encapsulate it by using extension methods:

public static bool IsInRange(this double? input, Tuple<double, double> range)
{
   if (!input.HasValue)
      return false;
   return input >= range.Item1 && input <= range.Item2;
}

and use it like this:

VitalRange sampleRanges = ....;
var validTempers = vitalSignList.Where(x=>x.Temprature.IsInRange(sampleRange.VitalRange);

I can't think for extremely faster way, but I prefer encapsulation.

2012-04-05 22:48
by Saeed Amiri
Ads