Beispiel #1
0
        public static TreeDictionary <int, int> ProcessRanges(IEnumerable <Tuple <int, int> > ranges)
        {
            var summedRangeEdges = new TreeDictionary <int, int>();

            foreach (var currentRange in ranges)
            {
                var currentRangeLeft  = currentRange.Item1;
                var currentRangeRight = currentRange.Item2;

                // Add left boundary if it isn't already there, with its value set as the value of the summed range it is included in, otherwise zero
                // E.g.: 1 to 10 with value 1 becomes 1 to 5 with value 1, 5 to 10 with value 1

                // Check if the node exists in the Tree
                if (!summedRangeEdges.Exists(x => x.Key == currentRangeLeft))
                {
                    summedRangeEdges.Add(currentRangeLeft,
                                         summedRangeEdges.TryPredecessor(currentRangeLeft, out var predecessor)
                            ? predecessor.Value
                            : 0);
                }

                // Increment the value of all summed range edges between the left boundary (inclusive) and the right boundary (exclusive)
                var summedRangesSubMap = summedRangeEdges.RangeFromTo(currentRangeLeft, currentRangeRight);

                var keys = new SortedArray <int>();
                foreach (var key in summedRangesSubMap)
                {
                    keys.Add(key.Key);
                }

                foreach (var key in keys)
                {
                    summedRangeEdges[key]++;
                }

                // Add the right boundary
                // If there isn't a summed range edge to its left, use 0 for the value (should never happen as per the "put" above)
                // If the right boundary was already in the map, leave it as is
                // If the right boundary wasn't in the map, use the value to its left minus 1 (since this is a right boundary, which means a range was closed)

                if (summedRangeEdges.Exists(x => x.Key == currentRangeRight))
                {
                    continue;
                }
                {
                    if (summedRangeEdges.TryPredecessor(currentRangeRight, out var predecessor))
                    {
                        summedRangeEdges.Add(currentRangeRight, predecessor.Value - 1);
                    }
                    else
                    {
                        summedRangeEdges.Add(currentRangeRight, 0);
                    }
                }
            }

            return(summedRangeEdges);
        }
        public void Insert(PointFr p, EventPoint ep) // Wstawia zdarzenie ep w punkcie p do bieżącej kolejki zdarzeń zdarzenia Right są dodawane na początku, pozostałe na końcu
        {
            var existing = new C5.LinkedList <EventPoint>();

            if (_events.Exists(kvp => kvp.Key.Equals(p)))
            {
                existing = _events[p];
                _events.Remove(p);
            }

            if (ep.Type == EventPointType.Right) // Zdarzenia 'Right' powinny byc na początku listy
            {
                existing.Insert(0, ep);
            }
            else
            {
                existing.Add(ep);
            }
            _events.Add(p, existing);
        }