public void IntersectWith(IEnumerable <T> other)
        {
            if (other == null)
            {
                throw new ArgumentNullException(nameof(other));
            }

            if (Count == 0)
            {
                return;
            }

            if (this == other)
            {
                return;
            }

            if (other is SortedTreeSet <T> sortedSet && Comparer.Equals(sortedSet.Comparer))
            {
                int i = 0;
                int j = 0;
                while (i < Count && j < sortedSet.Count)
                {
                    int comparison = Comparer.Compare(_sortedList[i], sortedSet._sortedList[j]);
                    if (comparison == 0)
                    {
                        // Keep the item
                        i++;
                        j++;
                    }
                    else if (comparison < 0)
                    {
                        _sortedList.RemoveAt(i);
                    }
                    else
                    {
                        // The item may be present, but it's not the current item
                        j++;
                    }
                }

                _sortedList.RemoveRange(i, Count - i);
            }
            else
            {
                var toSave = new SortedTreeSet <T>(Comparer);
                foreach (T item in other)
                {
                    if (Contains(item))
                    {
                        toSave.Add(item);
                    }
                }

                IntersectWith(toSave);
            }
        }
示例#2
0
        public TValue this[TKey key]
        {
            get
            {
                if (!_treeSet.TryGetValue(new KeyValuePair <TKey, TValue>(key, default), out KeyValuePair <TKey, TValue> value))
                {
                    throw new KeyNotFoundException();
                }

                return(value.Value);
            }

            set
            {
                _treeSet.Remove(new KeyValuePair <TKey, TValue>(key, default));
                _treeSet.Add(new KeyValuePair <TKey, TValue>(key, value));
            }
        }