Merge remote-tracking branch 'origin/ConnectPro_v1.4' into ConnectPro_v1.5

This commit is contained in:
Chris Cameron
2020-02-03 16:31:03 -05:00
3 changed files with 62 additions and 8 deletions

View File

@@ -149,6 +149,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Better VC-4 support for IcdConsole - Better VC-4 support for IcdConsole
- JSON refactoring for simpler deserialization - JSON refactoring for simpler deserialization
## [8.8.0] - 2020-01-23
### Added
- Added an overload to PriorityQueue for determing the de-duplication behaviour
## [8.7.2] - 2019-10-29 ## [8.7.2] - 2019-10-29
### Changed ### Changed
- Fixed a bug with PriorityQueue de-duplication where a new command would be inserted in the wrong position - Fixed a bug with PriorityQueue de-duplication where a new command would be inserted in the wrong position

View File

@@ -79,6 +79,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>
@@ -121,6 +135,27 @@ namespace ICD.Common.Utils.Collections
if (remove == null) if (remove == null)
throw new ArgumentNullException("remove"); throw new ArgumentNullException("remove");
EnqueueRemove(item, remove, priority, false);
}
/// <summary>
/// Removes any items in the queue matching the predicate.
/// Appends the given item at the end of the given priority level.
/// This is useful for reducing duplication, or replacing items with something more pertinent.
/// </summary>
/// <param name="item"></param>
/// <param name="remove"></param>
/// <param name="priority"></param>
/// <param name="deDuplicateToEndOfQueue"></param>
[PublicAPI]
public void EnqueueRemove([CanBeNull] T item, [NotNull] Func<T, bool> remove, int priority, bool deDuplicateToEndOfQueue)
{
if (remove == null)
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 =
@@ -129,6 +164,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);
@@ -139,7 +180,16 @@ namespace ICD.Common.Utils.Collections
m_PriorityToQueue.Remove(kvp.Key); m_PriorityToQueue.Remove(kvp.Key);
} }
if(deDuplicateToEndOfQueue)
Enqueue(item, priority); Enqueue(item, priority);
else
{
if(firstMatchingIndex == null)
Enqueue(item, lowestMatchingPriority);
else
Enqueue(item, lowestMatchingPriority, firstMatchingIndex.Value);
}
} }
/// <summary> /// <summary>