Esempio n. 1
0
        /// <summary>
        /// Performs the set operation on a given data structure.
        /// </summary>
        private static MutationResult Add(T item, MutationInput origin)
        {
            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, origin.HashBucketEqualityComparer, newBucket);

            Debug.Assert(result == OperationResult.SizeChanged);
            return(new MutationResult(newRoot, 1 /*result == OperationResult.SizeChanged ? 1 : 0*/));
        }
Esempio n. 2
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));
        }
Esempio n. 3
0
        /// <summary>
        /// Performs the set operation on a given data structure.
        /// </summary>
        private static MutationResult Union(IEnumerable <T> other, MutationInput origin)
        {
            Requires.NotNull(other, "other");

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

            foreach (var item in other.GetEnumerableDisposable <T, Enumerator>())
            {
                int             hashCode = origin.EqualityComparer.GetHashCode(item);
                HashBucket      bucket   = newRoot.GetValueOrDefault(hashCode);
                OperationResult result;
                var             newBucket = bucket.Add(item, origin.EqualityComparer, out result);
                if (result == OperationResult.SizeChanged)
                {
                    newRoot = UpdateRoot(newRoot, hashCode, newBucket);
                    count++;
                }
            }

            return(new MutationResult(newRoot, count));
        }
Esempio n. 4
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)
            {
                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));
        }