mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-14 04:05:04 +00:00
chore: merge util changes from dev needed for sharp bug backport to metlife 5.2
This commit is contained in:
@@ -67,6 +67,11 @@ namespace ICD.Common.Utils.Collections
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <summary>
|
||||
/// Returns a set containing all of this sets items plus all of the items in the given set.
|
||||
/// </summary>
|
||||
/// <param name="set"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public IcdHashSet<T> Union(IEnumerable<T> set)
|
||||
{
|
||||
@@ -80,6 +85,11 @@ namespace ICD.Common.Utils.Collections
|
||||
return unionSet;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a new set of this sets items exluding the items in the given set.
|
||||
/// </summary>
|
||||
/// <param name="set"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public IcdHashSet<T> Subtract(IEnumerable<T> set)
|
||||
{
|
||||
@@ -94,13 +104,11 @@ namespace ICD.Common.Utils.Collections
|
||||
return subtractSet;
|
||||
}
|
||||
|
||||
[PublicAPI]
|
||||
public bool IsSubsetOf(IcdHashSet<T> set)
|
||||
{
|
||||
IcdHashSet<T> setToCompare = set ?? NullSet;
|
||||
return this.All(setToCompare.Contains);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns all of the items that are common between this set and the given set.
|
||||
/// </summary>
|
||||
/// <param name="set"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public IcdHashSet<T> Intersection(IcdHashSet<T> set)
|
||||
{
|
||||
@@ -126,32 +134,74 @@ namespace ICD.Common.Utils.Collections
|
||||
[PublicAPI]
|
||||
public IcdHashSet<T> NonIntersection(IcdHashSet<T> set)
|
||||
{
|
||||
return Subtract(set).Union(set.Subtract(this));
|
||||
IcdHashSet<T> setToCompare = set ?? NullSet;
|
||||
|
||||
return Subtract(set).Union(setToCompare.Subtract(this));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the given set contains all of the items in this set.
|
||||
/// </summary>
|
||||
/// <param name="set"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public bool IsSubsetOf(IcdHashSet<T> set)
|
||||
{
|
||||
IcdHashSet<T> setToCompare = set ?? NullSet;
|
||||
|
||||
return this.All(setToCompare.Contains);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the given set contains all of the items in this set, and the sets are not equal.
|
||||
/// </summary>
|
||||
/// <param name="set"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public bool IsProperSubsetOf(IcdHashSet<T> set)
|
||||
{
|
||||
IcdHashSet<T> setToCompare = set ?? NullSet;
|
||||
|
||||
// Is a proper subset if A is a subset of B and A != B
|
||||
return (IsSubsetOf(setToCompare) && !setToCompare.IsSubsetOf(this));
|
||||
return IsSubsetOf(setToCompare) && !setToCompare.IsSubsetOf(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if this set contains all of the items in the given set.
|
||||
/// </summary>
|
||||
/// <param name="set"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public bool IsSupersetOf(IcdHashSet<T> set)
|
||||
{
|
||||
IcdHashSet<T> setToCompare = set ?? NullSet;
|
||||
|
||||
return setToCompare.IsSubsetOf(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if this set contains all of the items in the given set, and the sets are not equal.
|
||||
/// </summary>
|
||||
/// <param name="set"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public bool IsProperSupersetOf(IcdHashSet<T> set)
|
||||
{
|
||||
IcdHashSet<T> setToCompare = set ?? NullSet;
|
||||
|
||||
// B is a proper superset of A if B is a superset of A and A != B
|
||||
return (IsSupersetOf(setToCompare) && !setToCompare.IsSupersetOf(this));
|
||||
return IsSupersetOf(setToCompare) && !setToCompare.IsSupersetOf(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if this set contains all of the items in the given set, and vice versa.
|
||||
/// </summary>
|
||||
/// <param name="set"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public bool SetEquals(IcdHashSet<T> set)
|
||||
{
|
||||
IcdHashSet<T> setToCompare = set ?? NullSet;
|
||||
|
||||
return IsSupersetOf(setToCompare) && setToCompare.IsSupersetOf(this);
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -165,9 +215,9 @@ namespace ICD.Common.Utils.Collections
|
||||
/// <returns></returns>
|
||||
public bool Add(T item)
|
||||
{
|
||||
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||
if (item == null)
|
||||
// ReSharper restore CompareNonConstrainedGenericWithNull
|
||||
// ReSharper restore CompareNonConstrainedGenericWithNull
|
||||
throw new ArgumentNullException("item");
|
||||
|
||||
if (m_Dict.ContainsKey(item))
|
||||
|
||||
159
ICD.Common.Utils/Collections/IcdOrderedDictionary.cs
Normal file
159
ICD.Common.Utils/Collections/IcdOrderedDictionary.cs
Normal file
@@ -0,0 +1,159 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using ICD.Common.Utils.Extensions;
|
||||
|
||||
namespace ICD.Common.Utils.Collections
|
||||
{
|
||||
public sealed class IcdOrderedDictionary<TKey, TValue> : IDictionary<TKey, TValue>
|
||||
{
|
||||
private readonly List<TKey> m_OrderedKeys;
|
||||
private readonly Dictionary<TKey, TValue> m_Dictionary;
|
||||
private readonly IComparer<TKey> m_Comparer;
|
||||
|
||||
#region Properties
|
||||
|
||||
public int Count { get { return m_Dictionary.Count; } }
|
||||
|
||||
public bool IsReadOnly { get { return false; } }
|
||||
|
||||
public ICollection<TKey> Keys { get { return m_OrderedKeys; } }
|
||||
|
||||
public ICollection<TValue> Values
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_OrderedKeys.Select(k => m_Dictionary[k])
|
||||
.ToArray(Count);
|
||||
}
|
||||
}
|
||||
|
||||
public TValue this[TKey key]
|
||||
{
|
||||
get { return m_Dictionary[key]; }
|
||||
set
|
||||
{
|
||||
// ReSharper disable once CompareNonConstrainedGenericWithNull
|
||||
if (key == null)
|
||||
throw new ArgumentNullException("key");
|
||||
|
||||
if (!ContainsKey(key))
|
||||
m_OrderedKeys.AddSorted(key, m_Comparer);
|
||||
|
||||
m_Dictionary[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
public IcdOrderedDictionary()
|
||||
: this(Comparer<TKey>.Default)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
/// <param name="comparer"></param>
|
||||
public IcdOrderedDictionary(IComparer<TKey> comparer)
|
||||
{
|
||||
if (comparer == null)
|
||||
throw new ArgumentNullException("comparer");
|
||||
|
||||
m_Comparer = comparer;
|
||||
m_OrderedKeys = new List<TKey>();
|
||||
m_Dictionary = new Dictionary<TKey, TValue>();
|
||||
}
|
||||
|
||||
#region Methods
|
||||
|
||||
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
|
||||
{
|
||||
return m_OrderedKeys.Select(k => new KeyValuePair<TKey, TValue>(k, m_Dictionary[k]))
|
||||
.GetEnumerator();
|
||||
}
|
||||
|
||||
public void Add(TKey key, TValue value)
|
||||
{
|
||||
// ReSharper disable once CompareNonConstrainedGenericWithNull
|
||||
if (key == null)
|
||||
throw new ArgumentNullException("key");
|
||||
|
||||
if (m_Dictionary.ContainsKey(key))
|
||||
throw new ArgumentException("An item with the same key has already been added.", "key");
|
||||
|
||||
this[key] = value;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
m_OrderedKeys.Clear();
|
||||
m_Dictionary.Clear();
|
||||
}
|
||||
|
||||
public bool ContainsKey(TKey key)
|
||||
{
|
||||
return m_Dictionary.ContainsKey(key);
|
||||
}
|
||||
|
||||
public bool Remove(TKey key)
|
||||
{
|
||||
// ReSharper disable once CompareNonConstrainedGenericWithNull
|
||||
if (key == null)
|
||||
throw new ArgumentNullException("key");
|
||||
|
||||
if (!m_Dictionary.Remove(key))
|
||||
return false;
|
||||
|
||||
m_OrderedKeys.Remove(key);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TryGetValue(TKey key, out TValue value)
|
||||
{
|
||||
return m_Dictionary.TryGetValue(key, out value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
|
||||
{
|
||||
Add(item.Key, item.Value);
|
||||
}
|
||||
|
||||
bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item)
|
||||
{
|
||||
TValue value;
|
||||
return TryGetValue(item.Key, out value) &&
|
||||
EqualityComparer<TValue>.Default.Equals(value, item.Value);
|
||||
}
|
||||
|
||||
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int index)
|
||||
{
|
||||
foreach (KeyValuePair<TKey, TValue> kvp in this)
|
||||
{
|
||||
array.SetValue(kvp, index);
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
|
||||
{
|
||||
return (this as ICollection<KeyValuePair<TKey, TValue>>).Contains(item) && Remove(item.Key);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
244
ICD.Common.Utils/Collections/PriorityQueue.cs
Normal file
244
ICD.Common.Utils/Collections/PriorityQueue.cs
Normal file
@@ -0,0 +1,244 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using ICD.Common.Properties;
|
||||
using ICD.Common.Utils.Extensions;
|
||||
|
||||
namespace ICD.Common.Utils.Collections
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides a first-in first-out collection with enhanced insertion features.
|
||||
/// </summary>
|
||||
public sealed class PriorityQueue<T> : IEnumerable<T>, ICollection
|
||||
{
|
||||
private readonly IcdOrderedDictionary<int, List<T>> m_PriorityToQueue;
|
||||
private int m_Count;
|
||||
|
||||
#region Properties
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of items in the collection.
|
||||
/// </summary>
|
||||
public int Count { get { return m_Count; } }
|
||||
|
||||
/// <summary>
|
||||
/// Is the collection thread safe?
|
||||
/// </summary>
|
||||
public bool IsSynchronized { get { return false; } }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a reference for locking.
|
||||
/// </summary>
|
||||
public object SyncRoot { get { return this; } }
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
public PriorityQueue()
|
||||
{
|
||||
m_PriorityToQueue = new IcdOrderedDictionary<int, List<T>>();
|
||||
}
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <summary>
|
||||
/// Clears the collection.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public void Clear()
|
||||
{
|
||||
m_PriorityToQueue.Clear();
|
||||
m_Count = 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the item to the end of the queue.
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
[PublicAPI]
|
||||
public void Enqueue(T item)
|
||||
{
|
||||
Enqueue(item, int.MaxValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the item to the queue with the given priority.
|
||||
/// Lower values are dequeued first.
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="priority"></param>
|
||||
[PublicAPI]
|
||||
public void Enqueue(T item, int priority)
|
||||
{
|
||||
if (!m_PriorityToQueue.ContainsKey(priority))
|
||||
m_PriorityToQueue.Add(priority, new List<T>());
|
||||
|
||||
m_PriorityToQueue[priority].Add(item);
|
||||
m_Count++;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enqueues the item at the beginning of the queue.
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
[PublicAPI]
|
||||
public void EnqueueFirst(T item)
|
||||
{
|
||||
const int priority = int.MinValue;
|
||||
|
||||
if (!m_PriorityToQueue.ContainsKey(priority))
|
||||
m_PriorityToQueue.Add(priority, new List<T>());
|
||||
|
||||
m_PriorityToQueue[priority].Insert(0, item);
|
||||
m_Count++;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes any items in the queue matching the predicate.
|
||||
/// Inserts the given item in the position of the first removed item, or at the end of the queue.
|
||||
/// This is useful for reducing duplication, or replacing items with something more pertinant.
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="remove"></param>
|
||||
[PublicAPI]
|
||||
public void EnqueueRemove(T item, Func<T, bool> remove)
|
||||
{
|
||||
if (remove == null)
|
||||
throw new ArgumentNullException("remove");
|
||||
|
||||
EnqueueRemove(item, remove, int.MaxValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes any items in the queue matching the predicate.
|
||||
/// Inserts the given item in the position of the first removed item, or at the end of the queue.
|
||||
/// This is useful for reducing duplication, or replacing items with something more pertinant.
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="remove"></param>
|
||||
/// <param name="priority"></param>
|
||||
[PublicAPI]
|
||||
public void EnqueueRemove(T item, Func<T, bool> remove, int priority)
|
||||
{
|
||||
if (remove == null)
|
||||
throw new ArgumentNullException("remove");
|
||||
|
||||
bool inserted = false;
|
||||
|
||||
foreach (KeyValuePair<int, List<T>> kvp in m_PriorityToQueue)
|
||||
{
|
||||
int[] removeIndices =
|
||||
kvp.Value
|
||||
.FindIndices(v => remove(v))
|
||||
.Reverse()
|
||||
.ToArray();
|
||||
|
||||
if (removeIndices.Length == 0)
|
||||
continue;
|
||||
|
||||
foreach (int removeIndex in removeIndices)
|
||||
{
|
||||
kvp.Value.RemoveAt(removeIndex);
|
||||
m_Count--;
|
||||
}
|
||||
|
||||
if (!inserted)
|
||||
{
|
||||
int insertIndex = removeIndices[0];
|
||||
kvp.Value.Insert(insertIndex, item);
|
||||
m_Count++;
|
||||
|
||||
inserted = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!inserted)
|
||||
Enqueue(item, priority);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dequeues the first item with the lowest priority value.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public T Dequeue()
|
||||
{
|
||||
T output;
|
||||
if (TryDequeue(out output))
|
||||
return output;
|
||||
|
||||
throw new InvalidOperationException("The queue is empty.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to dequeue an item from the queue.
|
||||
/// </summary>
|
||||
/// <param name="output"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public bool TryDequeue(out T output)
|
||||
{
|
||||
output = default(T);
|
||||
|
||||
KeyValuePair<int, List<T>> kvp;
|
||||
if (!m_PriorityToQueue.TryFirst(out kvp))
|
||||
return false;
|
||||
|
||||
int priority = kvp.Key;
|
||||
List<T> queue = kvp.Value;
|
||||
|
||||
output = queue[0];
|
||||
queue.RemoveAt(0);
|
||||
|
||||
if (queue.Count == 0)
|
||||
m_PriorityToQueue.Remove(priority);
|
||||
|
||||
m_Count--;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets an enumerator for the items.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
return m_PriorityToQueue.Values
|
||||
.SelectMany(v => v)
|
||||
.GetEnumerator();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies the collection to the target array.
|
||||
/// </summary>
|
||||
/// <param name="array"></param>
|
||||
/// <param name="index"></param>
|
||||
public void CopyTo(Array array, int index)
|
||||
{
|
||||
foreach (T item in this)
|
||||
{
|
||||
array.SetValue(item, index);
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
/// <summary>
|
||||
/// Gets an enumerator for the items.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
273
ICD.Common.Utils/Collections/WeakKeyDictionary.cs
Normal file
273
ICD.Common.Utils/Collections/WeakKeyDictionary.cs
Normal file
@@ -0,0 +1,273 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using ICD.Common.Properties;
|
||||
using ICD.Common.Utils.Extensions;
|
||||
|
||||
namespace ICD.Common.Utils.Collections
|
||||
{
|
||||
public sealed class WeakKeyReference<T> : WeakReference
|
||||
{
|
||||
private readonly int m_HashCode;
|
||||
|
||||
public new T Target { get { return (T)base.Target; } }
|
||||
|
||||
public int HashCode { get { return m_HashCode; } }
|
||||
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
public WeakKeyReference(T key)
|
||||
: this(key, EqualityComparer<T>.Default)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="comparer"></param>
|
||||
public WeakKeyReference(T key, IEqualityComparer<T> comparer)
|
||||
: base(key)
|
||||
{
|
||||
if (comparer == null)
|
||||
throw new ArgumentNullException("comparer");
|
||||
|
||||
// Retain the object's hash code immediately so that even
|
||||
// if the target is GC'ed we will be able to find and
|
||||
// remove the dead weak reference.
|
||||
m_HashCode = comparer.GetHashCode(key);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class WeakKeyComparer<T> : IEqualityComparer<WeakKeyReference<T>>
|
||||
{
|
||||
private readonly IEqualityComparer<T> m_Comparer;
|
||||
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
/// <param name="comparer"></param>
|
||||
public WeakKeyComparer(IEqualityComparer<T> comparer)
|
||||
{
|
||||
if (comparer == null)
|
||||
comparer = EqualityComparer<T>.Default;
|
||||
|
||||
m_Comparer = comparer;
|
||||
}
|
||||
|
||||
public int GetHashCode(WeakKeyReference<T> weakKey)
|
||||
{
|
||||
return weakKey.HashCode;
|
||||
}
|
||||
|
||||
// Note: There are actually 9 cases to handle here.
|
||||
//
|
||||
// Let Wa = Alive Weak Reference
|
||||
// Let Wd = Dead Weak Reference
|
||||
// Let S = Strong Reference
|
||||
//
|
||||
// x | y | Equals(x,y)
|
||||
// -------------------------------------------------
|
||||
// Wa | Wa | comparer.Equals(x.Target, y.Target)
|
||||
// Wa | Wd | false
|
||||
// Wa | S | comparer.Equals(x.Target, y)
|
||||
// Wd | Wa | false
|
||||
// Wd | Wd | x == y
|
||||
// Wd | S | false
|
||||
// S | Wa | comparer.Equals(x, y.Target)
|
||||
// S | Wd | false
|
||||
// S | S | comparer.Equals(x, y)
|
||||
// -------------------------------------------------
|
||||
public bool Equals(WeakKeyReference<T> x, WeakKeyReference<T> y)
|
||||
{
|
||||
bool xIsDead;
|
||||
bool yIsDead;
|
||||
|
||||
T first = GetTarget(x, out xIsDead);
|
||||
T second = GetTarget(y, out yIsDead);
|
||||
|
||||
if (xIsDead)
|
||||
return yIsDead && x == y;
|
||||
|
||||
return !yIsDead && m_Comparer.Equals(first, second);
|
||||
}
|
||||
|
||||
private static T GetTarget(WeakKeyReference<T> obj, out bool isDead)
|
||||
{
|
||||
T target = obj.Target;
|
||||
isDead = !obj.IsAlive;
|
||||
return target;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// WeakDictionary keeps weak references to keys and drops key/value pairs once the key is garbage collected.
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey"></typeparam>
|
||||
/// <typeparam name="TValue"></typeparam>
|
||||
public sealed class WeakKeyDictionary<TKey, TValue> : IDictionary<TKey, TValue>
|
||||
{
|
||||
private readonly Dictionary<WeakKeyReference<TKey>, TValue> m_Dictionary;
|
||||
private readonly IEqualityComparer<TKey> m_Comparer;
|
||||
|
||||
#region Properties
|
||||
|
||||
public int Count { get { return GetAliveKvps().Count(); } }
|
||||
|
||||
public bool IsReadOnly { get { return false; } }
|
||||
|
||||
public ICollection<TKey> Keys { get { return GetAliveKvps().Select(kvp => kvp.Key).ToArray(); } }
|
||||
|
||||
public ICollection<TValue> Values { get { return GetAliveKvps().Select(kvp => kvp.Value).ToArray(); } }
|
||||
|
||||
public TValue this[TKey key]
|
||||
{
|
||||
get
|
||||
{
|
||||
TValue output;
|
||||
if (TryGetValue(key, out output))
|
||||
return output;
|
||||
|
||||
throw new KeyNotFoundException();
|
||||
}
|
||||
set
|
||||
{
|
||||
// ReSharper disable once CompareNonConstrainedGenericWithNull
|
||||
if (key == null)
|
||||
throw new ArgumentNullException("key");
|
||||
|
||||
WeakKeyReference<TKey> weakKey = new WeakKeyReference<TKey>(key, m_Comparer);
|
||||
m_Dictionary[weakKey] = value;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public WeakKeyDictionary()
|
||||
: this(0)
|
||||
{
|
||||
}
|
||||
|
||||
public WeakKeyDictionary(int capacity)
|
||||
: this(capacity, EqualityComparer<TKey>.Default)
|
||||
{
|
||||
}
|
||||
|
||||
public WeakKeyDictionary(IEqualityComparer<TKey> comparer)
|
||||
: this(0, comparer)
|
||||
{
|
||||
}
|
||||
|
||||
public WeakKeyDictionary(int capacity, IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
m_Comparer = comparer;
|
||||
|
||||
WeakKeyComparer<TKey> keyComparer = new WeakKeyComparer<TKey>(m_Comparer);
|
||||
m_Dictionary = new Dictionary<WeakKeyReference<TKey>, TValue>(capacity, keyComparer);
|
||||
}
|
||||
|
||||
#region Methods
|
||||
|
||||
public void Add(TKey key, TValue value)
|
||||
{
|
||||
// ReSharper disable once CompareNonConstrainedGenericWithNull
|
||||
if (key == null)
|
||||
throw new ArgumentNullException("key");
|
||||
|
||||
WeakKeyReference<TKey> weakKey = new WeakKeyReference<TKey>(key, m_Comparer);
|
||||
m_Dictionary.Add(weakKey, value);
|
||||
}
|
||||
|
||||
public bool ContainsKey(TKey key)
|
||||
{
|
||||
TValue unused;
|
||||
return TryGetValue(key, out unused);
|
||||
}
|
||||
|
||||
public bool Remove(TKey key)
|
||||
{
|
||||
return m_Dictionary.Remove(new WeakKeyReference<TKey>(key, m_Comparer));
|
||||
}
|
||||
|
||||
public bool TryGetValue(TKey key, out TValue value)
|
||||
{
|
||||
return m_Dictionary.TryGetValue(new WeakKeyReference<TKey>(key, m_Comparer), out value);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
m_Dictionary.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the left-over weak references for entries in the dictionary
|
||||
/// whose key or value has already been reclaimed by the garbage
|
||||
/// collector. This will reduce the dictionary's Count by the number
|
||||
/// of dead key-value pairs that were eliminated.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public void RemoveCollectedEntries()
|
||||
{
|
||||
IEnumerable<WeakKeyReference<TKey>> toRemove =
|
||||
m_Dictionary.Select(pair => pair.Key)
|
||||
.Where(weakKey => !weakKey.IsAlive)
|
||||
.ToArray();
|
||||
|
||||
m_Dictionary.RemoveAll(toRemove);
|
||||
}
|
||||
|
||||
#region ICollection
|
||||
|
||||
void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
|
||||
{
|
||||
Add(item.Key, item.Value);
|
||||
}
|
||||
|
||||
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
|
||||
{
|
||||
return Remove(item.Key);
|
||||
}
|
||||
|
||||
bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item)
|
||||
{
|
||||
return ContainsKey(item.Key);
|
||||
}
|
||||
|
||||
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
|
||||
{
|
||||
GetAliveKvps().CopyTo(array, arrayIndex);
|
||||
}
|
||||
|
||||
private IEnumerable<KeyValuePair<TKey, TValue>> GetAliveKvps()
|
||||
{
|
||||
foreach (KeyValuePair<WeakKeyReference<TKey>, TValue> kvp in m_Dictionary)
|
||||
{
|
||||
WeakKeyReference<TKey> weakKey = kvp.Key;
|
||||
TKey key = weakKey.Target;
|
||||
if (weakKey.IsAlive)
|
||||
yield return new KeyValuePair<TKey, TValue>(key, kvp.Value);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IEnumerator
|
||||
|
||||
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
|
||||
{
|
||||
return GetAliveKvps().GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -578,6 +578,23 @@ namespace ICD.Common.Utils.Extensions
|
||||
return extends.Where(i => !comparer.Equals(item, i));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies all the elements of the current one-dimensional array to the specified one-dimensional array
|
||||
/// starting at the specified destination array index. The index is specified as a 32-bit integer.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="array"></param>
|
||||
/// <param name="index"></param>
|
||||
public static void CopyTo<T>(this IEnumerable<T> extends, T[] array, int index)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
ICollection<T> collection = extends as ICollection<T> ?? extends.ToArray();
|
||||
collection.CopyTo(array, index);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the sequence as a IcdHashSet.
|
||||
/// </summary>
|
||||
@@ -801,6 +818,17 @@ namespace ICD.Common.Utils.Extensions
|
||||
return output;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps this object instance into an IEnumerable consisting of a single item.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"> Type of the object. </typeparam>
|
||||
/// <param name="item"> The instance that will be wrapped. </param>
|
||||
/// <returns> An IEnumerable<T> consisting of a single item. </returns>
|
||||
public static IEnumerable<T> Yield<T>(this T item)
|
||||
{
|
||||
yield return item;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Given a sequence [A, B, C] returns a sequence [[A, B], [B, C]]
|
||||
/// </summary>
|
||||
@@ -1057,6 +1085,49 @@ namespace ICD.Common.Utils.Extensions
|
||||
}
|
||||
}
|
||||
|
||||
// since S# can't do anonymous types
|
||||
private struct TryParseStruct<T>
|
||||
{
|
||||
public readonly T value;
|
||||
public readonly bool isParsed;
|
||||
|
||||
public TryParseStruct(T value, bool isParsed)
|
||||
{
|
||||
this.value = value;
|
||||
this.isParsed = isParsed;
|
||||
}
|
||||
}
|
||||
|
||||
// since Func<...,T> can't specify `out` parameters
|
||||
public delegate bool TryParseDelegate<T>(string input, out T output);
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to parse each value of the enumerable,
|
||||
/// throwing away the values that don't parse correctly.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">type to parse to</typeparam>
|
||||
/// <param name="extends">enumerable of strings to parse</param>
|
||||
/// <param name="tryParseFunc">TryParse function for given type</param>
|
||||
/// <returns>enumerable of successfully parsed values</returns>
|
||||
public static IEnumerable<T> TryParseSkipFailures<T>(this IEnumerable<string> extends,
|
||||
TryParseDelegate<T> tryParseFunc)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
if (tryParseFunc == null)
|
||||
throw new ArgumentNullException("tryParseFunc");
|
||||
|
||||
return extends.Select(str =>
|
||||
{
|
||||
T value;
|
||||
bool isParsed = tryParseFunc(str, out value);
|
||||
return new TryParseStruct<T>(value, isParsed);
|
||||
})
|
||||
.Where(v => v.isParsed)
|
||||
.Select(v => v.value);
|
||||
}
|
||||
|
||||
#if SIMPLSHARP
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -74,6 +74,9 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Attributes\AbstractIcdAttribute.cs" />
|
||||
<Compile Include="Collections\IcdOrderedDictionary.cs" />
|
||||
<Compile Include="Collections\PriorityQueue.cs" />
|
||||
<Compile Include="Collections\WeakKeyDictionary.cs" />
|
||||
<Compile Include="Comparers\PredicateComparer.cs" />
|
||||
<Compile Include="ConsoleColor.cs" />
|
||||
<Compile Include="EventArguments\BoolEventArgs.cs" />
|
||||
|
||||
Reference in New Issue
Block a user