Debian Codegraph @

mcs/class/referencesource/mscorlib/system/collections/objectmodel/keyedcollection.cs

raw
1 // ==++==
2 //
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 //
5 // ==--==
6 // <OWNER>Microsoft</OWNER>
7 //
8
9 namespace System.Collections.ObjectModel
10 {
11 using System;
12 using System.Collections.Generic;
13 using System.Diagnostics;
14 using System.Diagnostics.Contracts;
15
16 [Serializable]
17 [System.Runtime.InteropServices.ComVisible(false)]
18 [DebuggerTypeProxy(typeof(Mscorlib_KeyedCollectionDebugView<,>))]
19 [DebuggerDisplay("Count = {Count}")]
20 public abstract class KeyedCollection<TKey,TItem>: Collection<TItem>
21 {
22 const int defaultThreshold = 0;
23
24 IEqualityComparer<TKey> comparer;
25 Dictionary<TKey,TItem> dict;
26 int keyCount;
27 int threshold;
28
29 protected KeyedCollection(): this(null, defaultThreshold) {}
30
31 protected KeyedCollection(IEqualityComparer<TKey> comparer): this(comparer, defaultThreshold) {}
32
33
34 protected KeyedCollection(IEqualityComparer<TKey> comparer, int dictionaryCreationThreshold) {
35 if (comparer == null) {
36 comparer = EqualityComparer<TKey>.Default;
37 }
38
39 if (dictionaryCreationThreshold == -1) {
40 dictionaryCreationThreshold = int.MaxValue;
41 }
42
43 if( dictionaryCreationThreshold < -1) {
44 ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.dictionaryCreationThreshold, ExceptionResource.ArgumentOutOfRange_InvalidThreshold);
45 }
46
47 this.comparer = comparer;
48 this.threshold = dictionaryCreationThreshold;
49 }
50
51 public IEqualityComparer<TKey> Comparer {
52 get {
53 return comparer;
54 }
55 }
56
57 public TItem this[TKey key] {
58 get {
59 if( key == null) {
60 ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
61 }
62
63 if (dict != null) {
64 return dict[key];
65 }
66
67 foreach (TItem item in Items) {
68 if (comparer.Equals(GetKeyForItem(item), key)) return item;
69 }
70
71 ThrowHelper.ThrowKeyNotFoundException();
72 return default(TItem);
73 }
74 }
75
76 public bool Contains(TKey key) {
77 if( key == null) {
78 ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
79 }
80
81 if (dict != null) {
82 return dict.ContainsKey(key);
83 }
84
85 if (key != null) {
86 foreach (TItem item in Items) {
87 if (comparer.Equals(GetKeyForItem(item), key)) return true;
88 }
89 }
90 return false;
91 }
92
93 private bool ContainsItem(TItem item) {
94 TKey key;
95 if( (dict == null) || ((key = GetKeyForItem(item)) == null)) {
96 return Items.Contains(item);
97 }
98
99 TItem itemInDict;
100 bool exist = dict.TryGetValue(key, out itemInDict);
101 if( exist) {
102 return EqualityComparer<TItem>.Default.Equals(itemInDict, item);
103 }
104 return false;
105 }
106
107 public bool Remove(TKey key) {
108 if( key == null) {
109 ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
110 }
111
112 if (dict != null) {
113 if (dict.ContainsKey(key)) {
114 return Remove(dict[key]);
115 }
116
117 return false;
118 }
119
120 if (key != null) {
121 for (int i = 0; i < Items.Count; i++) {
122 if (comparer.Equals(GetKeyForItem(Items[i]), key)) {
123 RemoveItem(i);
124 return true;
125 }
126 }
127 }
128 return false;
129 }
130
131 protected IDictionary<TKey,TItem> Dictionary {
132 get { return dict; }
133 }
134
135 protected void ChangeItemKey(TItem item, TKey newKey) {
136 // check if the item exists in the collection
137 if( !ContainsItem(item)) {
138 ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_ItemNotExist);
139 }
140
141 TKey oldKey = GetKeyForItem(item);
142 if (!comparer.Equals(oldKey, newKey)) {
143 if (newKey != null) {
144 AddKey(newKey, item);
145 }
146
147 if (oldKey != null) {
148 RemoveKey(oldKey);
149 }
150 }
151 }
152
153 protected override void ClearItems() {
154 base.ClearItems();
155 if (dict != null) {
156 dict.Clear();
157 }
158
159 keyCount = 0;
160 }
161
162 protected abstract TKey GetKeyForItem(TItem item);
163
164 protected override void InsertItem(int index, TItem item) {
165 TKey key = GetKeyForItem(item);
166 if (key != null) {
167 AddKey(key, item);
168 }
169 base.InsertItem(index, item);
170 }
171
172 protected override void RemoveItem(int index) {
173 TKey key = GetKeyForItem(Items[index]);
174 if (key != null) {
175 RemoveKey(key);
176 }
177 base.RemoveItem(index);
178 }
179
180 protected override void SetItem(int index, TItem item) {
181 TKey newKey = GetKeyForItem(item);
182 TKey oldKey = GetKeyForItem(Items[index]);
183
184 if (comparer.Equals(oldKey, newKey)) {
185 if (newKey != null && dict != null) {
186 dict[newKey] = item;
187 }
188 }
189 else {
190 if (newKey != null) {
191 AddKey(newKey, item);
192 }
193
194 if (oldKey != null) {
195 RemoveKey(oldKey);
196 }
197 }
198 base.SetItem(index, item);
199 }
200
201 private void AddKey(TKey key, TItem item) {
202 if (dict != null) {
203 dict.Add(key, item);
204 }
205 else if (keyCount == threshold) {
206 CreateDictionary();
207 dict.Add(key, item);
208 }
209 else {
210 if (Contains(key)) {
211 ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_AddingDuplicate);
212 }
213
214 keyCount++;
215 }
216 }
217
218 private void CreateDictionary() {
219 dict = new Dictionary<TKey,TItem>(comparer);
220 foreach (TItem item in Items) {
221 TKey key = GetKeyForItem(item);
222 if (key != null) {
223 dict.Add(key, item);
224 }
225 }
226 }
227
228 private void RemoveKey(TKey key) {
229 Contract.Assert(key != null, "key shouldn't be null!");
230 if (dict != null) {
231 dict.Remove(key);
232 }
233 else {
234 keyCount--;
235 }
236 }
237 }
238 }