예제 #1
0
            /// <inheritdoc cref="ImmutableHashSet{T}.Builder.ExceptWith(IEnumerable{T})"/>
            public void ExceptWith(IEnumerable <T> other)
            {
                if (other is null)
                {
                    ThrowHelper.ThrowArgumentNullException(ExceptionArgument.other);
                }

                if (_mutableSet is not null)
                {
                    _mutableSet.ExceptWith(other);
                    return;
                }

                if (other == this)
                {
                    Clear();
                    return;
                }
                else if (other is ImmutableSegmentedHashSet <T> otherSet)
                {
                    if (otherSet == _set)
                    {
                        Clear();
                        return;
                    }
                    else if (otherSet.IsEmpty)
                    {
                        // No action required
                        return;
                    }
                    else
                    {
                        GetOrCreateMutableSet().ExceptWith(otherSet._set);
                        return;
                    }
                }
                else
                {
                    // Manually enumerate to avoid changes to the builder if 'other' is empty or does not contain any
                    // items present in the current set.
                    SegmentedHashSet <T>?mutableSet = null;
                    foreach (var item in other)
                    {
                        if (mutableSet is null)
                        {
                            if (!ReadOnlySet.Contains(item))
                            {
                                continue;
                            }

                            mutableSet = GetOrCreateMutableSet();
                        }

                        mutableSet.Remove(item);
                    }

                    return;
                }
            }