public IAsyncEnumerator <long> GetAsyncEnumerator(CancellationToken cancellationToken) { var en = new IntervalEnumerator(_period, _start, _end, cancellationToken); en.StartFirst(_initialDelay); return(en); }
public IAsyncEnumerator <long> GetAsyncEnumerator() { var en = new IntervalEnumerator(_period, _start, _end); en.StartFirst(_initialDelay); return(en); }
static void StepAnchorTestHelper <T, TSize>(T start, T end, TSize stepSize, T anchor, T[] expected) where T : IComparable <T> where TSize : IComparable <TSize> { var enumerator = new IntervalEnumerator <T, TSize>(new Interval <T, TSize>(start, end), stepSize, anchor); var test = enumerator.ToArray(); Assert.IsTrue(expected.SequenceEqual(test)); }
static void StepTestHelper <T, TSize>(T start, bool startIncluded, T end, bool endIncluded, TSize stepSize, T[] expected) where T : IComparable <T> where TSize : IComparable <TSize> { var enumerator = new IntervalEnumerator <T, TSize>( new Interval <T, TSize>(start, startIncluded, end, endIncluded), stepSize); Assert.IsTrue(expected.SequenceEqual(enumerator)); }
// Time: O(n * logN) // Space: O(n) public static List <Interval> MergeOverlapingIntervals(List <Interval> intervals) { // If there is only 1 interval then just return the input. if (intervals.Count < 2) { return(intervals); } // Order the objects by their start properties intervals = intervals.OrderBy(x => x.Start).ToList(); // This is the output List <Interval> mergedIntervals = new List <Interval>(); // Define an enumerator for the interval objects so we can while loop through them. IntervalEnumerator intervalEnumerator = new IntervalEnumerator(intervals); // Iterate to the first Interval intervalEnumerator.MoveNext(); Interval interval = intervalEnumerator.Current; int start = interval.Start; int end = interval.End; do { interval = intervalEnumerator.Current; if (interval.Start <= end) { end = Math.Max(interval.End, end); } else { mergedIntervals.Add(new Interval(start, end)); start = interval.Start; end = interval.End; } } while (intervalEnumerator.MoveNext()); mergedIntervals.Add(new Interval(start, end)); return(mergedIntervals); }