mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 21:24:58 +00:00
Merge remote-tracking branch 'origin/ConnectPro_v1.1' into ConnectPro_v1.2
# Conflicts: # CHANGELOG.md # ICD.Common.Utils/Extensions/DictionaryExtensions.cs # ICD.Common.Utils/Properties/AssemblyInfo.cs
This commit is contained in:
@@ -91,6 +91,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
|
||||||
|
|||||||
@@ -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>
|
||||||
@@ -129,11 +143,32 @@ namespace ICD.Common.Utils.Collections
|
|||||||
/// <param name="remove"></param>
|
/// <param name="remove"></param>
|
||||||
/// <param name="priority"></param>
|
/// <param name="priority"></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)
|
||||||
{
|
{
|
||||||
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 =
|
||||||
@@ -142,6 +177,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 +193,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>
|
||||||
|
|||||||
@@ -141,20 +141,50 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
/// <param name="key"></param>
|
/// <param name="key"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public static TValue GetOrAddNew<TKey, TValue>(this IDictionary<TKey, TValue> extends, TKey key)
|
public static TValue GetOrAddNew<TKey, TValue>([NotNull] this IDictionary<TKey, TValue> extends,
|
||||||
|
[NotNull] TKey key)
|
||||||
where TValue : new()
|
where TValue : new()
|
||||||
{
|
{
|
||||||
if (extends == null)
|
if (extends == null)
|
||||||
throw new ArgumentNullException("extends");
|
throw new ArgumentNullException("extends");
|
||||||
|
|
||||||
// ReSharper disable once CompareNonConstrainedGenericWithNull
|
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||||
if (key == null)
|
if (key == null)
|
||||||
|
// ReSharper restore CompareNonConstrainedGenericWithNull
|
||||||
throw new ArgumentNullException("key");
|
throw new ArgumentNullException("key");
|
||||||
|
|
||||||
|
return extends.GetOrAddNew(key, () => ReflectionUtils.CreateInstance<TValue>());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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;
|
TValue value;
|
||||||
if (!extends.TryGetValue(key, out value))
|
if (!extends.TryGetValue(key, out value))
|
||||||
{
|
{
|
||||||
value = ReflectionUtils.CreateInstance<TValue>();
|
value = valueFunc();
|
||||||
extends.Add(key, value);
|
extends.Add(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user