This commit is contained in:
Jack Kanarish
2018-04-16 15:25:26 -04:00
9 changed files with 543 additions and 6 deletions

View File

@@ -0,0 +1,158 @@
using System;
using ICD.Common.Utils.Collections;
using NUnit.Framework;
namespace ICD.Common.Utils.Tests.Collections
{
[TestFixture]
public sealed class WeakKeyDictionaryTest
{
private sealed class TestClass
{
}
#region Properties
[Test]
public void CountTest()
{
#if DEBUG
Assert.Inconclusive();
return;
#endif
WeakKeyDictionary<TestClass, int> dict = new WeakKeyDictionary<TestClass, int>();
Assert.AreEqual(0, dict.Count);
TestClass instance = new TestClass();
dict.Add(instance, 0);
Assert.AreEqual(1, dict.Count, "Expected the added item to increase the dictionary size");
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
Assert.AreEqual(1, dict.Count, "Expected the dictionary to have one uncollected item");
// Need to actually USE the instance at some point AFTER the collection, otherwise it gets optimized out
// and is collected prematurely.
// ReSharper disable once ReturnValueOfPureMethodIsNotUsed
instance.ToString();
// Now clear the reference to make sure the instance gets collected.
// ReSharper disable once RedundantAssignment
instance = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
Assert.AreEqual(0, dict.Count, "Expected the dictionary to be empty after collecting");
}
[Test]
public void KeysTest()
{
Assert.Inconclusive();
}
[Test]
public void ValuesTest()
{
Assert.Inconclusive();
}
[Test]
public void IndexerTest()
{
Assert.Inconclusive();
}
#endregion
#region Methods
[Test]
public void AddTest()
{
Assert.Inconclusive();
}
[Test]
public void ContainsKeyTest()
{
Assert.Inconclusive();
}
[Test]
public void RemoveTest()
{
Assert.Inconclusive();
}
[Test]
public void TryGetValueTest()
{
Assert.Inconclusive();
}
[Test]
public void ClearTest()
{
Assert.Inconclusive();
}
[Test]
public void RemoveCollectedEntries()
{
#if DEBUG
Assert.Inconclusive();
return;
#endif
WeakKeyDictionary<TestClass, int> dict = new WeakKeyDictionary<TestClass, int>();
dict.RemoveCollectedEntries();
Assert.AreEqual(0, dict.Count, "Expected new dictionary to start empty");
TestClass instance = new TestClass();
dict.Add(instance, 0);
dict.RemoveCollectedEntries();
Assert.AreEqual(1, dict.Count, "Expected instance to add to the dictionary count");
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
dict.RemoveCollectedEntries();
Assert.AreEqual(1, dict.Count, "Expected instance to remain uncollected and stay in the dictionary");
// Need to actually USE the instance at some point AFTER the collection, otherwise it gets optimized out
// and is collected prematurely.
// ReSharper disable once ReturnValueOfPureMethodIsNotUsed
instance.ToString();
// Now clear the reference to make sure the instance gets collected.
// ReSharper disable once RedundantAssignment
instance = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
dict.RemoveCollectedEntries();
Assert.AreEqual(0, dict.Count, "Expected instance to be collected and removed from the dictionary");
}
[Test]
public void GetEnumeratorTest()
{
Assert.Inconclusive();
}
#endregion
}
}

View File

@@ -0,0 +1,271 @@
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
{
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)
{
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
}
}

View File

@@ -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>

View File

@@ -14,9 +14,6 @@ namespace ICD.Common.Utils.Extensions
/// <param name="sender"></param>
public static void Raise(this EventHandler extends, object sender)
{
if (sender == null)
throw new ArgumentNullException("sender");
if (extends != null)
extends(sender, EventArgs.Empty);
}
@@ -31,9 +28,6 @@ namespace ICD.Common.Utils.Extensions
public static void Raise<T>(this EventHandler<T> extends, object sender, T args)
where T : EventArgs
{
if (sender == null)
throw new ArgumentNullException("sender");
if (args == null)
throw new ArgumentNullException("args");

View File

@@ -75,6 +75,24 @@ namespace ICD.Common.Utils.Extensions
return Type.GetType(value);
}
/// <summary>
/// Gets the current value as an unsigned integer.
/// </summary>
/// <param name="extends"></param>
/// <returns></returns>
[PublicAPI]
public static uint GetValueAsUInt(this JsonReader extends)
{
if (extends == null)
throw new ArgumentNullException("extends");
if (extends.TokenType == JsonToken.Integer)
return (uint)(long)extends.Value;
string message = string.Format("Token {0} {1} is not {2}", extends.TokenType, extends.Value, JsonToken.Integer);
throw new InvalidCastException(message);
}
/// <summary>
/// Gets the current value as an integer.
/// </summary>

View File

@@ -10,6 +10,46 @@ namespace ICD.Common.Utils.Extensions
/// </summary>
public static class ListExtensions
{
/// <summary>
/// Adds the items into a sorted list.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="extends"></param>
/// <param name="items"></param>
[PublicAPI]
public static void AddSorted<T>(this List<T> extends, IEnumerable<T> items)
{
if (extends == null)
throw new ArgumentNullException("extends");
if (items == null)
throw new ArgumentNullException("items");
extends.AddSorted(items, Comparer<T>.Default);
}
/// <summary>
/// Adds the item into a sorted list.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="extends"></param>
/// <param name="items"></param>
/// <param name="comparer"></param>
[PublicAPI]
public static void AddSorted<T>(this List<T> extends, IEnumerable<T> items, IComparer<T> comparer)
{
if (extends == null)
throw new ArgumentNullException("extends");
if (items == null)
throw new ArgumentNullException("items");
if (comparer == null)
throw new ArgumentNullException("comparer");
items.ForEach(i => extends.AddSorted(i, comparer));
}
/// <summary>
/// Adds the item into a sorted list.
/// </summary>

View File

@@ -74,6 +74,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Attributes\AbstractIcdAttribute.cs" />
<Compile Include="Collections\WeakKeyDictionary.cs" />
<Compile Include="Comparers\PredicateComparer.cs" />
<Compile Include="ConsoleColor.cs" />
<Compile Include="EventArguments\BoolEventArgs.cs" />

View File

@@ -414,5 +414,43 @@ namespace ICD.Common.Utils
throw new InvalidCastException(message, e);
}
}
/// <summary>
/// Subscribes to the event on the given instance using the handler and callback method.
/// </summary>
/// <param name="instance">The instance with the event. Null for static types.</param>
/// <param name="eventInfo">The EventInfo for the event.</param>
/// <param name="handler">The instance with the callback MethodInfo. Null for static types.</param>
/// <param name="callback">The MethodInfo for the callback method.</param>
/// <returns></returns>
public static Delegate SubscribeEvent(object instance, EventInfo eventInfo, object handler, MethodInfo callback)
{
if (eventInfo == null)
throw new ArgumentNullException("eventInfo");
if (callback == null)
throw new ArgumentNullException("callback");
Delegate output = CreateDelegate(eventInfo.EventHandlerType, handler, callback);
eventInfo.AddEventHandler(instance, output);
return output;
}
/// <summary>
/// Unsubscribes from the event on the given instance.
/// </summary>
/// <param name="instance">The instance with the event. Null for static types.</param>
/// <param name="eventInfo">The EventInfo for the event.</param>
/// <param name="callback">The Delegate to be removed from the event.</param>
public static void UnsubscribeEvent(object instance, EventInfo eventInfo, Delegate callback)
{
if (eventInfo == null)
throw new ArgumentNullException("eventInfo");
if (callback == null)
throw new ArgumentNullException("callback");
eventInfo.RemoveEventHandler(instance, callback);
}
}
}