mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 13:15:07 +00:00
perf: IcdOrderedDictionary maintains a collection of values ordered by key
This commit is contained in:
@@ -9,6 +9,7 @@ namespace ICD.Common.Utils.Collections
|
|||||||
public sealed class IcdOrderedDictionary<TKey, TValue> : IDictionary<TKey, TValue>
|
public sealed class IcdOrderedDictionary<TKey, TValue> : IDictionary<TKey, TValue>
|
||||||
{
|
{
|
||||||
private readonly List<TKey> m_OrderedKeys;
|
private readonly List<TKey> m_OrderedKeys;
|
||||||
|
private readonly List<TValue> m_ValuesOrderedByKey;
|
||||||
private readonly Dictionary<TKey, TValue> m_Dictionary;
|
private readonly Dictionary<TKey, TValue> m_Dictionary;
|
||||||
private readonly IComparer<TKey> m_Comparer;
|
private readonly IComparer<TKey> m_Comparer;
|
||||||
|
|
||||||
@@ -20,14 +21,7 @@ namespace ICD.Common.Utils.Collections
|
|||||||
|
|
||||||
public ICollection<TKey> Keys { get { return m_OrderedKeys; } }
|
public ICollection<TKey> Keys { get { return m_OrderedKeys; } }
|
||||||
|
|
||||||
public ICollection<TValue> Values
|
public ICollection<TValue> Values { get { return m_ValuesOrderedByKey; } }
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return m_OrderedKeys.Select(k => m_Dictionary[k])
|
|
||||||
.ToArray(m_OrderedKeys.Count);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public TValue this[TKey key]
|
public TValue this[TKey key]
|
||||||
{
|
{
|
||||||
@@ -39,7 +33,10 @@ namespace ICD.Common.Utils.Collections
|
|||||||
throw new ArgumentNullException("key");
|
throw new ArgumentNullException("key");
|
||||||
|
|
||||||
if (!ContainsKey(key))
|
if (!ContainsKey(key))
|
||||||
m_OrderedKeys.AddSorted(key, m_Comparer);
|
{
|
||||||
|
int index = m_OrderedKeys.AddSorted(key, m_Comparer);
|
||||||
|
m_ValuesOrderedByKey.Insert(index, value);
|
||||||
|
}
|
||||||
|
|
||||||
m_Dictionary[key] = value;
|
m_Dictionary[key] = value;
|
||||||
}
|
}
|
||||||
@@ -79,6 +76,7 @@ namespace ICD.Common.Utils.Collections
|
|||||||
|
|
||||||
m_Comparer = comparer;
|
m_Comparer = comparer;
|
||||||
m_OrderedKeys = new List<TKey>();
|
m_OrderedKeys = new List<TKey>();
|
||||||
|
m_ValuesOrderedByKey = new List<TValue>();
|
||||||
m_Dictionary = new Dictionary<TKey, TValue>(equalityComparer);
|
m_Dictionary = new Dictionary<TKey, TValue>(equalityComparer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,6 +103,7 @@ namespace ICD.Common.Utils.Collections
|
|||||||
public void Clear()
|
public void Clear()
|
||||||
{
|
{
|
||||||
m_OrderedKeys.Clear();
|
m_OrderedKeys.Clear();
|
||||||
|
m_ValuesOrderedByKey.Clear();
|
||||||
m_Dictionary.Clear();
|
m_Dictionary.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,7 +121,10 @@ namespace ICD.Common.Utils.Collections
|
|||||||
if (!m_Dictionary.Remove(key))
|
if (!m_Dictionary.Remove(key))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
m_OrderedKeys.Remove(key);
|
int index = m_OrderedKeys.BinarySearch(key, m_Comparer);
|
||||||
|
|
||||||
|
m_OrderedKeys.RemoveAt(index);
|
||||||
|
m_ValuesOrderedByKey.RemoveAt(index);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user