dotnetanywhere-wb /corlib/System.Collections.Generic/Dictionary.cs

Language C# Lines 615
MD5 Hash 107ae653e84ec9331314068af46c2856 Estimated Cost $12,857 (why?)
Repository https://bitbucket.org/cosi2/dotnetanywhere-wb.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
using System;

#if LOCALTEST
using System.Collections;
using System.Collections.Generic;
namespace System_.Collections.Generic {
#else
namespace System.Collections.Generic {
#endif
	public class Dictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>,
		IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable {

		public struct Enumerator : IEnumerator<KeyValuePair<TKey, TValue>>,
			IDisposable, IDictionaryEnumerator, IEnumerator {

			Dictionary<TKey, TValue> dict;
			private int curSlot, curItem;

			internal Enumerator(Dictionary<TKey, TValue> dictionary) {
				this.dict = dictionary;
				this.curSlot = -1;
				this.curItem = 0;
			}

			public bool MoveNext() {
				do {
					if (this.curSlot >= this.dict.capacity) {
						return false;
					}
					if (this.curSlot < 0 || this.dict.keys[this.curSlot] == null ||
						this.curItem > this.dict.keys[this.curSlot].Count) {
						this.curSlot++;
						this.curItem = 0;
					} else {
						this.curItem++;
					}
					if (this.curSlot >= this.dict.capacity) {
						return false;
					}
				} while (this.dict.keys[this.curSlot] == null || this.curItem >= dict.keys[this.curSlot].Count);
				return true;
			}

			public KeyValuePair<TKey, TValue> Current {
				get {
					return new KeyValuePair<TKey, TValue>(
						this.dict.keys[this.curSlot][this.curItem],
						this.dict.values[this.curSlot][this.curItem]
						);
				}
			}

			object IEnumerator.Current {
				get { return this.Current; }
			}

			void IEnumerator.Reset() {
				this.curSlot = -1;
				this.curItem = 0;
			}

			DictionaryEntry IDictionaryEnumerator.Entry {
				get {
					return new DictionaryEntry(
						this.dict.keys[this.curSlot][this.curItem],
						this.dict.values[this.curSlot][this.curItem]
					);
				}
			}

			object IDictionaryEnumerator.Key {
				get {
					return dict.keys[this.curSlot][this.curItem];
				}
			}

			object IDictionaryEnumerator.Value {
				get {
					return this.dict.values[this.curSlot][this.curItem];
				}
			}

			public void Dispose() {
				this.dict = null;
			}
		}

		public sealed class KeyCollection : ICollection<TKey>, IEnumerable<TKey>, ICollection, IEnumerable {

			public struct Enumerator : IEnumerator<TKey>, IDisposable, IEnumerator {

				private Dictionary<TKey, TValue>.Enumerator hostEnumerator;

				internal Enumerator(Dictionary<TKey, TValue> host) {
					this.hostEnumerator = host.GetEnumerator();
				}

				public void Dispose() {
					this.hostEnumerator.Dispose();
				}

				public bool MoveNext() {
					return this.hostEnumerator.MoveNext();
				}

				public TKey Current {
					get { return this.hostEnumerator.Current.Key; }
				}

				object IEnumerator.Current {
					get { return this.hostEnumerator.Current.Key; }
				}

				void IEnumerator.Reset() {
					((IEnumerator)this.hostEnumerator).Reset();
				}
			}

			private Dictionary<TKey, TValue> dictionary;

			public KeyCollection(Dictionary<TKey, TValue> dictionary) {
				if (dictionary == null) {
					throw new ArgumentException("dictionary");
				}
				this.dictionary = dictionary;
			}

			public void CopyTo(TKey[] array, int index) {
				throw new NotImplementedException();
			}

			public Enumerator GetEnumerator() {
				return new Enumerator(this.dictionary);
			}

			void ICollection<TKey>.Add(TKey item) {
				throw new NotSupportedException("this is a read-only collection");
			}

			void ICollection<TKey>.Clear() {
				throw new NotSupportedException("this is a read-only collection");
			}

			bool ICollection<TKey>.Contains(TKey item) {
				return this.dictionary.ContainsKey(item);
			}

			bool ICollection<TKey>.Remove(TKey item) {
				throw new NotSupportedException("this is a read-only collection");
			}

			IEnumerator<TKey> IEnumerable<TKey>.GetEnumerator() {
				return this.GetEnumerator();
			}

			void ICollection.CopyTo(Array array, int index) {
				this.CopyTo((TKey[])array, index);
			}

			IEnumerator IEnumerable.GetEnumerator() {
				return this.GetEnumerator();
			}

			public int Count {
				get {
					return this.dictionary.Count;
				}
			}

			bool ICollection<TKey>.IsReadOnly {
				get {
					return true;
				}
			}

			bool ICollection.IsSynchronized {
				get {
					return false;
				}
			}

			object ICollection.SyncRoot {
				get {
					return ((ICollection)this.dictionary).SyncRoot;
				}
			}

		}

		public sealed class ValueCollection : ICollection<TValue>, IEnumerable<TValue>, ICollection, IEnumerable {

			public struct Enumerator : IEnumerator<TValue>, IDisposable, IEnumerator {

				private Dictionary<TKey, TValue>.Enumerator hostEnumerator;

				internal Enumerator(Dictionary<TKey, TValue> host) {
					this.hostEnumerator = host.GetEnumerator();
				}

				public void Dispose() {
					this.hostEnumerator.Dispose();
				}

				public bool MoveNext() {
					return this.hostEnumerator.MoveNext();
				}

				public TValue Current {
					get { return this.hostEnumerator.Current.Value; }
				}

				object IEnumerator.Current {
					get { return this.hostEnumerator.Current.Value; }
				}

				void IEnumerator.Reset() {
					((IEnumerator)this.hostEnumerator).Reset();
				}
			}

			private Dictionary<TKey, TValue> dictionary;

			public ValueCollection(Dictionary<TKey, TValue> dictionary) {
				if (dictionary == null) {
					throw new ArgumentException("dictionary");
				}
				this.dictionary = dictionary;
			}

			public void CopyTo(TValue[] array, int index) {
				throw new NotImplementedException();
			}

			public Enumerator GetEnumerator() {
				return new Enumerator(this.dictionary);
			}

			void ICollection<TValue>.Add(TValue item) {
				throw new NotSupportedException("this is a read-only collection");
			}

			void ICollection<TValue>.Clear() {
				throw new NotSupportedException("this is a read-only collection");
			}

			bool ICollection<TValue>.Contains(TValue item) {
				return this.dictionary.ContainsValue(item);
			}

			bool ICollection<TValue>.Remove(TValue item) {
				throw new NotSupportedException("this is a read-only collection");
			}

			IEnumerator<TValue> IEnumerable<TValue>.GetEnumerator() {
				return this.GetEnumerator();
			}

			void ICollection.CopyTo(Array array, int index) {
				this.CopyTo((TValue[])array, index);
			}

			IEnumerator IEnumerable.GetEnumerator() {
				return this.GetEnumerator();
			}

			public int Count {
				get {
					return this.dictionary.Count;
				}
			}

			bool ICollection<TValue>.IsReadOnly {
				get {
					return true;
				}
			}

			bool ICollection.IsSynchronized {
				get {
					return false;
				}
			}

			object ICollection.SyncRoot {
				get {
					return ((ICollection)this.dictionary).SyncRoot;
				}
			}

		}

		private static int[] capacities = {
			11, 23, 47, 97, 191, 379, 757, 1511, 3023, 6047, 12097, 24179, 48353, 96731
		};

		private List<TKey>[] keys;
		private List<TValue>[] values;
		private int capacity, capacityIndex, count;
		private IEqualityComparer<TKey> comparer;

		public Dictionary() {
			this.Init(0, null);
		}

		public Dictionary(IDictionary<TKey, TValue> dictionary) : this(dictionary, null) { }

		public Dictionary(IEqualityComparer<TKey> comparer) {
			this.Init(0, comparer);
		}

		public Dictionary(int capacity) {
			this.Init(capacity, null);
		}

		public Dictionary(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer) {
			this.Init(dictionary.Count, comparer);
			foreach (var item in dictionary) {
				this.Add(item.Key, item.Value);
			}
		}

		public Dictionary(int capacity, IEqualityComparer<TKey> comparer) {
			this.Init(capacity, comparer);
		}

		private void Init(int initialCapacity, IEqualityComparer<TKey> comparer) {
			// Initialise the comparer
			this.comparer = comparer ?? EqualityComparer<TKey>.Default;
			// Initialise the capacity of the dictionary
			this.capacityIndex = -1;
			for (int i = 0; i < capacities.Length; i++) {
				if (initialCapacity <= capacities[i]) {
					this.capacityIndex = i;
					this.capacity = capacities[i];
					break;
				}
			}
			if (this.capacityIndex == -1){
				// If the capacity is off the end of the scale, then just use the capacity given
				this.capacity = initialCapacity;
				this.capacityIndex = capacities.Length;
			}
			this.Clear();
		}

		private int GetSlot(TKey key) {
			uint hash = (uint)key.GetHashCode();
			return (int)(hash % (uint)this.capacity);
		}

		public void Add(TKey key, TValue value) {
			Add(key, value, false);
		}

		private void Add(TKey key, TValue value, bool replace) {
			if (key == null) {
				throw new ArgumentNullException();
			}
			int slot = this.GetSlot(key);
			List<TKey> keySlot = this.keys[slot];
			if (keySlot != null) {
				// There are element(s) at this index, so see if this key is already in this dictionary
				// Can't use keySlot.IndexOf() because it doesn't honour the comparer
				for (int i = keySlot.Count - 1; i >= 0; i--) {
					if (this.comparer.Equals(keySlot[i], key)) {
						// The key is already in this dictionary
						if (replace) {
							this.values[slot][i] = value;
							return;
						} else {
							throw new ArgumentException("Key already exists in dictionary");
						}
					}
				}
				// Key not already in dictionary, so carry on
			}
			this.count++;
			if (this.count > this.capacity) {
				// Increase capacity
				List<TKey>[] currentKeys = this.keys;
				List<TValue>[] currentValues = this.values;
				this.capacityIndex++;
				this.capacity = (this.capacityIndex >= capacities.Length) ?
					this.capacity * 2 + 1 : capacities[this.capacityIndex];
				this.Clear();
				// Add all the items in this dictionary to the enlarged dictionary lists.
				for (int slotIdx = currentKeys.Length - 1; slotIdx >= 0; slotIdx--) {
					List<TKey> currentKeySlot = currentKeys[slotIdx];
					if (currentKeySlot != null) {
						List<TValue> currentValueSlot = currentValues[slotIdx];
						for (int listIdx = currentKeySlot.Count - 1; listIdx >= 0; listIdx--) {
							this.Add(currentKeySlot[listIdx], currentValueSlot[listIdx], false);
						}
					}
				}
				// Reload these values, as they will have changed due to dictionary capacity resizing
				slot = key.GetHashCode() % this.capacity;
				keySlot = this.keys[slot];
			}
			List<TValue> valueSlot;
			if (keySlot == null) {
				// There are no elements at this index, so create a new list for the element being added
				this.keys[slot] = keySlot = new List<TKey>(1);
				this.values[slot] = valueSlot = new List<TValue>(1);
			} else {
				valueSlot = this.values[slot];
			}
//			Console.WriteLine("CHECK8\n");
//			Console.WriteLine("keySlot={0}, valueSlot={1}\n", keySlot, valueSlot);
			keySlot.Add(key);
			valueSlot.Add(value);
		}

		public TValue this[TKey key] {
			get {
				TValue value;
				if (this.TryGetValue(key, out value)) {
					return value;
				}
				throw new KeyNotFoundException(key.ToString());
			}
			set {
				Add(key, value, true);
			}
		}

		public bool TryGetValue(TKey key, out TValue value) {
			if (key == null) {
				throw new ArgumentNullException();
			}
			int slot = this.GetSlot(key);
			List<TKey> keySlot = this.keys[slot];
			if (keySlot != null) {
				// Can't use keySlot.IndexOf() because it doesn't honour the comparer
				for (int i = keySlot.Count - 1; i >= 0; i--) {
					if (this.comparer.Equals(keySlot[i], key)) {
						value = this.values[slot][i];
						return true;
					}
				}
			}
			value = default(TValue);
			return false;
		}

		public bool ContainsKey(TKey key) {
			TValue dummy;
			return (TryGetValue(key, out dummy));
		}

		public bool ContainsValue(TValue value) {
			Enumerator e = new Enumerator(this);
			while (e.MoveNext()) {
				if (e.Current.Value.Equals(value)) {
					return true;
				}
			}
			return false;
		}

		public bool Remove(TKey key) {
			int slot = this.GetSlot(key);
			List<TKey> keySlot = this.keys[slot];
			if (keySlot != null) {
				// Can't use keySlot.IndexOf() because it doesn't honour the comparer
				for (int i = keySlot.Count - 1; i >= 0; i--) {
					if (this.comparer.Equals(keySlot[i], key)) {
						keySlot.RemoveAt(i);
						this.values[slot].RemoveAt(i);
						this.count--;
						return true;
					}
				}
			}
			return false;
		}

		public IEqualityComparer<TKey> Comparer {
			get {
				return this.comparer;
			}
		}

		public int Count {
			get {
				return this.count;
			}
		}

		public KeyCollection Keys {
			get {
				return new KeyCollection(this);
			}
		}

		public ValueCollection Values {
			get {
				return new ValueCollection(this);
			}
		}

		public bool IsReadOnly {
			get {
				return false;
			}
		}

		public void Clear() {
			this.keys = new List<TKey>[this.capacity];
			this.values = new List<TValue>[this.capacity];
			this.count = 0;
		}

		public Enumerator GetEnumerator() {
			return new Enumerator(this);
		}

		ICollection<TKey> IDictionary<TKey, TValue>.Keys {
			get {
				return new KeyCollection(this);
			}
		}

		ICollection<TValue> IDictionary<TKey, TValue>.Values {
			get {
				return new ValueCollection(this);
			}
		}

		void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item) {
			this.Add(item.Key, item.Value);
		}

		bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item) {
			return this.ContainsKey(item.Key);
		}

		void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) {
			throw new Exception("The method or operation is not implemented.");
		}

		bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item) {
			return this.Remove(item.Key);
		}

		IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator() {
			return new Enumerator(this);
		}

		IEnumerator IEnumerable.GetEnumerator() {
			return new Enumerator(this);
		}

		public bool IsFixedSize {
			get {
				return false;
			}
		}

		object IDictionary.this[object key] {
			get {
				return this[(TKey)key];
			}
			set {
				this[(TKey)key] = (TValue)value;
			}
		}

		ICollection IDictionary.Keys {
			get {
				return Keys;
			}
		}

		ICollection IDictionary.Values {
			get {
				return Values;
			}
		}

		void IDictionary.Add(object key, object value) {
			Add((TKey)key, (TValue)value);
		}
		
		bool IDictionary.Contains(object key) {
			return ContainsKey((TKey)key);
		}

		IDictionaryEnumerator IDictionary.GetEnumerator() {
			return new Enumerator(this);
		}

		void IDictionary.Remove(object key) {
			Remove((TKey)key);
		}

		public bool IsSynchronized {
			get {
				return false;
			}
		}

		public object SyncRoot {
			get {
				return this;
			}
		}

		public void CopyTo(Array array, int index) {
			throw new Exception("The method or operation is not implemented.");
		}

	}
}
Back to Top