| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using CoreWCF.Runtime; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.Collections.Generic |
| | | 9 | | { |
| | | 10 | | public abstract class SynchronizedKeyedCollection<K, T> : SynchronizedCollection<T> |
| | | 11 | | { |
| | | 12 | | private const int DefaultThreshold = 0; |
| | | 13 | | private readonly IEqualityComparer<K> _comparer; |
| | | 14 | | private Dictionary<K, T> _dictionary; |
| | | 15 | | private int _keyCount; |
| | | 16 | | private readonly int _threshold; |
| | | 17 | | |
| | 587 | 18 | | protected SynchronizedKeyedCollection() |
| | | 19 | | { |
| | 587 | 20 | | _comparer = EqualityComparer<K>.Default; |
| | 587 | 21 | | _threshold = int.MaxValue; |
| | 587 | 22 | | } |
| | | 23 | | |
| | | 24 | | protected SynchronizedKeyedCollection(object syncRoot) |
| | 1760 | 25 | | : base(syncRoot) |
| | | 26 | | { |
| | 1760 | 27 | | _comparer = EqualityComparer<K>.Default; |
| | 1760 | 28 | | _threshold = int.MaxValue; |
| | 1760 | 29 | | } |
| | | 30 | | |
| | | 31 | | protected SynchronizedKeyedCollection(object syncRoot, IEqualityComparer<K> comparer) |
| | 0 | 32 | | : base(syncRoot) |
| | | 33 | | { |
| | 0 | 34 | | _comparer = comparer ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(comparer)); |
| | 0 | 35 | | _threshold = int.MaxValue; |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | protected SynchronizedKeyedCollection(object syncRoot, IEqualityComparer<K> comparer, int dictionaryCreationThre |
| | 0 | 39 | | : base(syncRoot) |
| | | 40 | | { |
| | 0 | 41 | | if (dictionaryCreationThreshold < -1) |
| | | 42 | | { |
| | 0 | 43 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(diction |
| | 0 | 44 | | SR.Format(SR.ValueMustBeInRange, -1, int.MaxValue))); |
| | | 45 | | } |
| | 0 | 46 | | else if (dictionaryCreationThreshold == -1) |
| | | 47 | | { |
| | 0 | 48 | | _threshold = int.MaxValue; |
| | | 49 | | } |
| | | 50 | | else |
| | | 51 | | { |
| | 0 | 52 | | _threshold = dictionaryCreationThreshold; |
| | | 53 | | } |
| | | 54 | | |
| | 0 | 55 | | _comparer = comparer ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(comparer)); |
| | 0 | 56 | | } |
| | | 57 | | |
| | | 58 | | public T this[K key] |
| | | 59 | | { |
| | | 60 | | get |
| | | 61 | | { |
| | 3181 | 62 | | if (key == null) |
| | | 63 | | { |
| | 0 | 64 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(key)); |
| | | 65 | | } |
| | | 66 | | |
| | 3181 | 67 | | lock (SyncRoot) |
| | | 68 | | { |
| | 3181 | 69 | | if (_dictionary != null) |
| | | 70 | | { |
| | 0 | 71 | | return _dictionary[key]; |
| | | 72 | | } |
| | | 73 | | |
| | 28826 | 74 | | for (int i = 0; i < Items.Count; i++) |
| | | 75 | | { |
| | 14413 | 76 | | T item = Items[i]; |
| | 14413 | 77 | | if (_comparer.Equals(key, GetKeyForItem(item))) |
| | | 78 | | { |
| | 3181 | 79 | | return item; |
| | | 80 | | } |
| | | 81 | | } |
| | | 82 | | |
| | 0 | 83 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new KeyNotFoundException()); |
| | | 84 | | } |
| | 3181 | 85 | | } |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | protected IDictionary<K, T> Dictionary |
| | | 89 | | { |
| | 0 | 90 | | get { return _dictionary; } |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | private void AddKey(K key, T item) |
| | | 94 | | { |
| | 3709 | 95 | | if (_dictionary != null) |
| | | 96 | | { |
| | 0 | 97 | | _dictionary.Add(key, item); |
| | | 98 | | } |
| | 3709 | 99 | | else if (_keyCount == _threshold) |
| | | 100 | | { |
| | 0 | 101 | | CreateDictionary(); |
| | 0 | 102 | | _dictionary.Add(key, item); |
| | | 103 | | } |
| | | 104 | | else |
| | | 105 | | { |
| | 3709 | 106 | | if (Contains(key)) |
| | | 107 | | { |
| | 0 | 108 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.CannotAddTwoItemsWithTheSameKeyToSyn |
| | | 109 | | } |
| | | 110 | | |
| | 3709 | 111 | | _keyCount++; |
| | | 112 | | } |
| | 3709 | 113 | | } |
| | | 114 | | |
| | | 115 | | protected void ChangeItemKey(T item, K newKey) |
| | | 116 | | { |
| | | 117 | | // check if the item exists in the collection |
| | 0 | 118 | | if (!ContainsItem(item)) |
| | | 119 | | { |
| | 0 | 120 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.ItemDoesNotExistInSynchronizedKeyedColle |
| | | 121 | | } |
| | | 122 | | |
| | 0 | 123 | | K oldKey = GetKeyForItem(item); |
| | 0 | 124 | | if (!_comparer.Equals(newKey, oldKey)) |
| | | 125 | | { |
| | 0 | 126 | | if (newKey != null) |
| | | 127 | | { |
| | 0 | 128 | | AddKey(newKey, item); |
| | | 129 | | } |
| | | 130 | | |
| | 0 | 131 | | if (oldKey != null) |
| | | 132 | | { |
| | 0 | 133 | | RemoveKey(oldKey); |
| | | 134 | | } |
| | | 135 | | } |
| | 0 | 136 | | } |
| | | 137 | | |
| | | 138 | | protected override void ClearItems() |
| | | 139 | | { |
| | 33 | 140 | | base.ClearItems(); |
| | | 141 | | |
| | 33 | 142 | | if (_dictionary != null) |
| | | 143 | | { |
| | 0 | 144 | | _dictionary.Clear(); |
| | | 145 | | } |
| | | 146 | | |
| | 33 | 147 | | _keyCount = 0; |
| | 33 | 148 | | } |
| | | 149 | | |
| | | 150 | | public bool Contains(K key) |
| | | 151 | | { |
| | 7589 | 152 | | if (key == null) |
| | | 153 | | { |
| | 0 | 154 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(key)); |
| | | 155 | | } |
| | | 156 | | |
| | 7589 | 157 | | lock (SyncRoot) |
| | | 158 | | { |
| | 7589 | 159 | | if (_dictionary != null) |
| | | 160 | | { |
| | 0 | 161 | | return _dictionary.ContainsKey(key); |
| | | 162 | | } |
| | | 163 | | |
| | 7589 | 164 | | if (key != null) |
| | | 165 | | { |
| | 59600 | 166 | | for (int i = 0; i < Items.Count; i++) |
| | | 167 | | { |
| | 25390 | 168 | | T item = Items[i]; |
| | 25390 | 169 | | if (_comparer.Equals(key, GetKeyForItem(item))) |
| | | 170 | | { |
| | 3179 | 171 | | return true; |
| | | 172 | | } |
| | | 173 | | } |
| | | 174 | | } |
| | 4410 | 175 | | return false; |
| | | 176 | | } |
| | 7589 | 177 | | } |
| | | 178 | | |
| | | 179 | | private bool ContainsItem(T item) |
| | | 180 | | { |
| | | 181 | | K key; |
| | 0 | 182 | | if ((_dictionary == null) || ((key = GetKeyForItem(item)) == null)) |
| | | 183 | | { |
| | 0 | 184 | | return Items.Contains(item); |
| | | 185 | | } |
| | | 186 | | |
| | | 187 | | |
| | 0 | 188 | | if (_dictionary.TryGetValue(key, out T itemInDict)) |
| | | 189 | | { |
| | 0 | 190 | | return EqualityComparer<T>.Default.Equals(item, itemInDict); |
| | | 191 | | } |
| | | 192 | | |
| | 0 | 193 | | return false; |
| | | 194 | | } |
| | | 195 | | |
| | | 196 | | private void CreateDictionary() |
| | | 197 | | { |
| | 0 | 198 | | _dictionary = new Dictionary<K, T>(_comparer); |
| | | 199 | | |
| | 0 | 200 | | foreach (T item in Items) |
| | | 201 | | { |
| | 0 | 202 | | K key = GetKeyForItem(item); |
| | 0 | 203 | | if (key != null) |
| | | 204 | | { |
| | 0 | 205 | | _dictionary.Add(key, item); |
| | | 206 | | } |
| | | 207 | | } |
| | 0 | 208 | | } |
| | | 209 | | |
| | | 210 | | protected abstract K GetKeyForItem(T item); |
| | | 211 | | |
| | | 212 | | protected override void InsertItem(int index, T item) |
| | | 213 | | { |
| | 3709 | 214 | | K key = GetKeyForItem(item); |
| | | 215 | | |
| | 3709 | 216 | | if (key != null) |
| | | 217 | | { |
| | 3709 | 218 | | AddKey(key, item); |
| | | 219 | | } |
| | | 220 | | |
| | 3709 | 221 | | base.InsertItem(index, item); |
| | 3709 | 222 | | } |
| | | 223 | | |
| | | 224 | | public bool Remove(K key) |
| | | 225 | | { |
| | 0 | 226 | | if (key == null) |
| | | 227 | | { |
| | 0 | 228 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(key)); |
| | | 229 | | } |
| | | 230 | | |
| | 0 | 231 | | lock (SyncRoot) |
| | | 232 | | { |
| | 0 | 233 | | if (_dictionary != null) |
| | | 234 | | { |
| | 0 | 235 | | if (_dictionary.ContainsKey(key)) |
| | | 236 | | { |
| | 0 | 237 | | return Remove(_dictionary[key]); |
| | | 238 | | } |
| | | 239 | | else |
| | | 240 | | { |
| | 0 | 241 | | return false; |
| | | 242 | | } |
| | | 243 | | } |
| | | 244 | | else |
| | | 245 | | { |
| | 0 | 246 | | for (int i = 0; i < Items.Count; i++) |
| | | 247 | | { |
| | 0 | 248 | | if (_comparer.Equals(key, GetKeyForItem(Items[i]))) |
| | | 249 | | { |
| | 0 | 250 | | RemoveItem(i); |
| | 0 | 251 | | return true; |
| | | 252 | | } |
| | | 253 | | } |
| | 0 | 254 | | return false; |
| | | 255 | | } |
| | | 256 | | } |
| | 0 | 257 | | } |
| | | 258 | | |
| | | 259 | | protected override void RemoveItem(int index) |
| | | 260 | | { |
| | 0 | 261 | | K key = GetKeyForItem(Items[index]); |
| | | 262 | | |
| | 0 | 263 | | if (key != null) |
| | | 264 | | { |
| | 0 | 265 | | RemoveKey(key); |
| | | 266 | | } |
| | | 267 | | |
| | 0 | 268 | | base.RemoveItem(index); |
| | 0 | 269 | | } |
| | | 270 | | |
| | | 271 | | private void RemoveKey(K key) |
| | | 272 | | { |
| | 0 | 273 | | if (!(key != null)) |
| | | 274 | | { |
| | | 275 | | Fx.Assert(false, "key shouldn't be null!"); |
| | 0 | 276 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(key)); |
| | | 277 | | } |
| | 0 | 278 | | if (_dictionary != null) |
| | | 279 | | { |
| | 0 | 280 | | _dictionary.Remove(key); |
| | | 281 | | } |
| | | 282 | | else |
| | | 283 | | { |
| | 0 | 284 | | _keyCount--; |
| | | 285 | | } |
| | 0 | 286 | | } |
| | | 287 | | |
| | | 288 | | protected override void SetItem(int index, T item) |
| | | 289 | | { |
| | 0 | 290 | | K newKey = GetKeyForItem(item); |
| | 0 | 291 | | K oldKey = GetKeyForItem(Items[index]); |
| | | 292 | | |
| | 0 | 293 | | if (_comparer.Equals(newKey, oldKey)) |
| | | 294 | | { |
| | 0 | 295 | | if ((newKey != null) && (_dictionary != null)) |
| | | 296 | | { |
| | 0 | 297 | | _dictionary[newKey] = item; |
| | | 298 | | } |
| | | 299 | | } |
| | | 300 | | else |
| | | 301 | | { |
| | 0 | 302 | | if (newKey != null) |
| | | 303 | | { |
| | 0 | 304 | | AddKey(newKey, item); |
| | | 305 | | } |
| | | 306 | | |
| | 0 | 307 | | if (oldKey != null) |
| | | 308 | | { |
| | 0 | 309 | | RemoveKey(oldKey); |
| | | 310 | | } |
| | | 311 | | } |
| | 0 | 312 | | base.SetItem(index, item); |
| | 0 | 313 | | } |
| | | 314 | | } |
| | | 315 | | } |