BiasCorrectQ.Utils.ValueIndex C# (CSharp) Method

ValueIndex() static private method

static private ValueIndex ( double value, List list ) : int
value double
list List
return int
        internal static int ValueIndex(double value, List<double> list)
        {
            bool listAscending = (list.Last() > list.First());

            for (int i = 0; i < list.Count; i++)
            {
            bool found = listAscending ? list[i] >= value : value >= list[i];

            if (found)
            {
                return i;
            }
            }
            return -1;
        }

Usage Example

Example #1
0
        private static double Interpolate(double value, List <double> valuesList,
                                          List <double> interpList)
        {
            int idx = Utils.ValueIndex(value, valuesList);

            // out of bounds, interpolation unknown
            if (idx < 0)
            {
                return(idx);
            }

            // no interpolation needed, first value in list
            if (idx == 0)
            {
                return(interpList.First());
            }

            double x  = value;
            double x1 = valuesList[idx - 1];
            double x2 = valuesList[idx];

            double y1 = interpList[idx - 1];
            double y2 = interpList[idx];

            return(Utils.Interpolate(x, x1, x2, y1, y2));
        }