mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 05:05:05 +00:00
feat: Initial commit of WeakKeyDictionary for keeping caches of items that can not invalidate themselves
This commit is contained in:
283
ICD.Common.Utils/Collections/WeakKeyDictionary.cs
Normal file
283
ICD.Common.Utils/Collections/WeakKeyDictionary.cs
Normal file
@@ -0,0 +1,283 @@
|
||||
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
|
||||
{
|
||||
RemoveCollectedEntries();
|
||||
return m_Dictionary.Count;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsReadOnly { get { return false; } }
|
||||
|
||||
public ICollection<TKey> Keys { get { throw new NotImplementedException(); } }
|
||||
|
||||
public ICollection<TValue> Values { get { throw new NotImplementedException(); } }
|
||||
|
||||
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
|
||||
|
||||
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Add(TKey key, TValue value)
|
||||
{
|
||||
if (key == null)
|
||||
throw new ArgumentNullException("key");
|
||||
|
||||
WeakKeyReference<TKey> weakKey = new WeakKeyReference<TKey>(key, m_Comparer);
|
||||
IcdConsole.PrintLine("Adding key {0} with hash code {1}", weakKey.Target, weakKey.HashCode);
|
||||
|
||||
m_Dictionary.Add(weakKey, value);
|
||||
|
||||
IcdConsole.PrintLine("Internal dict count is now {0}", m_Dictionary.Count);
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
IcdConsole.PrintLine("-------------------");
|
||||
foreach (var item in toRemove)
|
||||
IcdConsole.PrintLine("{0} - {1}", item, item.Target);
|
||||
IcdConsole.PrintLine("-------------------");
|
||||
|
||||
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>>.Contains(KeyValuePair<TKey, TValue> item)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IEnumerator
|
||||
|
||||
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user