예제 #1
0
        public virtual bool Equals(RangeBase <T> other)
        {
            if (other == null)
            {
                return(false);
            }

            return(this.Comparer.IsEqualTo(this.Start, other.Start) && this.Comparer.IsEqualTo(this.End, other.End));
        }
예제 #2
0
        public static IEnumerable <RangeBase <T> > FromEnumerable(IEnumerable <T> enumerable, T maxInterval)
        {
            var ranges = new List <RangeBase <T> >();

            RangeBase <T> currentRange = null;

            foreach (var item in enumerable)
            {
                if (currentRange == null)
                {
                    currentRange = new RangeBase <T>(item, item);
                    ranges.Add(currentRange);
                    continue;
                }

                if (currentRange.TryMerge(item, maxInterval))
                {
                    continue;
                }

                var range = ranges.FirstOrDefault(x => x != currentRange && x.TryMerge(item, maxInterval));
                if (range != null)
                {
                    currentRange = range;
                    continue;
                }

                currentRange = new RangeBase <T>(item, item);
                ranges.Add(currentRange);
            }

            var targetRanges = new List <RangeBase <T> >(ranges.Count);

            foreach (var range in ranges.Where(range => !targetRanges.Any(x => x.TryMerge(range, maxInterval))))
            {
                targetRanges.Add(range);
            }

            return(targetRanges);
        }
예제 #3
0
        public virtual bool TryMerge(RangeBase <T> other, T maxInterval)
        {
            var comparer   = this.Comparer ?? other.Comparer;
            var subtractor = Subtractor <T> .Default;
            var absolutor  = Absolutor <T> .Default;

            if (!this.IsValid || !other.IsValid)
            {
                throw new ArgumentException("Invalid range");
            }

            if (this.Intersects(other))
            {
                this.Start = comparer.Min(this.Start, other.Start);
                this.End   = comparer.Max(this.End, other.End);
                return(true);
            }

            var diffAbs  = absolutor.Abs(subtractor.Subtract(this.Start, other.End));
            var diffAbs1 = absolutor.Abs(subtractor.Subtract(this.End, other.Start));

            if (comparer.IsBiggerThan(diffAbs, maxInterval) && comparer.IsBiggerThan(diffAbs1, maxInterval))
            {
                return(false);
            }

            if (comparer.IsBiggerThanOrEqual(this.Start, other.End))
            {
                this.Start = other.Start;
                return(true);
            }
            else if (comparer.IsLessThanOrEqual(this.End, other.Start))
            {
                this.End = other.End;
                return(true);
            }

            return(false);
        }
예제 #4
0
 public static IEnumerable <TimeSpanRange> FromEnumerable(IEnumerable <TimeSpan> enumerable, int maxInterval = 1)
 {
     return(RangeBase <long> .FromEnumerable(enumerable.Select(x => x.Ticks), maxInterval).Select(x => new TimeSpanRange(x)));
 }
예제 #5
0
 public TimeSpanRange(RangeBase <long> range, Comparer <TimeSpan> comparer = null) : this(range.Start, range.End, comparer)
 {
 }