Beispiel #1
0
        public TkInterval ExtractInterval(TimeSpan timeSpan)
        {
            var newDatePosition   = DateTime.MinValue.AddTicks(end.Ticks - timeSpan.Ticks);
            var extractedInterval = new TkInterval(newDatePosition, end);

            return(extractedInterval);
        }
Beispiel #2
0
        // The complement of B, ∁B. Is everything that's outside of B
        public static TkTimeSet Complement(TkTimeSet set)
        {
            IEnumerable <TkInterval> complement(TkTimeSet s)
            {
                var t0 = DateTime.MinValue;

                foreach (var interval in s.GetOrderedIntervals())
                {
                    var t1 = interval.start;
                    if ((t0 - t1).Ticks != 0)
                    {
                        var complementInterval = new TkInterval(t0, t1);
                        yield return(complementInterval);
                    }
                    t0 = interval.end;
                }

                if ((DateTime.MaxValue - t0).Ticks != 0)
                {
                    var lastInterval = new TkInterval(t0, DateTime.MaxValue);
                    yield return(lastInterval);
                }
            }

            var complementSet = complement(set).ToArray();

            return(new TkTimeSet(complementSet));
        }
Beispiel #3
0
 public bool Overlaps(TkInterval other)
 {
     // Does this interval contain the start or end of the other interval?
     return(Contains(other.start) || Contains(other.end) ||
            // Does the other interval contain the start or end of this interval?
            other.Contains(start) || other.Contains(end));
 }
Beispiel #4
0
        public TkInterval ElementAt(int index)
        {
            if (index >= Count())
            {
                return(TkInterval.Null());
            }

            return(_tkIntervals[index]);
        }
Beispiel #5
0
        public TkTimeSet Copy()
        {
            var copy = new TkInterval[_tkIntervals.Length];

            for (var i = 0; i < _tkIntervals.Length; i++)
            {
                var interval = _tkIntervals[i];
                copy[i] = new TkInterval(interval.start, interval.end);
            }

            return(new TkTimeSet(copy));
        }
Beispiel #6
0
        // Does not join overlapping tkIntervals
        // if an tkInterval in A already contains the space made up by an tkInterval in B, it'll be ignored
        public static TkTimeSet Union(TkTimeSet a, TkTimeSet b)
        {
            if (b.IsNull || b.IsEmpty())
            {
                return(a);
            }
            if (a.IsNull || a.IsEmpty())
            {
                return(b);
            }

            // The size of the union between a and b will at end be a.count + b.count
            var union      = new TkInterval[a.Count() + b.Count()];
            var unionIndex = a.Count();

            // Set the union equal to A
            for (var i = 0; i < a._tkIntervals.Length; i++)
            {
                union[i] = a._tkIntervals[i];
            }

            // Only insert tkIntervals from B into the union
            // if no tkInterval in A claims that space
            for (var i = 0; i < b._tkIntervals.Length; i++)
            {
                var bInterval = b._tkIntervals[i];
                var exists    = false;

                for (var j = 0; j < a._tkIntervals.Length; j++)
                {
                    var aInterval = a._tkIntervals[j];
                    if (aInterval.Contains(bInterval))
                    {
                        exists = true;
                        break;
                    }
                }

                if (!exists)
                {
                    union[unionIndex] = bInterval;
                    unionIndex++;
                }
            }

            return(new TkTimeSet(union));
        }
Beispiel #7
0
        public int indexOf(TkInterval tkInterval)
        {
            if (string.IsNullOrEmpty(tkInterval.id))
            {
                return(-1);
            }

            for (var i = 0; i < Count(); i++)
            {
                var _interval = ElementAt(i);
                if (_interval.id == tkInterval.id)
                {
                    return(i);
                }
            }

            return(-1);
        }
Beispiel #8
0
        public TkInterval ExtractInterval(TimeSpan timeSpan)
        {
            var orderedIntervals      = GetOrderedIntervals();
            var intervalToExtractFrom = orderedIntervals.FirstOrDefault(o => o.Length() > timeSpan);

            if (intervalToExtractFrom.isNull)
            {
                return(TkInterval.Null());
            }

            var index             = indexOf(intervalToExtractFrom);
            var extractedInterval = new TkInterval();

            if (index != -1)
            {
                extractedInterval = intervalToExtractFrom.ExtractInterval(timeSpan);

                intervalToExtractFrom.SubtractTimeSpan(timeSpan);

                var updatedInterval = new TkInterval()
                {
                    id    = intervalToExtractFrom.id,
                    start = intervalToExtractFrom.start,
                    end   = intervalToExtractFrom.end
                };

                // If we have less than 5 minutes left, remove it
                if (updatedInterval.Length() < TimeSpan.FromMinutes(5))
                {
                    Remove(updatedInterval);
                }
                else
                {
                    _tkIntervals[index] = updatedInterval;
                    _tkIntervals        = GetOrderedIntervals();
                }
            }

            return(extractedInterval);
        }
Beispiel #9
0
 public void Remove(TkInterval tkInterval)
 {
     _tkIntervals = _tkIntervals.Where(o => o.id != tkInterval.id).ToArray();
 }
Beispiel #10
0
        public static TkTimeSet Intersect(TkTimeSet first, TkTimeSet second)
        {
            IEnumerable <TkInterval> intersect(TkTimeSet a, TkTimeSet b)
            {
                int aIndex = 0, bIndex = 0;

                while (aIndex < a.Count() &&
                       bIndex < b.Count())
                {
                    var aInterval = a.ElementAt(aIndex);
                    var bInterval = b.ElementAt(bIndex);

                    // If A doesn't contain B
                    if (!aInterval.Overlaps(bInterval))
                    {
                        if (aInterval < bInterval)
                        {
                            aIndex++;
                        }
                        else
                        {
                            bIndex++;
                        }
                    }
                    // If A contains B, return B
                    else if (aInterval.Encloses(bInterval))
                    {
                        yield return(bInterval);

                        bIndex++;
                    }
                    // If B contains A, return A
                    else if (bInterval.Encloses(aInterval))
                    {
                        yield return(aInterval);

                        aIndex++;
                    }
                    // If A and B are separate tkIntervals
                    else
                    {
                        var highestMin = Max(aInterval.start, bInterval.start);
                        var lowestMax  = Min(aInterval.end, bInterval.end);

                        var interval = new TkInterval(highestMin, lowestMax);
                        if (interval.Length() > TimeSpan.Zero)
                        {
                            yield return(interval);
                        }

                        if (aInterval < bInterval)
                        {
                            aIndex++;
                        }
                        else
                        {
                            bIndex++;
                        }
                    }
                }
            }

            var intersection = intersect(first, second).ToArray();

            return(new TkTimeSet(intersection));
        }
Beispiel #11
0
 public bool Encloses(TkInterval other)
 {
     return(Contains(other.start) && Contains(other.end));
 }