public static IntervalDictionary <K, VR> Filter <K, V, VR>(this IntervalDictionary <K, V> me, Interval <K> filter, Func <Interval <K>, V, VR> mapper)
            where K : struct, IComparable <K>, IEquatable <K>
        {
            IntervalDictionary <K, VR> result = new IntervalDictionary <K, VR>();

            foreach (var item in me)
            {
                var intersection = item.Key.Intersection(filter);
                if (intersection != null)
                {
                    result.Add(intersection.Value, mapper(intersection.Value, item.Value));
                }
            }
            return(result);
        }
        internal static IntervalDictionary <K, int> ToIndexIntervalDictinary <Q, K>(this IEnumerable <Q> squares, Func <Q, IEnumerable <K> > func)
            where K : struct, IComparable <K>, IEquatable <K>
        {
            List <K> list = squares.SelectMany(func).Distinct().ToList();

            list.Sort();

            IntervalDictionary <K, int> result = new IntervalDictionary <K, int>();

            for (int i = 0; i < list.Count - 1; i++)
            {
                result.Add(new Interval <K>(list[i], list[i + 1]), i);
            }

            return(result);
        }