Esempio n. 1
0
        /// <summary>
        /// </summary>
        /// <param name="BlockIndex"></param>
        /// <param name="NewState"></param>
        public void Set(int BlockIndex, bool NewState, bool DoNotCompact = false)
        {
            if (BlockIndex >= Size)
            {
                Resize(BlockIndex + 1);
            }

            for (int i = 0; i < Ranges.Count; i++)
            {
                Range range = Ranges[i];
                if (BlockIndex >= range.Start && BlockIndex <= range.End)
                {
                    if (range.State != NewState)
                    {
                        int EndOffset = 1;

                        // Start block
                        if (range.Start < BlockIndex)
                        {
                            Ranges.Insert(i, new Range {
                                Start = range.Start, End = BlockIndex - 1, State = range.State
                            });
                            EndOffset++;
                        }

                        // End block
                        if (range.End > BlockIndex)
                        {
                            Ranges.Insert(i + EndOffset, new Range {
                                Start = BlockIndex + 1, End = range.End, State = range.State
                            });
                        }

                        // Remap this range as middle.
                        range.Start = BlockIndex;
                        range.End   = BlockIndex;
                        range.State = NewState;

                        Ranges[i + EndOffset - 1] = range;
                    }

                    break;
                }
            }

            if (!DoNotCompact)
            {
                CompactRanges();
            }

#if DO_VALIDATION
            Validate();
#endif
        }
Esempio n. 2
0
        public void Add(Range range)
        {
            if (Count == 0)
            {
                Ranges.Add(range);
                return;
            }

            if (range.End < FirstRange.Start)
            {
                Ranges.Insert(0, range);
                return;
            }

            if (range.Start > LastRange.End)
            {
                Ranges.Add(range);
                return;
            }

            for (int i = 0; i < Count; i++)
            {
                var liveRange = Ranges[i];

                if (liveRange.Start == range.Start && liveRange.End == range.End)
                {
                    return;
                }

                if (liveRange.IsAdjacent(range) || liveRange.Intersects(range))
                {
                    liveRange = new Range(Math.Min(range.Start, liveRange.Start), Math.Max(range.End, liveRange.End));
                    Ranges[i] = liveRange;

                    for (int z = i + 1; z < Count; z++)
                    {
                        var nextLiveRange = Ranges[z];
                        if (liveRange.IsAdjacent(nextLiveRange) || liveRange.Intersects(nextLiveRange))
                        {
                            var newliveRange = new Range(Math.Min(liveRange.Start, nextLiveRange.Start), Math.Max(liveRange.End, nextLiveRange.End));
                            Ranges[i] = liveRange;
                            Ranges.RemoveAt(z);

                            continue;
                        }

                        return;
                    }

                    return;
                }
            }
        }