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.
36 using System.Collections.Generic;
37 using System.Runtime.InteropServices;
39 namespace System.Collections.ObjectModel
43 public abstract class KeyedCollection<TKey, TItem> : Collection<TItem>
45 private Dictionary<TKey, TItem> dictionary;
46 private IEqualityComparer<TKey> comparer;
47 private int dictionaryCreationThreshold;
49 protected KeyedCollection ()
54 protected KeyedCollection (IEqualityComparer<TKey> comparer)
59 protected KeyedCollection (IEqualityComparer<TKey> comparer, int dictionaryCreationThreshold)
62 this.comparer = comparer;
64 this.comparer = EqualityComparer<TKey>.Default;
66 this.dictionaryCreationThreshold = dictionaryCreationThreshold;
68 if (dictionaryCreationThreshold == 0)
69 dictionary = new Dictionary<TKey, TItem> (this.comparer);
72 public bool Contains (TKey key)
74 if (dictionary != null)
75 return dictionary.ContainsKey (key);
76 return IndexOfKey (key) >= 0;
79 private int IndexOfKey (TKey key)
81 for (int i = Count - 1; i >= 0; i--)
83 TKey lkey = GetKeyForItem (this [i]);
84 if (comparer.Equals (key, lkey))
90 public bool Remove (TKey key)
93 if (dictionary != null)
95 if (dictionary.TryGetValue (key, out item))
96 return base.Remove(item);
101 int idx = IndexOfKey (key);
110 public IEqualityComparer<TKey> Comparer {
116 public TItem this [TKey key] {
118 if (dictionary != null)
119 return dictionary [key];
121 int idx = IndexOfKey (key);
125 throw new KeyNotFoundException();
129 protected void ChangeItemKey (TItem item, TKey newKey)
131 if (!Contains(item)) throw new ArgumentException();
133 TKey oldKey = GetKeyForItem (item);
134 if (comparer.Equals (oldKey, newKey)) return;
136 if (Contains (newKey)) throw new ArgumentException();
137 if (dictionary != null)
140 if (!dictionary.Remove (oldKey))
141 throw new ArgumentException();
143 dictionary.Add (newKey, item);
147 protected override void ClearItems ()
149 if (dictionary != null)
157 protected abstract TKey GetKeyForItem (TItem item);
159 protected override void InsertItem (int index, TItem item)
161 if (dictionary != null)
163 dictionary.Add (GetKeyForItem (item), item);
167 if (dictionaryCreationThreshold != -1 && Count + 1 > dictionaryCreationThreshold)
169 dictionary = new Dictionary<TKey, TItem> (comparer);
171 for (int i = Count - 1; i >= 0; i--)
173 TItem dictitem = this[i];
174 dictionary.Add(GetKeyForItem(dictitem), dictitem);
177 dictionary.Add (GetKeyForItem (item), item);
180 base.InsertItem (index, item);
183 protected override void RemoveItem (int index)
185 if (dictionary != null)
187 TKey key = GetKeyForItem (this [index]);
188 dictionary.Remove (key);
190 base.RemoveItem (index);
193 protected override void SetItem (int index, TItem item)
195 if (dictionary != null)
197 dictionary.Remove (GetKeyForItem (this [index]));
198 dictionary.Add (GetKeyForItem (item), item);
200 base.SetItem (index, item);
203 protected IDictionary<TKey, TItem> Dictionary {