mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 13:15:07 +00:00
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:
committed by
Chris Cameron
parent
55bf458a2b
commit
ac4c0eccc9
@@ -84,6 +84,20 @@ namespace ICD.Common.Utils.Collections
|
|||||||
m_Count++;
|
m_Count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds the item to the queue with the given priority at the given index.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="item"></param>
|
||||||
|
/// <param name="priority"></param>
|
||||||
|
/// <param name="position"></param>
|
||||||
|
[PublicAPI]
|
||||||
|
public void Enqueue([CanBeNull] T item, int priority, int position)
|
||||||
|
{
|
||||||
|
m_PriorityToQueue.GetOrAddNew(priority, ()=> new List<T>())
|
||||||
|
.Insert(position, item);
|
||||||
|
m_Count++;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Enqueues the item at the beginning of the queue.
|
/// Enqueues the item at the beginning of the queue.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -117,7 +131,7 @@ namespace ICD.Common.Utils.Collections
|
|||||||
if (remove == null)
|
if (remove == null)
|
||||||
throw new ArgumentNullException("remove");
|
throw new ArgumentNullException("remove");
|
||||||
|
|
||||||
EnqueueRemove(item, remove, int.MaxValue);
|
EnqueueRemove(item, remove, int.MaxValue, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -128,12 +142,16 @@ namespace ICD.Common.Utils.Collections
|
|||||||
/// <param name="item"></param>
|
/// <param name="item"></param>
|
||||||
/// <param name="remove"></param>
|
/// <param name="remove"></param>
|
||||||
/// <param name="priority"></param>
|
/// <param name="priority"></param>
|
||||||
|
/// <param name="deDuplicateToEndOfQueue"></param>
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public void EnqueueRemove(T item, Func<T, bool> remove, int priority)
|
public void EnqueueRemove([CanBeNull] T item, [NotNull] Func<T, bool> remove, int priority, bool deDuplicateToEndOfQueue)
|
||||||
{
|
{
|
||||||
if (remove == null)
|
if (remove == null)
|
||||||
throw new ArgumentNullException("remove");
|
throw new ArgumentNullException("remove");
|
||||||
|
|
||||||
|
int lowestMatchingPriority = int.MaxValue;
|
||||||
|
int? firstMatchingIndex = null;
|
||||||
|
|
||||||
foreach (KeyValuePair<int, List<T>> kvp in m_PriorityToQueue.ToArray())
|
foreach (KeyValuePair<int, List<T>> kvp in m_PriorityToQueue.ToArray())
|
||||||
{
|
{
|
||||||
int[] removeIndices =
|
int[] removeIndices =
|
||||||
@@ -142,6 +160,12 @@ namespace ICD.Common.Utils.Collections
|
|||||||
.Reverse()
|
.Reverse()
|
||||||
.ToArray();
|
.ToArray();
|
||||||
|
|
||||||
|
if (removeIndices.Any() && kvp.Key < lowestMatchingPriority )
|
||||||
|
{
|
||||||
|
lowestMatchingPriority = kvp.Key;
|
||||||
|
firstMatchingIndex = removeIndices.Last();
|
||||||
|
}
|
||||||
|
|
||||||
foreach (int removeIndex in removeIndices)
|
foreach (int removeIndex in removeIndices)
|
||||||
{
|
{
|
||||||
kvp.Value.RemoveAt(removeIndex);
|
kvp.Value.RemoveAt(removeIndex);
|
||||||
@@ -152,7 +176,16 @@ namespace ICD.Common.Utils.Collections
|
|||||||
m_PriorityToQueue.Remove(kvp.Key);
|
m_PriorityToQueue.Remove(kvp.Key);
|
||||||
}
|
}
|
||||||
|
|
||||||
Enqueue(item, priority);
|
|
||||||
|
if(deDuplicateToEndOfQueue)
|
||||||
|
Enqueue(item, priority);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(firstMatchingIndex == null)
|
||||||
|
Enqueue(item, lowestMatchingPriority);
|
||||||
|
else
|
||||||
|
Enqueue(item, lowestMatchingPriority, firstMatchingIndex.Value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -130,6 +130,41 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
return value;
|
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>
|
/// <summary>
|
||||||
/// Gets a key for the given value.
|
/// Gets a key for the given value.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user