public GetRange ( System.DateTime RangeStart, System.DateTime RangeEnd ) : IEnumerable |
||
RangeStart | System.DateTime | |
RangeEnd | System.DateTime | |
리턴 | IEnumerable |
// Optimized 2008-07-13 public IEnumerable <TimedEvent> GetRange(DateTime RangeStart, DateTime RangeEnd) { // AAAA // BBBB // xxxxxx // // AA // BB // xxxx // // AAAA // BBBB // xxxxxx // CC // xxxxxxxx TimedEvent MatchEvent = null; foreach (TimedEvent A in _Schedule.GetRange(RangeStart, RangeEnd)) { if (MatchEvent == null) { // Load up MatchEvent MatchEvent = A; } else if (MatchEvent.Intersects(A) || MatchEvent.IsAdjacentTo(A)) { // Compute union and move on MatchEvent = MatchEvent.Union(A)[0]; } else { // No intersection, return MatchEvent yield return(MatchEvent); MatchEvent = A; } } // If MatchEvent has a value, return it if (MatchEvent != null) { yield return(MatchEvent); } yield break; }
static void Main(string[] args) { // Create schedule programmatically. ISchedule schedule1 = new IntervalSchedule(TimeSpan.FromSeconds(11), TimeSpan.Zero, DateTime.MinValue); ISchedule schedule2 = new IntervalSchedule(TimeSpan.FromMinutes(1), TimeSpan.Zero, DateTime.MinValue.AddSeconds(1)); ISchedule schedule3 = new CronSchedule("*/5", "*", "*", "*", "*", TimeSpan.Zero); ISchedule combinedSchedule = new ListSchedule(new[] { schedule1, schedule2, schedule3 }); // Print schedule TDL. Console.WriteLine("Forecasting events from expression:\n{0}", combinedSchedule.ToString()); // Forecast timed events for the next hour. IEnumerable<TimedEvent> events = combinedSchedule.GetRange(DateTime.Now, DateTime.Now.AddHours(1)); int eventCount = events.Count(); Console.WriteLine("Found {0:d} events.", eventCount); // Print to screen. foreach (TimedEvent e in events) { Console.WriteLine("Event time: {0:G}", e.StartTime); } }
// Optimized 2008-07-13 public IEnumerable <TimedEvent> GetRange(DateTime RangeStart, DateTime RangeEnd) { /* * Input: * A: xxxx xxxx * B: xxxxxx * * Compare: * A: AAAA xxxx * B: BBBBBB * * Compute difference: * * A: AA-- xxxx * B: --BBBB * * Compare next: * A: AA xxxx * B: BBBB * * Compare next: * A: AA BBBB * B: xxxx * * Compare next: * A: RR BBBB * B: AAAA * * Compute difference: * A: RR --BB * B: AA-- * * Result: * RR RR RR * */ IEnumerator <TimedEvent> ListIterator = _Schedule.GetRange(RangeStart, RangeEnd).GetEnumerator(); if (ListIterator.MoveNext()) { TimedEvent DiffEvent = null; DateTime Max = ListIterator.Current.StartTime; var BufferList = new List <TimedEvent>(new TimedEvent[] { ListIterator.Current }); do { // Get next event TimedEvent A = BufferList[0]; BufferList.RemoveAt(0); // Figure out what to do with it if (DiffEvent == null) { DiffEvent = A; } else if (DiffEvent.Intersects(A)) { // With an intersection, add difference events to list and redo TimedEvent[] DiffEvents = DiffEvent.Difference(A); if (DiffEvents.Length > 0) { TimedEvent LastEvent = DiffEvents[DiffEvents.Length - 1]; if (LastEvent.StartTime > Max) { Max = LastEvent.StartTime; } BufferList.AddRange(DiffEvents); BufferList.Sort(); } DiffEvent = null; } else { // No intersection, return working DiffEvent yield return(DiffEvent); DiffEvent = A; } // Fetch more events? if (BufferList.Count == 0) { while (ListIterator.MoveNext()) { TimedEvent Event1 = ListIterator.Current; BufferList.Add(Event1); // Stop buffering when we pass the maximum StartTime we need if (Event1.StartTime > Max) { Max = Event1.StartTime; break; } } } // Continue as long as more data exists } while (BufferList.Count > 0); // If working DiffEvent has a value, return it if (DiffEvent != null) { yield return(DiffEvent); } } yield break; }