1 // -*- Mode: csharp; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3 // System.Collections.ObjectModel.KeyedCollection
6 // Zoltan Varga (vargaz@gmail.com)
8 // (C) 2005 Novell, Inc.
12 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Collections.Generic;
36 using System.Runtime.InteropServices;
37 using System.Diagnostics;
39 namespace System.Collections.ObjectModel
43 [DebuggerDisplay ("Count={Count}")]
44 [DebuggerTypeProxy (typeof (CollectionDebuggerView<,>))]
45 public abstract class KeyedCollection<TKey, TItem> : Collection<TItem>
47 private Dictionary<TKey, TItem> dictionary;
48 private IEqualityComparer<TKey> comparer;
49 private int dictionaryCreationThreshold;
51 protected KeyedCollection ()
56 protected KeyedCollection (IEqualityComparer<TKey> comparer)
61 protected KeyedCollection (IEqualityComparer<TKey> comparer, int dictionaryCreationThreshold)
64 this.comparer = comparer;
66 this.comparer = EqualityComparer<TKey>.Default;
68 this.dictionaryCreationThreshold = dictionaryCreationThreshold;
70 if (dictionaryCreationThreshold == 0)
71 dictionary = new Dictionary<TKey, TItem> (this.comparer);
74 public bool Contains (TKey key)
76 if (dictionary != null)
77 return dictionary.ContainsKey (key);
78 return IndexOfKey (key) >= 0;
81 private int IndexOfKey (TKey key)
83 for (int i = Count - 1; i >= 0; i--)
85 TKey lkey = GetKeyForItem (this [i]);
86 if (comparer.Equals (key, lkey))
92 public bool Remove (TKey key)
95 if (dictionary != null)
97 if (dictionary.TryGetValue (key, out item))
98 return base.Remove(item);
103 int idx = IndexOfKey (key);
112 public IEqualityComparer<TKey> Comparer {
118 public TItem this [TKey key] {
120 if (dictionary != null)
121 return dictionary [key];
123 int idx = IndexOfKey (key);
127 throw new KeyNotFoundException();
131 protected void ChangeItemKey (TItem item, TKey newKey)
133 if (!Contains(item)) throw new ArgumentException();
135 TKey oldKey = GetKeyForItem (item);
136 if (comparer.Equals (oldKey, newKey)) return;
138 if (Contains (newKey)) throw new ArgumentException();
139 if (dictionary != null)
142 if (!dictionary.Remove (oldKey))
143 throw new ArgumentException();
145 dictionary.Add (newKey, item);
149 protected override void ClearItems ()
151 if (dictionary != null)
159 protected abstract TKey GetKeyForItem (TItem item);
161 protected override void InsertItem (int index, TItem item)
163 TKey key = GetKeyForItem (item);
165 throw new ArgumentNullException ("GetKeyForItem(item)");
167 if (dictionary != null && dictionary.ContainsKey (key))
168 throw new ArgumentException ("An element with the same key already exists in the dictionary.");
170 if (dictionary == null)
171 for (int i = 0; i < Count; ++i) {
172 if (comparer.Equals (key, GetKeyForItem (this [i]))) {
173 throw new ArgumentException ("An element with the same key already exists in the dictionary.");
177 base.InsertItem (index, item);
179 if (dictionary != null)
180 dictionary.Add (key, item);
181 else if (dictionaryCreationThreshold != -1 && Count > dictionaryCreationThreshold) {
182 dictionary = new Dictionary<TKey, TItem> (comparer);
184 for (int i = 0; i < Count; ++i) {
185 TItem dictitem = this [i];
186 dictionary.Add (GetKeyForItem (dictitem), dictitem);
191 protected override void RemoveItem (int index)
193 if (dictionary != null)
195 TKey key = GetKeyForItem (this [index]);
196 dictionary.Remove (key);
198 base.RemoveItem (index);
201 protected override void SetItem (int index, TItem item)
203 if (dictionary != null)
205 dictionary.Remove (GetKeyForItem (this [index]));
206 dictionary.Add (GetKeyForItem (item), item);
208 base.SetItem (index, item);
211 protected IDictionary<TKey, TItem> Dictionary {