/// <summary> /// For an array of monotonically increasing values, returns /// the index i of the largest value such that array[i] <= x. /// If x is smaller than the first value, then -1 is returned. /// If x is greater than the last value, then a.Length is returned. /// </summary> public static long LongIndexOfLargestLessOrEqual <T>(this T[] a, T x) where T : IComparable { var r = new Range1l(0, a.Length - 1); if (x.CompareTo(a[0]) < 0) { return(-1); } if (x.CompareTo(a[r.Max]) > 0) { return(a.Length); } while (r.Size > 1) { var half = r.Center; if (x.CompareTo(a[half]) < 0) { r.Max = half; } else { r.Min = half; } } return(r.Min); }
public void Write(Range1l x) { Write(x.Min); Write(x.Max); }