Esempio n. 1
0
        public void Remove(IntRange range)
        {
            // if the range doesn't intersect this collection do nothing
            if (!range.Intersects(IntRange.FromEndpoints(this.Lower, this.Upper)))
            {
                return;
            }

            var middle = new List <IntRange>();

            foreach (var S in this.ranges)
            {
                if (!range.Intersects(S))
                {
                    middle.Add(S);
                    continue;
                }

                if ((range.Lower <= S.Lower) && (range.Upper >= S.Upper))
                {
                    // disregard S completely
                    continue;
                }

                if (range.Lower > S.Lower)
                {
                    if (S.Lower <= (range.Lower - 1))
                    {
                        var X = IntRange.FromEndpoints(S.Lower, range.Lower - 1);
                        middle.Add(X);
                    }
                }

                if (range.Upper <= S.Upper)
                {
                    if ((range.Upper + 1) <= S.Upper)
                    {
                        var X = IntRange.FromEndpoints(range.Upper + 1, S.Upper);
                        middle.Add(X);
                    }
                }
                else
                {
                    throw new System.InvalidOperationException("internal error");
                }
            }

            this.ranges = middle;
        }
Esempio n. 2
0
        public void Add(IntRange range)
        {
            var left  = new List <IntRange>();
            var right = new List <IntRange>();

            foreach (var rng in this.ranges)
            {
                if (range.Intersects(rng) || range.Touches(rng))
                {
                    range = range.JoinWith(rng);
                }
                else if (rng.Upper < range.Lower)
                {
                    left.Add(rng);
                }
                else if (range.Upper < rng.Lower)
                {
                    right.Add(rng);
                }
                else
                {
                    throw new System.InvalidOperationException("Internal Error");
                }
            }

            this.ranges = left.Concat(EnumerableUtil.Single(range)).Concat(right).ToList();
        }