示例#1
0
        /// <summary>
        /// See the <see cref="IImmutableDictionary{TKey, TValue}"/> interface.
        /// </summary>
        public ImmutableDictionary <TKey, TValue> WithComparers(IEqualityComparer <TKey>?keyComparer, IEqualityComparer <TValue>?valueComparer)
        {
            if (keyComparer == null)
            {
                keyComparer = EqualityComparer <TKey> .Default;
            }

            if (valueComparer == null)
            {
                valueComparer = EqualityComparer <TValue> .Default;
            }

            if (this.KeyComparer == keyComparer)
            {
                if (this.ValueComparer == valueComparer)
                {
                    return(this);
                }
                else
                {
                    // When the key comparer is the same but the value comparer is different, we don't need a whole new tree
                    // because the structure of the tree does not depend on the value comparer.
                    // We just need a new root node to store the new value comparer.
                    var comparers = _comparers.WithValueComparer(valueComparer);
                    return(new ImmutableDictionary <TKey, TValue>(_root, comparers, _count));
                }
            }
            else
            {
                var comparers = Comparers.Get(keyComparer, valueComparer);
                var set       = new ImmutableDictionary <TKey, TValue>(comparers);
                set = set.AddRange(this, avoidToHashMap: true);
                return(set);
            }
        }