From 3e780a2cba114704d5407f3636eae73bff2623e3 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Wed, 11 Apr 2018 13:09:20 -0400 Subject: [PATCH] feature: Implementing remaining WeakKeyDictionary members --- .../Collections/WeakKeyDictionary.cs | 46 ++++++++----------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/ICD.Common.Utils/Collections/WeakKeyDictionary.cs b/ICD.Common.Utils/Collections/WeakKeyDictionary.cs index eac0a83..8801c2a 100644 --- a/ICD.Common.Utils/Collections/WeakKeyDictionary.cs +++ b/ICD.Common.Utils/Collections/WeakKeyDictionary.cs @@ -116,20 +116,13 @@ namespace ICD.Common.Utils.Collections #region Properties - public int Count - { - get - { - RemoveCollectedEntries(); - return m_Dictionary.Count; - } - } + public int Count { get { return GetAliveKvps().Count(); } } public bool IsReadOnly { get { return false; } } - public ICollection Keys { get { throw new NotImplementedException(); } } + public ICollection Keys { get { return GetAliveKvps().Select(kvp => kvp.Key).ToArray(); } } - public ICollection Values { get { throw new NotImplementedException(); } } + public ICollection Values { get { return GetAliveKvps().Select(kvp => kvp.Value).ToArray(); } } public TValue this[TKey key] { @@ -178,22 +171,13 @@ namespace ICD.Common.Utils.Collections #region Methods - bool ICollection>.Remove(KeyValuePair item) - { - throw new NotImplementedException(); - } - public void Add(TKey key, TValue value) { if (key == null) throw new ArgumentNullException("key"); WeakKeyReference weakKey = new WeakKeyReference(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) @@ -241,21 +225,22 @@ namespace ICD.Common.Utils.Collections Add(item.Key, item.Value); } + bool ICollection>.Remove(KeyValuePair item) + { + return Remove(item.Key); + } + bool ICollection>.Contains(KeyValuePair item) { - throw new NotImplementedException(); + return ContainsKey(item.Key); } void ICollection>.CopyTo(KeyValuePair[] array, int arrayIndex) { - throw new NotImplementedException(); + GetAliveKvps().CopyTo(array, arrayIndex); } - #endregion - - #region IEnumerator - - public IEnumerator> GetEnumerator() + private IEnumerable> GetAliveKvps() { foreach (KeyValuePair, TValue> kvp in m_Dictionary) { @@ -266,6 +251,15 @@ namespace ICD.Common.Utils.Collections } } + #endregion + + #region IEnumerator + + public IEnumerator> GetEnumerator() + { + return GetAliveKvps().GetEnumerator(); + } + IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator();