Example #1
0
            public TValue GetValueOrDefault(TKey key, TValue defaultValue)
            {
                Requires.NotNullAllowStructs(key, "key");

                TValue value;

                if (this.TryGetValue(key, out value))
                {
                    return(value);
                }

                return(defaultValue);
            }
Example #2
0
        public static TValue GetValueOrDefault <TKey, TValue>(this IImmutableDictionary <TKey, TValue> dictionary, TKey key, TValue defaultValue) where TKey : notnull
        {
            Requires.NotNull(dictionary, nameof(dictionary));
            Requires.NotNullAllowStructs(key, nameof(key));

            TValue value;

            if (dictionary.TryGetValue(key, out value !))
            {
                return(value);
            }

            return(defaultValue);
        }
Example #3
0
            internal ref readonly TValue ValueRef(TKey key, IComparer <TKey> keyComparer)
            {
                Requires.NotNullAllowStructs(key, nameof(key));
                Requires.NotNull(keyComparer, nameof(keyComparer));

                var match = this.Search(key, keyComparer);

                if (match.IsEmpty)
                {
                    throw new KeyNotFoundException(SR.Format(SR.Arg_KeyNotFoundWithKey, key.ToString()));
                }

                return(ref match._value);
            }
Example #4
0
            /// <summary>
            /// Initializes a new instance of the <see cref="ImmutableSortedSet{T}.Node"/> class
            /// that is not yet frozen.
            /// </summary>
            /// <param name="key">The value stored by this node.</param>
            /// <param name="left">The left branch.</param>
            /// <param name="right">The right branch.</param>
            /// <param name="frozen">Whether this node is prefrozen.</param>
            private Node(T key, Node left, Node right, bool frozen = false)
            {
                Requires.NotNullAllowStructs(key, nameof(key));
                Requires.NotNull(left, nameof(left));
                Requires.NotNull(right, nameof(right));
                Debug.Assert(!frozen || (left._frozen && right._frozen));

                _key    = key;
                _left   = left;
                _right  = right;
                _height = checked ((byte)(1 + Math.Max(left._height, right._height)));
                _count  = 1 + left._count + right._count;
                _frozen = frozen;
            }
Example #5
0
        public static TValue GetValueOrDefault <TKey, TValue>(this IImmutableDictionary <TKey, TValue> dictionary, TKey key, TValue defaultValue)
        {
            Requires.NotNull(dictionary, "dictionary");
            Requires.NotNullAllowStructs(key, "key");

            TValue value;

            if (dictionary.TryGetValue(key, out value))
            {
                return(value);
            }

            return(defaultValue);
        }
Example #6
0
        /// <summary>
        /// Gets the <typeparamref name="TValue"/> with the specified key.
        /// </summary>
        public TValue this[TKey key]
        {
            get
            {
                Requires.NotNullAllowStructs(key, nameof(key));

                TValue value;
                if (this.TryGetValue(key, out value))
                {
                    return(value);
                }

                throw new KeyNotFoundException();
            }
        }
Example #7
0
            internal bool Contains(KeyValuePair <TKey, TValue> pair, IComparer <TKey> keyComparer, IEqualityComparer <TValue> valueComparer)
            {
                Requires.NotNullAllowStructs(pair.Key, nameof(pair.Key));
                Requires.NotNull(keyComparer, nameof(keyComparer));
                Requires.NotNull(valueComparer, nameof(valueComparer));

                var matchingNode = this.Search(pair.Key, keyComparer);

                if (matchingNode.IsEmpty)
                {
                    return(false);
                }

                return(valueComparer.Equals(matchingNode._value, pair.Value));
            }
Example #8
0
        public bool TryGetValue(T equalValue, out T actualValue)
        {
            Requires.NotNullAllowStructs(equalValue, "value");

            int        hashCode = this.equalityComparer.GetHashCode(equalValue);
            HashBucket bucket;

            if (this.root.TryGetValue(hashCode, out bucket))
            {
                return(bucket.TryExchange(equalValue, this.equalityComparer, out actualValue));
            }

            actualValue = equalValue;
            return(false);
        }
Example #9
0
        /// <summary>
        /// Gets the <typeparamref name="TValue"/> with the specified key.
        /// </summary>
        public TValue this[TKey key]
        {
            get
            {
                Requires.NotNullAllowStructs(key, nameof(key));

                TValue value;
                if (this.TryGetValue(key, out value !))
                {
                    return(value);
                }

                throw new KeyNotFoundException(SR.Format(SR.Arg_KeyNotFoundWithKey, key.ToString()));
            }
        }
            /// <summary>
            /// Initializes a new instance of the <see cref="ImmutableSortedDictionary{TKey, TValue}.Node"/> class
            /// that is not yet frozen.
            /// </summary>
            /// <param name="key">The key.</param>
            /// <param name="value">The value.</param>
            /// <param name="left">The left.</param>
            /// <param name="right">The right.</param>
            /// <param name="frozen">Whether this node is prefrozen.</param>
            private Node(TKey key, TValue value, Node left, Node right, bool frozen = false)
            {
                Requires.NotNullAllowStructs(key, nameof(key));
                Requires.NotNull(left, nameof(left));
                Requires.NotNull(right, nameof(right));
                Debug.Assert(!frozen || (left._frozen && right._frozen));

                _key    = key;
                _value  = value;
                _left   = left;
                _right  = right;
                _height = checked ((byte)(1 + Math.Max(left._height, right._height)));
                _frozen = frozen;

                Debug.Assert(!this.IsEmpty);
            }
Example #11
0
        public bool TryGetValue(T equalValue, out T actualValue)
        {
            Requires.NotNullAllowStructs(equalValue, nameof(equalValue));

            Node searchResult = _root.Search(equalValue, _comparer);

            if (searchResult.IsEmpty)
            {
                actualValue = equalValue;
                return(false);
            }
            else
            {
                actualValue = searchResult.Key;
                return(true);
            }
        }
Example #12
0
        /// <summary>
        /// Performs the operation on a given data structure.
        /// </summary>
        private static MutationResult Add(TKey key, TValue value, KeyCollisionBehavior behavior, MutationInput origin)
        {
            Requires.NotNullAllowStructs(key, nameof(key));

            OperationResult result;
            int             hashCode  = origin.KeyComparer.GetHashCode(key);
            HashBucket      bucket    = origin.Root.GetValueOrDefault(hashCode);
            var             newBucket = bucket.Add(key, value, origin.KeyOnlyComparer, origin.ValueComparer, behavior, out result);

            if (result == OperationResult.NoChangeRequired)
            {
                return(new MutationResult(origin));
            }

            var newRoot = UpdateRoot(origin.Root, hashCode, newBucket, origin.HashBucketComparer);

            return(new MutationResult(newRoot, result == OperationResult.SizeChanged ? +1 : 0));
        }
Example #13
0
            internal bool TryGetKey(TKey equalKey, IComparer <TKey> keyComparer, out TKey actualKey)
            {
                Requires.NotNullAllowStructs(equalKey, nameof(equalKey));
                Requires.NotNull(keyComparer, nameof(keyComparer));

                var match = this.Search(equalKey, keyComparer);

                if (match.IsEmpty)
                {
                    actualKey = equalKey;
                    return(false);
                }
                else
                {
                    actualKey = match._key;
                    return(true);
                }
            }
Example #14
0
            internal bool TryGetValue(TKey key, IComparer <TKey> keyComparer, out TValue value)
            {
                Requires.NotNullAllowStructs(key, nameof(key));
                Requires.NotNull(keyComparer, nameof(keyComparer));

                var match = this.Search(key, keyComparer);

                if (match.IsEmpty)
                {
                    value = default(TValue);
                    return(false);
                }
                else
                {
                    value = match._value;
                    return(true);
                }
            }
Example #15
0
        /// <summary>
        /// Performs the set operation on a given data structure.
        /// </summary>
        private static MutationResult Add(T item, MutationInput origin)
        {
            Requires.NotNullAllowStructs(item, "item");

            OperationResult result;
            int             hashCode  = origin.EqualityComparer.GetHashCode(item);
            HashBucket      bucket    = origin.Root.GetValueOrDefault(hashCode);
            var             newBucket = bucket.Add(item, origin.EqualityComparer, out result);

            if (result == OperationResult.NoChangeRequired)
            {
                return(new MutationResult(origin.Root, 0));
            }

            var newRoot = UpdateRoot(origin.Root, hashCode, newBucket);

            Debug.Assert(result == OperationResult.SizeChanged);
            return(new MutationResult(newRoot, 1 /*result == OperationResult.SizeChanged ? 1 : 0*/));
        }
Example #16
0
            internal int IndexOf(T key, IComparer <T> comparer)
            {
                Requires.NotNullAllowStructs(key, nameof(key));
                Requires.NotNull(comparer, nameof(comparer));

                if (this.IsEmpty)
                {
                    return(-1);
                }
                else
                {
                    int compare = comparer.Compare(key, _key);
                    if (compare == 0)
                    {
                        return(_left.Count);
                    }
                    else if (compare > 0)
                    {
                        int  result  = _right.IndexOf(key, comparer);
                        bool missing = result < 0;
                        if (missing)
                        {
                            result = ~result;
                        }

                        result = _left.Count + 1 + result;
                        if (missing)
                        {
                            result = ~result;
                        }

                        return(result);
                    }
                    else
                    {
                        return(_left.IndexOf(key, comparer));
                    }
                }
            }
Example #17
0
            /// <summary>
            /// Adds the specified key to the tree.
            /// </summary>
            /// <param name="key">The key.</param>
            /// <param name="comparer">The comparer.</param>
            /// <param name="mutated">Receives a value indicating whether this node tree has mutated because of this operation.</param>
            /// <returns>The new tree.</returns>
            internal Node Add(T key, IComparer <T> comparer, out bool mutated)
            {
                Requires.NotNullAllowStructs(key, nameof(key));
                Requires.NotNull(comparer, nameof(comparer));

                if (this.IsEmpty)
                {
                    mutated = true;
                    return(new Node(key, this, this));
                }
                else
                {
                    Node result        = this;
                    int  compareResult = comparer.Compare(key, _key);
                    if (compareResult > 0)
                    {
                        var newRight = _right.Add(key, comparer, out mutated);
                        if (mutated)
                        {
                            result = this.Mutate(right: newRight);
                        }
                    }
                    else if (compareResult < 0)
                    {
                        var newLeft = _left.Add(key, comparer, out mutated);
                        if (mutated)
                        {
                            result = this.Mutate(left: newLeft);
                        }
                    }
                    else
                    {
                        mutated = false;
                        return(this);
                    }

                    return(mutated ? MakeBalanced(result) : result);
                }
            }
Example #18
0
        /// <summary>
        /// Performs the set operation on a given data structure.
        /// </summary>
        private static MutationResult Remove(T item, MutationInput origin)
        {
            Requires.NotNullAllowStructs(item, "item");

            var        result   = OperationResult.NoChangeRequired;
            int        hashCode = origin.EqualityComparer.GetHashCode(item);
            HashBucket bucket;
            var        newRoot = origin.Root;

            if (origin.Root.TryGetValue(hashCode, out bucket))
            {
                var newBucket = bucket.Remove(item, origin.EqualityComparer, out result);
                if (result == OperationResult.NoChangeRequired)
                {
                    return(new MutationResult(origin.Root, 0));
                }

                newRoot = UpdateRoot(origin.Root, hashCode, newBucket);
            }

            return(new MutationResult(newRoot, result == OperationResult.SizeChanged ? -1 : 0));
        }
Example #19
0
        /// <summary>
        /// Performs the operation on a given data structure.
        /// </summary>
        private static MutationResult AddRange(IEnumerable <KeyValuePair <TKey, TValue> > items, MutationInput origin, KeyCollisionBehavior collisionBehavior = KeyCollisionBehavior.ThrowIfValueDifferent)
        {
            Requires.NotNull(items, nameof(items));

            int countAdjustment = 0;
            var newRoot         = origin.Root;

            foreach (var pair in items)
            {
                Requires.NotNullAllowStructs(pair.Key, nameof(pair.Key));
                int             hashCode = origin.KeyComparer.GetHashCode(pair.Key);
                HashBucket      bucket   = newRoot.GetValueOrDefault(hashCode);
                OperationResult result;
                var             newBucket = bucket.Add(pair.Key, pair.Value, origin.KeyOnlyComparer, origin.ValueComparer, collisionBehavior, out result);
                newRoot = UpdateRoot(newRoot, hashCode, newBucket, origin.HashBucketComparer);
                if (result == OperationResult.SizeChanged)
                {
                    countAdjustment++;
                }
            }

            return(new MutationResult(newRoot, countAdjustment));
        }
Example #20
0
 /// <summary>
 /// See the <see cref="IImmutableDictionary{TKey, TValue}"/> interface.
 /// </summary>
 public bool TryGetKey(TKey equalKey, out TKey actualKey)
 {
     Requires.NotNullAllowStructs(equalKey, nameof(equalKey));
     return(_root.TryGetKey(equalKey, _keyComparer, out actualKey));
 }
Example #21
0
 public static bool Contains <TKey, TValue>(this IImmutableDictionary <TKey, TValue> map, TKey key, TValue value)
 {
     Requires.NotNull(map, "map");
     Requires.NotNullAllowStructs(key, "key");
     return(map.Contains(new KeyValuePair <TKey, TValue>(key, value)));
 }
 internal SecurePooledObject(T newValue)
 {
     Requires.NotNullAllowStructs(newValue, nameof(newValue));
     _value = newValue;
 }
Example #23
0
 /// <summary>
 /// See the <see cref="IImmutableSet&lt;T&gt;"/> interface.
 /// </summary>
 public bool Contains(T item)
 {
     Requires.NotNullAllowStructs(item, "item");
     return(Contains(item, this.Origin));
 }
Example #24
0
 /// <summary>
 /// See the <see cref="IImmutableDictionary{TKey, TValue}"/> interface.
 /// </summary>
 public bool TryGetKey(TKey equalKey, out TKey actualKey)
 {
     Requires.NotNullAllowStructs(equalKey, nameof(equalKey));
     return(TryGetKey(equalKey, this.Origin, out actualKey));
 }
Example #25
0
 internal bool ContainsKey(TKey key, IComparer <TKey> keyComparer)
 {
     Requires.NotNullAllowStructs(key, nameof(key));
     Requires.NotNull(keyComparer, nameof(keyComparer));
     return(!this.Search(key, keyComparer).IsEmpty);
 }
Example #26
0
 /// <summary>
 /// See the <see cref="IImmutableDictionary{TKey, TValue}"/> interface.
 /// </summary>
 public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
 {
     Requires.NotNullAllowStructs(key, nameof(key));
     return(TryGetValue(key, this.Origin, out value !));
 }
Example #27
0
 /// <summary>
 /// Determines whether the specified key contains key.
 /// </summary>
 /// <param name="key">The key.</param>
 /// <returns>
 ///   <c>true</c> if the specified key contains key; otherwise, <c>false</c>.
 /// </returns>
 public bool ContainsKey(TKey key)
 {
     Requires.NotNullAllowStructs(key, nameof(key));
     return(ContainsKey(key, this.Origin));
 }
Example #28
0
 /// <summary>
 /// See the <see cref="IImmutableDictionary{TKey, TValue}"/> interface.
 /// </summary>
 public bool ContainsKey(TKey key)
 {
     Requires.NotNullAllowStructs(key, nameof(key));
     return(_root.ContainsKey(key, _keyComparer));
 }
Example #29
0
            /// <summary>
            /// Returns a read-only reference to the value associated with the provided key.
            /// </summary>
            /// <exception cref="KeyNotFoundException">If the key is not present.</exception>
            public ref readonly TValue ValueRef(TKey key)
            {
                Requires.NotNullAllowStructs(key, nameof(key));

                return(ref _root.ValueRef(key, _keyComparer));
            }
Example #30
0
 /// <summary>
 /// See the <see cref="IImmutableDictionary{TKey, TValue}"/> interface.
 /// </summary>
 public bool TryGetValue(TKey key, out TValue value)
 {
     Requires.NotNullAllowStructs(key, nameof(key));
     return(_root.TryGetValue(key, _keyComparer, out value));
 }