public TResult Simplify <TResult>([NotNull] ICollection <NumericRange <T> > obj, NumericRangeComparer <T> comparer = null, TResult defaultValue = default(TResult))
            where TResult : List <NumericRange <T> >, new()
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }
            if (obj.Count == 0)
            {
                return(defaultValue);
            }

            TResult result = new TResult();

            result.AddRange(obj);
            if (result.Count <= 1)
            {
                return(result);
            }

            comparer ??= NumericRangeComparer <T> .Default;
            result.Sort(comparer);

            for (int i = result.Count - 1; i >= 0 && result.Count > 1; --i)
            {
                int n = 0;
                NumericRange <T> x = result[i];

                for (int j = result.Count - 1; j >= 0 && result.Count > 1; --j)
                {
                    NumericRange <T> y = result[j];
                    if (!x.Merge(y))
                    {
                        continue;
                    }
                    result[i] = x;
                    result.RemoveAt(j);
                    --j;
                    ++n;
                }

                i -= n;
            }

            return(result);
        }
示例#2
0
 public virtual TResult Simplify <TResult>(NumericRangeComparer <T> comparer = null, TResult defaultValue = default(TResult))
     where TResult : List <NumericRange <T> >, new()
 {
     return(NumericRangeCollectionComparer <T> .Default.Simplify(this, comparer, defaultValue));
 }