public bool Contains(SelectionCellRangeWithItems rangeWithItems)
        {
            if ((m_unsortedRanges.Count <= 0) || (SelectedCellsStorage.IsEmpty(rangeWithItems)))
            {
                return(false);
            }

            if (rangeWithItems.Length == 1)
            {
                return(this.IndexOfOverlap(rangeWithItems).Any());
            }

            var store = new SelectedCellsStorage(null);

            store.Add(rangeWithItems);

            foreach (var match in this.IndexOfOverlap(rangeWithItems))
            {
                store.Remove(m_unsortedRanges[match].Value);

                if (store.Count == 0)
                {
                    return(true);
                }
            }

            return(false);
        }
        public bool Contains(SelectionCellRange range)
        {
            if ((m_unsortedRanges.Count <= 0) || (SelectedCellsStorage.IsEmpty(range)))
            {
                return(false);
            }

            return(this.Contains(new SelectionCellRangeWithItems(range.ItemRange, null, range.ColumnRange)));
        }
            internal ItemComparer(SelectionCellRangeWithItemsWrapper target)
            {
                if (target == null)
                {
                    throw new ArgumentNullException("target");
                }

                if (SelectedCellsStorage.IsEmpty(target.Value))
                {
                    throw new ArgumentException("The selection range must not be empty.", "target");
                }

                m_target = target;
            }
        private IEnumerable <int> IndexOfOverlap(SelectionCellRange target)
        {
            if ((m_sortedRanges.Count <= 0) || SelectedCellsStorage.IsEmpty(target))
            {
                yield break;
            }

            var comparer = new ItemRangeComparer(target.ItemRange);

            var index = this.FindIndex(comparer);

            if (index < 0)
            {
                yield break;
            }

            while (index > 0)
            {
                if (comparer.Compare(m_sortedRanges[index - 1]) != 0)
                {
                    break;
                }

                index--;
            }

            for (int i = index; i < m_sortedRanges.Count; i++)
            {
                var wrapper = m_sortedRanges[i];
                if (comparer.Compare(wrapper) != 0)
                {
                    break;
                }

                var currentRangeWithItems = wrapper.Value;
                var overlap = target.Intersect(currentRangeWithItems.CellRange);

                if (!SelectedCellsStorage.IsEmpty(overlap))
                {
                    yield return(wrapper.Index);
                }
            }
        }
        private void Insert(int index, SelectionCellRangeWithItems item, bool repairIndex)
        {
            var wrapper = new SelectionCellRangeWithItemsWrapper(item, index);

            m_unsortedRanges.Insert(index, wrapper);

            if (repairIndex)
            {
                this.RepairIndex(index + 1);
            }

            if (!SelectedCellsStorage.IsEmpty(item))
            {
                var insertionIndex = ~this.FindIndex(new ItemComparer(wrapper));
                Debug.Assert(insertionIndex >= 0);

                m_sortedRanges.Insert(insertionIndex, wrapper);
            }
        }
        private void RemoveAt(int index, bool repairIndex)
        {
            var wrapper = m_unsortedRanges[index];

            Debug.Assert(wrapper.Index == index);

            if (!SelectedCellsStorage.IsEmpty(wrapper.Value))
            {
                var removalIndex = this.FindIndex(new ItemComparer(wrapper));
                Debug.Assert(removalIndex >= 0);

                m_sortedRanges.RemoveAt(removalIndex);
            }

            m_unsortedRanges.RemoveAt(index);

            if (repairIndex)
            {
                this.RepairIndex(index);
            }
        }