|
|
@@ -42,58 +42,167 @@ namespace System.Collections.ObjectModel
|
|
|
[Serializable]
|
|
|
public abstract class KeyedCollection<TKey, TItem> : Collection<TItem>
|
|
|
{
|
|
|
+ private Dictionary<TKey, TItem> dictionary;
|
|
|
+ private IEqualityComparer<TKey> comparer;
|
|
|
+ private int dictionaryCreationThreshold;
|
|
|
+
|
|
|
+ protected KeyedCollection ()
|
|
|
+ : this (null, 0)
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+ protected KeyedCollection (IEqualityComparer<TKey> comparer)
|
|
|
+ : this(comparer, 0)
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+ protected KeyedCollection (IEqualityComparer<TKey> comparer, int dictionaryCreationThreshold)
|
|
|
+ {
|
|
|
+ if (comparer != null)
|
|
|
+ this.comparer = comparer;
|
|
|
+ else
|
|
|
+ this.comparer = EqualityComparer<TKey>.Default;
|
|
|
+
|
|
|
+ this.dictionaryCreationThreshold = dictionaryCreationThreshold;
|
|
|
+
|
|
|
+ if (dictionaryCreationThreshold == 0)
|
|
|
+ dictionary = new Dictionary<TKey, TItem> (this.comparer);
|
|
|
+ }
|
|
|
+
|
|
|
public bool Contains (TKey key)
|
|
|
{
|
|
|
- throw new NotImplementedException ();
|
|
|
+ if (dictionary != null)
|
|
|
+ return dictionary.ContainsKey (key);
|
|
|
+ return IndexOfKey (key) >= 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ private int IndexOfKey (TKey key)
|
|
|
+ {
|
|
|
+ for (int i = Count - 1; i >= 0; i--)
|
|
|
+ {
|
|
|
+ TKey lkey = GetKeyForItem (this [i]);
|
|
|
+ if (comparer.Equals (key, lkey))
|
|
|
+ return i;
|
|
|
+ }
|
|
|
+ return -1;
|
|
|
}
|
|
|
|
|
|
public bool Remove (TKey key)
|
|
|
{
|
|
|
- throw new NotImplementedException ();
|
|
|
+ TItem item;
|
|
|
+ if (dictionary != null)
|
|
|
+ {
|
|
|
+ if (dictionary.TryGetValue (key, out item))
|
|
|
+ return base.Remove(item);
|
|
|
+ else
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ int idx = IndexOfKey (key);
|
|
|
+
|
|
|
+ if (idx == -1)
|
|
|
+ return false;
|
|
|
+
|
|
|
+ RemoveAt(idx);
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
public IEqualityComparer<TKey> Comparer {
|
|
|
get {
|
|
|
- throw new NotImplementedException ();
|
|
|
+ return comparer;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public TItem this[TKey key] {
|
|
|
+ public TItem this [TKey key] {
|
|
|
get {
|
|
|
- throw new NotImplementedException ();
|
|
|
+ if (dictionary != null)
|
|
|
+ return dictionary [key];
|
|
|
+
|
|
|
+ int idx = IndexOfKey (key);
|
|
|
+ if (idx >= 0)
|
|
|
+ return base [idx];
|
|
|
+ else
|
|
|
+ throw new KeyNotFoundException();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
protected void ChangeItemKey (TItem item, TKey newKey)
|
|
|
{
|
|
|
- throw new NotImplementedException ();
|
|
|
+ if (!Contains(item)) throw new ArgumentException();
|
|
|
+
|
|
|
+ TKey oldKey = GetKeyForItem (item);
|
|
|
+ if (comparer.Equals (oldKey, newKey)) return;
|
|
|
+
|
|
|
+ if (Contains (newKey)) throw new ArgumentException();
|
|
|
+ if (dictionary != null)
|
|
|
+ {
|
|
|
+
|
|
|
+ if (!dictionary.Remove (oldKey))
|
|
|
+ throw new ArgumentException();
|
|
|
+
|
|
|
+ dictionary.Add (newKey, item);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- protected void ClearItems ()
|
|
|
+ protected override void ClearItems ()
|
|
|
{
|
|
|
- throw new NotImplementedException ();
|
|
|
+ if (dictionary != null)
|
|
|
+ {
|
|
|
+ dictionary.Clear();
|
|
|
+ }
|
|
|
+
|
|
|
+ base.ClearItems ();
|
|
|
}
|
|
|
|
|
|
protected abstract TKey GetKeyForItem (TItem item);
|
|
|
|
|
|
- protected virtual void InsertItem (int index, TItem item)
|
|
|
+ protected override void InsertItem (int index, TItem item)
|
|
|
{
|
|
|
- throw new NotImplementedException ();
|
|
|
+ if (dictionary != null)
|
|
|
+ {
|
|
|
+ dictionary.Add (GetKeyForItem (item), item);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if (dictionaryCreationThreshold != -1 && Count + 1 > dictionaryCreationThreshold)
|
|
|
+ {
|
|
|
+ dictionary = new Dictionary<TKey, TItem> (comparer);
|
|
|
+
|
|
|
+ for (int i = Count - 1; i >= 0; i--)
|
|
|
+ {
|
|
|
+ TItem dictitem = this[i];
|
|
|
+ dictionary.Add(GetKeyForItem(dictitem), dictitem);
|
|
|
+ }
|
|
|
+
|
|
|
+ dictionary.Add (GetKeyForItem (item), item);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ base.InsertItem (index, item);
|
|
|
}
|
|
|
|
|
|
- protected virtual void RemoveItem (int index)
|
|
|
+ protected override void RemoveItem (int index)
|
|
|
{
|
|
|
- throw new NotImplementedException ();
|
|
|
+ if (dictionary != null)
|
|
|
+ {
|
|
|
+ TKey key = GetKeyForItem (this [index]);
|
|
|
+ dictionary.Remove (key);
|
|
|
+ }
|
|
|
+ base.RemoveItem (index);
|
|
|
}
|
|
|
|
|
|
- protected virtual void SetItem (int index, TItem item)
|
|
|
+ protected override void SetItem (int index, TItem item)
|
|
|
{
|
|
|
- throw new NotImplementedException ();
|
|
|
+ if (dictionary != null)
|
|
|
+ {
|
|
|
+ dictionary.Remove (GetKeyForItem (this [index]));
|
|
|
+ dictionary.Add (GetKeyForItem (item), item);
|
|
|
+ }
|
|
|
+ base.SetItem (index, item);
|
|
|
}
|
|
|
|
|
|
protected IDictionary<TKey, TItem> Dictionary {
|
|
|
get {
|
|
|
- throw new NotImplementedException ();
|
|
|
+ return dictionary;
|
|
|
}
|
|
|
}
|
|
|
}
|