Exemplo n.º 1
0
            public static Dictionary <K, V> merge <K, V>(Dictionary <K, V> left, Dictionary <K, V> right, bool inplace = true, DictionaryMergePicker <K, V> picker = null)
            {
                if (picker == null)
                {
                    picker = delegate(K key, V leftValue, V rightValue) { return(rightValue); };
                }
                Dictionary <K, V> destination;

                if (!inplace)
                {
                    destination = new Dictionary <K, V>();
                    foreach (KeyValuePair <K, V> item in left)
                    {
                        destination.Add(item.Key, item.Value);
                    }
                }
                else
                {
                    destination = left;
                }
                foreach (KeyValuePair <K, V> item in right)
                {
                    destination[item.Key] = item.Value;
                }
                return(destination);
            }
Exemplo n.º 2
0
            /// <summary>
            ///   Merges two dictionaries. Merging implies having a dictionary with all the keys from the left and the right dictionaries. If they have the same
            ///     key, a
            /// </summary>
            /// <typeparam name="K">A dictionary key.</typeparam>
            /// <typeparam name="V">A dictioanry value.</typeparam>
            /// <param name="left">The left side dictionary.</param>
            /// <param name="right">The right side dictionary.</param>
            /// <param name="inPlace">If <c>true</c> the dictionary being merged is the left. Otherwise, it is a new dictionary having a merge of left and right.</param>
            /// <param name="picker">The merge function. If absent, a function taking the value from the right side will be used.</param>
            /// <returns>The merged dictionary. Will be a new one if <paramref name="inPlace"/> is false - otherwise, the left dictionary will be merged and returned</returns>
            public static Dictionary <K, V> Merge <K, V>(Dictionary <K, V> left, Dictionary <K, V> right, bool inPlace = true, DictionaryMergePicker <K, V> picker = null)
            {
                if (picker == null)
                {
                    picker = delegate(K key, V leftValue, V rightValue) { return(rightValue); };
                }
                Dictionary <K, V> destination = inPlace ? left : new Dictionary <K, V>(left);

                foreach (KeyValuePair <K, V> item in right)
                {
                    if (destination.ContainsKey(item.Key))
                    {
                        destination[item.Key] = picker(item.Key, destination[item.Key], item.Value);
                    }
                    else
                    {
                        destination[item.Key] = item.Value;
                    }
                }
                return(destination);
            }