Exemplo n.º 1
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);
        }
Exemplo n.º 2
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));
        }