예제 #1
0
 public static LocalDateTime?MinStart(LocalInterval interval1, LocalInterval interval2)
 {
     if (!interval1.Start.HasValue || !interval2.Start.HasValue)
     {
         return(null);
     }
     return(Min(interval1.Start.Value, interval2.Start.Value));
 }
예제 #2
0
 public static bool Starts(LocalInterval interval1, LocalInterval interval2)
 {
     if (!interval1.Start.HasValue || !interval2.Start.HasValue)
     {
         return(true);
     }
     return(Min(interval1.Start.Value, interval2.Start.Value) == interval1.Start.Value);
 }
예제 #3
0
 public static bool Ends(LocalInterval interval1, LocalInterval interval2)
 {
     if (!interval1.End.HasValue || !interval2.End.HasValue)
     {
         return(true);
     }
     return(Max(interval1.End.Value, interval2.End.Value) == interval1.End.Value);
 }
예제 #4
0
 public static LocalDateTime?MaxEnd(LocalInterval interval1, LocalInterval interval2)
 {
     if (!interval1.End.HasValue || !interval2.End.HasValue)
     {
         return(null);
     }
     return(Max(interval1.End.Value, interval2.End.Value));
 }
예제 #5
0
 public static bool AreTouching(LocalInterval int1, LocalInterval int2)
 {
     if (!int2.Start.HasValue && !int1.Start.HasValue)
     {
         return(true);
     }
     return(int2.Start.HasValue && int1.Touches(int2.Start.Value) ||
            int1.Start.HasValue && int2.Touches(int1.Start.Value));
 }
 protected bool Equals(LocalInterval other)
 {
     return(string.Equals(Name, other.Name) &&
            Start.Equals(other.Start) &&
            End.Equals(other.End) &&
            (
                Payload == null && other.Payload == null ||
                Payload != null && Payload.Equals(other.Payload)
            ));
 }
예제 #7
0
 public static LocalDateTime?MaxStart(LocalInterval interval1, LocalInterval interval2)
 {
     if (!interval1.Start.HasValue)
     {
         return(interval2.Start);
     }
     if (!interval2.Start.HasValue)
     {
         return(interval1.Start);
     }
     return(Max(interval1.Start.Value, interval2.Start.Value));
 }
예제 #8
0
 public static LocalDateTime?MinEnd(LocalInterval interval1, LocalInterval interval2)
 {
     if (!interval1.End.HasValue)
     {
         return(interval2.End);
     }
     if (!interval2.End.HasValue)
     {
         return(interval1.End);
     }
     return(Min(interval1.End.Value, interval2.End.Value));
 }
예제 #9
0
        public static IEnumerable <LocalInterval> Fragment(IEnumerable <LocalInterval> intervals1, IEnumerable <LocalInterval> intervals2)
        {
            var int1List = intervals1.ToList();
            var int2List = intervals2.ToList();

            var aggregate = int1List.Select(i => new Edge(i, i.Start, true))
                            .Concat(int1List.Select(i => new Edge(i, i.End)))
                            .Concat(int2List.Select(i => new Edge(i, i.Start, true)))
                            .Concat(int2List.Select(i => new Edge(i, i.End)))
                            .OrderBy(dateTime => dateTime.Value)
                            .Aggregate(new IntervalAggregator(), (agg, edge) =>
            {
                if (agg.Fragments.Count > 0 && agg.Intervals.Count > 0)
                {
                    var close = agg.Fragments.Where(f => !f.End.HasValue).ToList();

                    for (int i = 0; i < close.Count; i++)
                    {
                        close[i] = new LocalInterval(close[i].Start, edge.Value, close[i].Payload);
                    }

                    agg.Fragments = agg.Fragments.Where(f => f.End.HasValue).Concat(close).ToList();
                }

                if (edge.IsStart)
                {
                    agg.Intervals.Add(edge.Interval);
                }
                else
                {
                    agg.Intervals.Remove(edge.Interval);
                }

                if (agg.Intervals.Count > 0)
                {
                    agg.Intervals
                    .Where(i => i.Payload != null)
                    .GroupBy(i => i.Payload.GetType())
                    .ToList()
                    .ForEach(grp =>
                    {
                        var total = grp.Aggregate((TimePayloadBase) new NullPayload(), (_total, interval) => _total + interval.Payload);
                        agg.Fragments.Add(new LocalInterval(edge.Value, null, total));
                    });
                }

                return(agg);
            });

            return(aggregate.Fragments.Where(f => f.Start != f.End));
        }
예제 #10
0
        public static List <LocalInterval> Not(LocalInterval interval)
        {
            var neg = new List <LocalInterval>();

            if (interval.Start.HasValue)
            {
                neg.Add(new LocalInterval(null, interval.Start, interval.Payload));
            }

            if (interval.End.HasValue)
            {
                neg.Add(new LocalInterval(interval.End, null, interval.Payload));
            }

            return(neg);
        }
예제 #11
0
        public static IEnumerable <LocalInterval> MergeTouching(IEnumerable <LocalInterval> intervals)
        {
            return(intervals
                   .OrderBy(i => i.Start)
                   .Aggregate(new List <LocalInterval>(), (result, interval) =>
            {
                var last = result.LastOrDefault();
                var toAdd = interval.Clone();

                if (last != null &&
                    AreTouching(last, toAdd) &&
                    (
                        last.Payload == null && toAdd.Payload == null ||
                        last.Payload != null && last.Payload.Equals(toAdd.Payload)
                    ))
                {
                    result = result.Take(result.Count - 1).ToList();
                    toAdd = new LocalInterval(MinStart(last, toAdd), MaxEnd(last, toAdd), toAdd.Payload?.Clone(), toAdd.Name);
                }

                result.Add(toAdd);
                return result;
            }));
        }
예제 #12
0
 public Edge(LocalInterval interval, LocalDateTime?value, bool isStart = false)
 {
     Interval = interval;
     Value    = value;
     IsStart  = isStart;
 }
예제 #13
0
 public static IEnumerable <LocalInterval> Intersection(IEnumerable <LocalInterval> intervals, LocalInterval interval)
 {
     return(Intersection(intervals, new List <LocalInterval> {
         interval
     }));
 }
예제 #14
0
 public static bool Overlap(LocalInterval interval1, LocalInterval interval2)
 {
     return((interval1.Start == null || interval2.End == null || interval1.Start < interval2.End) &&
            (interval2.Start == null || interval1.End == null || interval1.End >= interval2.Start));
 }
예제 #15
0
        private static IEnumerable <LocalDateTime?> ToDates(LocalInterval interval)
        {
            yield return(interval.Start);

            yield return(interval.End);
        }