mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 21:24:58 +00:00
Dictionary.Update extension method returns a bool for change state
This commit is contained in:
@@ -168,7 +168,8 @@ namespace ICD.Common.Utils.Tests.Extensions
|
|||||||
{3, 30}
|
{3, 30}
|
||||||
};
|
};
|
||||||
|
|
||||||
a.Update(b);
|
Assert.IsTrue(a.Update(b));
|
||||||
|
Assert.IsFalse(a.Update(b));
|
||||||
|
|
||||||
Assert.AreEqual(3, a.Count);
|
Assert.AreEqual(3, a.Count);
|
||||||
Assert.AreEqual(10, a[1]);
|
Assert.AreEqual(10, a[1]);
|
||||||
|
|||||||
@@ -191,8 +191,9 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
/// <typeparam name="TValue"></typeparam>
|
/// <typeparam name="TValue"></typeparam>
|
||||||
/// <param name="extends"></param>
|
/// <param name="extends"></param>
|
||||||
/// <param name="other"></param>
|
/// <param name="other"></param>
|
||||||
|
/// <returns></returns>
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public static void Update<TKey, TValue>(this IDictionary<TKey, TValue> extends, IDictionary<TKey, TValue> other)
|
public static bool Update<TKey, TValue>(this IDictionary<TKey, TValue> extends, IDictionary<TKey, TValue> other)
|
||||||
{
|
{
|
||||||
if (extends == null)
|
if (extends == null)
|
||||||
throw new ArgumentNullException("extends");
|
throw new ArgumentNullException("extends");
|
||||||
@@ -200,8 +201,40 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
if (other == null)
|
if (other == null)
|
||||||
throw new ArgumentNullException("other");
|
throw new ArgumentNullException("other");
|
||||||
|
|
||||||
|
return extends.Update(other, EqualityComparer<TValue>.Default);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Updates the dictionary with items from the other dictionary.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TKey"></typeparam>
|
||||||
|
/// <typeparam name="TValue"></typeparam>
|
||||||
|
/// <param name="extends"></param>
|
||||||
|
/// <param name="other"></param>
|
||||||
|
/// <param name="comparer"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[PublicAPI]
|
||||||
|
public static bool Update<TKey, TValue>(this IDictionary<TKey, TValue> extends, IDictionary<TKey, TValue> other,
|
||||||
|
IEqualityComparer<TValue> comparer)
|
||||||
|
{
|
||||||
|
if (extends == null)
|
||||||
|
throw new ArgumentNullException("extends");
|
||||||
|
|
||||||
|
if (other == null)
|
||||||
|
throw new ArgumentNullException("other");
|
||||||
|
|
||||||
|
bool change = false;
|
||||||
|
|
||||||
foreach (KeyValuePair<TKey, TValue> pair in other)
|
foreach (KeyValuePair<TKey, TValue> pair in other)
|
||||||
|
{
|
||||||
|
if (extends.ContainsKey(pair.Key) && comparer.Equals(pair.Value, extends[pair.Key]))
|
||||||
|
continue;
|
||||||
|
|
||||||
extends[pair.Key] = pair.Value;
|
extends[pair.Key] = pair.Value;
|
||||||
|
change = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return change;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user