feat: add ability to select when de-duplication should queue at the end of the queue, or at the first occurance

# Conflicts:
#	ICD.Common.Utils/Collections/PriorityQueue.cs
This commit is contained in:
Jack Kanarish
2020-01-17 17:01:31 -05:00
committed by Chris Cameron
parent 55bf458a2b
commit ac4c0eccc9
2 changed files with 71 additions and 3 deletions

View File

@@ -130,6 +130,41 @@ namespace ICD.Common.Utils.Extensions
return value;
}
/// <summary>
/// If the key is present in the dictionary return the value, otherwise add a new value to the dictionary and return it.
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
/// <param name="extends"></param>
/// <param name="key"></param>
/// <param name="valueFunc"></param>
/// <returns></returns>
[PublicAPI]
public static TValue GetOrAddNew<TKey, TValue>([NotNull] this IDictionary<TKey, TValue> extends,
[NotNull] TKey key,
[NotNull] Func<TValue> valueFunc)
{
if (extends == null)
throw new ArgumentNullException("extends");
// ReSharper disable CompareNonConstrainedGenericWithNull
if (key == null)
// ReSharper restore CompareNonConstrainedGenericWithNull
throw new ArgumentNullException("key");
if (valueFunc == null)
throw new ArgumentNullException("valueFunc");
TValue value;
if (!extends.TryGetValue(key, out value))
{
value = valueFunc();
extends.Add(key, value);
}
return value;
}
/// <summary>
/// Gets a key for the given value.
/// </summary>