Ejemplo n.º 1
0
        public void AddX(int value)
        {
            if (LastAdded == value)
            {
                return;
            }
            LastAdded = value;
            _Count    = -1;
            if (LastInterval == null)
            {
                AddNewInterval(value, 0);
                return;
            }
            //if (value == LastInterval.End)
            //{
            //    return;
            //}
            if (LastInterval.IsNext(value))
            {
                LastInterval.End++;
                return;
            }
            if (value > LastInterval.End)
            {
                AddNewInterval(value);
                return;
            }
            if (FirstInterval.IsPrev(value))
            {
                FirstInterval.Start--;
                return;
            }
            if (value < FirstInterval.Start)
            {
                AddNewInterval(value, 0);
                return;
            }

            var vi = Intervals.BinarySearch(new Interval(value), ic);

            if (vi > -1 && vi < Intervals.Count)
            {
                var i    = Intervals[vi];
                var prev = vi - 1 > -1 ? Intervals[vi - 1] : null;
                var next = vi + 1 < Intervals.Count ? Intervals[vi + 1] : null;


                if (i.Start - 1 == value)
                {
                    if (prev == null || prev.End < value)
                    {
                        i.Start--;
                    }
                    return;
                }
                if (i.End + 1 == value)
                {
                    if (next == null || next.Start > value)
                    {
                        i.End++;
                    }
                    return;
                }
                if (value < i.Start)
                {
                    AddNewInterval(value, vi); return;
                }
                if (value > i.End)
                {
                    AddNewInterval(value, vi + 1); return;
                }
            }
            else
            {
                vi = ~vi;
                AddNewInterval(value, vi);
            }
        }