File: SortedDictionary.cs

package info (click to toggle)
mono 1.2.2.1-1etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 142,720 kB
  • ctags: 256,408
  • sloc: cs: 1,495,736; ansic: 249,442; sh: 18,327; xml: 12,463; makefile: 5,046; perl: 1,248; asm: 635; yacc: 285; sql: 7
file content (786 lines) | stat: -rw-r--r-- 17,323 bytes parent folder | download | duplicates (2)
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
//
// System.Collections.Generic.SortedDictionary
//
// Authors:
//    Kazuki Oikawa (kazuki@panicode.com)
//    Atsushi Enomoto (atsushi@ximian.com)
//

//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#if NET_2_0
using System;
using System.Collections;

namespace System.Collections.Generic
{
	[Serializable]
	public class SortedDictionary<TKey,TValue> : IDictionary<TKey,TValue>, ICollection<KeyValuePair<TKey,TValue>>, IEnumerable<KeyValuePair<TKey,TValue>>, IDictionary, ICollection, IEnumerable
	{
		TKey [] _keys;
		TValue [] _values;
		IComparer<TKey> _comparer;
		int _size;
		int _version = 0;
		const int DefaultCapacitySize = 4;

		KeyCollection _keyList;
		ValueCollection _valueList;

		#region Constructor
		public SortedDictionary () : this (0, null)
		{
		}

		public SortedDictionary (IComparer<TKey> comparer) : this (0, comparer)
		{
		}

		public SortedDictionary (IDictionary<TKey,TValue> dic) : this (dic, null)
		{
		}

		// it disappeared in 2.0 RTM
		SortedDictionary (int capacity) : this (capacity, null)
		{

		}

		public SortedDictionary (IDictionary<TKey,TValue> dic, IComparer<TKey> comparer) : this (dic == null ? 0 : dic.Count, comparer)
		{
			if (dic == null)
				throw new ArgumentNullException ();

			dic.Keys.CopyTo (_keys, 0);
			dic.Values.CopyTo (_values, 0);
			Array.Sort<TKey,TValue> (_keys, _values);
			_size = dic.Count;
		}

		// it disappeared in 2.0 RTM
		SortedDictionary (int capacity, IComparer<TKey> comparer)
		{
			if (capacity < 0)
				throw new ArgumentOutOfRangeException ();

			_keys = new TKey [capacity];
			_values = new TValue [capacity];
			_size = 0;

			if (comparer == null)
				_comparer = Comparer<TKey>.Default;
			else
				_comparer = comparer;
		}
		#endregion

		#region PublicProperty

		public IComparer<TKey> Comparer {
			get { return _comparer; }
		}

		// It disappeared in 2.0 RTM.
		int Capacity {
			get { return _keys.Length; }
			set {
				if (value < _size)
					throw new ArgumentOutOfRangeException ();
				
				Array.Resize<TKey> (ref _keys, value);
				Array.Resize<TValue> (ref _values, value);
			}
		}

		public int Count {
			get { return _size; }
		}

		public TValue this [TKey key] {
			get {
				int index = IndexOfKey (key);
				if (index >= 0)
					return _values [index];
				
				throw new KeyNotFoundException ();
			}
			set {
				if (key == null)
					throw new ArgumentNullException ();
				
				int index = IndexOfKey (key);
				if (index < 0)
					Add (key, value);
				else
					_values [index] = value;
			}
		}

		public KeyCollection Keys {
			get { return GetKeyCollection (); }
		}

		public ValueCollection Values {
			get { return GetValueCollection (); }
		}
		#endregion

		#region PublicMethod

		public void Add (TKey key, TValue value)
		{
			if (key == null) 
				throw new ArgumentNullException ();
			
			int index = Array.BinarySearch<TKey> (_keys, 0, _size, key, _comparer);
			if (index >= 0)
				throw new ArgumentException ();

			index = ~index;

			if (_size == _keys.Length)
				Capacity += Capacity > 0 ? Capacity : DefaultCapacitySize;

			if (index < _size) {
				Array.Copy (_keys, index, _keys, index + 1, _size - index);
				Array.Copy (_values, index, _values, index + 1, _size - index);
			}

			_keys [index] = key;
			_values [index] = value;
			_size++;
			_version++;
		}

		public void Clear ()
		{
			Array.Clear (_keys, 0, _size);
			Array.Clear (_values, 0, _size);
			_size = 0;
			_version++;
		}

		public bool ContainsKey (TKey key)
		{
			return IndexOfKey (key) >= 0;
		}

		public bool ContainsValue (TValue value)
		{
			return IndexOfValue (value) >= 0;
		}

		public void CopyTo (KeyValuePair<TKey,TValue>[] array, int arrayIndex)
		{
			if (array == null)
				throw new ArgumentNullException ();
			if (arrayIndex < 0 || array.Length <= arrayIndex)
				throw new ArgumentOutOfRangeException ();
			if (array.Length - arrayIndex < _size)
				throw new ArgumentException ();

			for (int i = 0; i < _size; i ++) {
				array [arrayIndex + i] = new KeyValuePair<TKey,TValue> (_keys [i], _values [i]);
			}
		}
		
		public Enumerator GetEnumerator ()
		{
			return new Enumerator (this);
		}

		int IndexOfKey (TKey key)
		{
			if (key == null)
				throw new ArgumentNullException ();

			return Array.BinarySearch<TKey> (_keys, 0, _size, key, _comparer);
		}

		int IndexOfValue (TValue value)
		{
			return Array.IndexOf<TValue> (_values, value, 0, _size);
		}

		public bool Remove (TKey key)
		{
			int index = IndexOfKey (key);
			if (index >= 0) {
				RemoveAt (index);
				return true;
			}
			return false;
		}

		void RemoveAt (int index)
		{
			if (index < 0 || _size <= index)
				throw new ArgumentOutOfRangeException ();

			_size--;
			if (index < _size) {
				Array.Copy (_keys, index + 1, _keys, index, _size - index);
				Array.Copy (_values, index + 1, _values, index, _size - index);
			}

			_keys[_size] = default (TKey) ;
			_values[_size] = default (TValue) ;
			_version++;
		}

		public bool TryGetValue (TKey key, out TValue value)
		{
			int index = IndexOfKey (key);
			if (index >= 0) {
				value = _values [index];
				return true;
			}

			value = default (TValue) ;
			return false;			
		}

		#endregion

		#region PrivateMethod
		private KeyCollection GetKeyCollection ()
		{
			if (_keyList == null)
				_keyList = new KeyCollection (this);
			return _keyList;
		}
		private ValueCollection GetValueCollection ()
		{
			if (_valueList == null)
				_valueList = new ValueCollection (this);
			return _valueList;
		}

		TKey ToKey (object key)
		{
			if (key == null)
				throw new ArgumentNullException ("key");
			if (!(key is TKey))
				throw new ArgumentException (String.Format ("Key \"{0}\" cannot be converted to the key type {1}.", key, typeof (TKey)));
			return (TKey) key;
		}

		TValue ToValue (object value)
		{
			if (!(value is TValue) && (value != null || typeof (TValue).IsValueType))
				throw new ArgumentException (String.Format ("Value \"{0}\" cannot be converted to the value type {1}.", value, typeof (TValue)));
			return (TValue) value;
		}
		#endregion

		#region IDictionary<TKey,TValue> Member

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

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

		#endregion

		#region ICollection<KeyValuePair<TKey,TValue>> Member

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

		bool ICollection<KeyValuePair<TKey,TValue>>.Contains (KeyValuePair<TKey,TValue> item)
		{
			int index = IndexOfKey (item.Key);
			return index >= 0 && Comparer<TValue>.Default.Compare (_values [index], item.Value) == 0;
		}

		bool ICollection<KeyValuePair<TKey,TValue>>.IsReadOnly {
			get { return false; }
		}

		bool ICollection<KeyValuePair<TKey,TValue>>.Remove (KeyValuePair<TKey,TValue> item)
		{
			int index = IndexOfKey (item.Key);
			if (index >= 0 && Comparer<TValue>.Default.Compare (_values [index], item.Value) == 0) {
				RemoveAt (index);
				return true;
			}
			return false;
		}

		#endregion

		#region IDictionary Member

		void IDictionary.Add (object key, object value)
		{
			Add (ToKey (key), ToValue (value));
		}

		bool IDictionary.Contains (object key)
		{
			return ContainsKey (ToKey (key));
		}

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

		bool IDictionary.IsFixedSize {
			get { return false; }
		}

		bool IDictionary.IsReadOnly {
			get { return false; }
		}

		ICollection IDictionary.Keys  {
			get { return GetKeyCollection (); }
		}

		void IDictionary.Remove (object key)
		{
			Remove (ToKey (key));
		}
		ICollection IDictionary.Values {
			get { return GetValueCollection (); }
		}

		object IDictionary.this [object key] {
			get {
				return this [ToKey (key)];
			}
			set {
				if (!(value is TValue))
					throw new ArgumentException ();

				this [ToKey (key)] = ToValue (value);
			}
		}

		#endregion

		#region ICollection Member

		void ICollection.CopyTo (Array array, int index)
		{
			if (array == null)
				throw new ArgumentNullException ();
			if (index < 0 || array.Length <= index)
				throw new ArgumentOutOfRangeException ();
			if (array.Length - index < _size)
				throw new ArgumentException ();

			for (int i = 0; i < _size; i ++) {
				array.SetValue (new KeyValuePair<TKey,TValue> (_keys [i], _values [i]), i);
			}
		}

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

		// TODO:Is this correct? If this is wrong,please fix.
		object ICollection.SyncRoot {
			get { return this; }
		}

		#endregion

		#region IEnumerable Member

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

		#endregion

		#region IEnumerable<TKey> Member

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

		#endregion

		[Serializable]
		public sealed class ValueCollection : ICollection<TValue>,
			IEnumerable<TValue>, ICollection, IEnumerable
		{
			SortedDictionary<TKey,TValue> _dic;

			public ValueCollection (SortedDictionary<TKey,TValue> dic)
			{
				_dic = dic;
			}

			void ICollection<TValue>.Add (TValue item)
			{
				throw new NotSupportedException ();
			}

			void ICollection<TValue>.Clear ()
			{
				throw new NotSupportedException ();
			}

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

			public void CopyTo (TValue [] array, int arrayIndex)
			{
				if (array == null)
					throw new ArgumentNullException ();
				if (arrayIndex < 0 || array.Length <= arrayIndex)
					throw new ArgumentOutOfRangeException ();
				if (array.Length - arrayIndex < _dic._size)
					throw new ArgumentException ();
				Array.Copy (_dic._values, 0, array, arrayIndex, _dic._size);
			}

			public int Count {
				get { return _dic._size; }
			}

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

			bool ICollection<TValue>.Remove (TValue item)
			{
				throw new NotSupportedException ();
			}

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

			public Enumerator GetEnumerator ()
			{
				return new Enumerator (_dic);
			}
		
			void ICollection.CopyTo(Array array, int index)
			{
				if (array == null)
					throw new ArgumentNullException ();
				if (index < 0 || array.Length <= index)
					throw new ArgumentOutOfRangeException ();
				if (array.Length - index < _dic._size)
					throw new ArgumentException ();
				Array.Copy (_dic._values, 0, array, index, _dic._size);
			}

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

			object ICollection.SyncRoot {
				get { return _dic; }
			}

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

			public struct Enumerator : IEnumerator<TValue>,IEnumerator, IDisposable
			{
				SortedDictionary<TKey,TValue> _dic;
				int _version;
				int _index;
				int _count;

				internal Enumerator (SortedDictionary<TKey,TValue> dic)
				{
					_dic = dic;
					_version = dic._version;
					_index = -1;
					_count = dic._size;
				}

				public TValue Current {
					get {
						if (_count <= _index)
							throw new InvalidOperationException ();
						return _dic._values [_index];
					}
				}

				public bool MoveNext ()
				{
					if (_version != _dic._version)
						throw new InvalidOperationException ();

					if (_index + 1 < _count) {
						_index ++;
						return true;
					}

					return false;
				}

				public void Dispose ()
				{
					_dic = null;
				}

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

				void IEnumerator.Reset ()
				{
					if (_version != _dic._version)
						throw new InvalidOperationException ();
					_index = -1;
				}
			}
		}

		[Serializable]
		public sealed class KeyCollection : ICollection<TKey>,
			IEnumerable<TKey>, ICollection, IEnumerable
		{
			SortedDictionary<TKey,TValue> _dic;

			public KeyCollection (SortedDictionary<TKey,TValue> dic)
			{
				_dic = dic;
			}

			void ICollection<TKey>.Add (TKey item)
			{
				throw new NotSupportedException ();
			}

			void ICollection<TKey>.Clear ()
			{
				throw new NotSupportedException ();
			}

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

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

			public void CopyTo (TKey [] array, int arrayIndex)
			{
				Array.Copy (_dic._keys, 0, array, arrayIndex, _dic._size);
			}

			public int Count {
				get { return _dic._size; }
			}

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

			bool ICollection<TKey>.Remove (TKey item)
			{
				throw new NotSupportedException ();
			}

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

			void ICollection.CopyTo (Array array, int index)
			{
				if (array == null)
					throw new ArgumentNullException ();
				if (index < 0 || array.Length <= index)
					throw new ArgumentOutOfRangeException ();
				if (array.Length - index < _dic._size)
					throw new ArgumentException ();
				Array.Copy (_dic._keys, 0, array, index, _dic._size);
			}

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

			object ICollection.SyncRoot {
				get { return _dic; }
			}

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

			public struct Enumerator : IEnumerator<TKey>, IEnumerator, IDisposable
			{
				SortedDictionary<TKey,TValue> _dic;
				int _version;
				int _index;
				int _count;

				internal Enumerator (SortedDictionary<TKey,TValue> dic)
				{
					_dic = dic;
					_version = dic._version;
					_index = -1;
					_count = dic._size;
				}

				public TKey Current {
					get {
						if (_count <= _index)
							throw new InvalidOperationException ();
						return _dic._keys [_index];
					}
				}

				public bool MoveNext ()
				{
					if (_version != _dic._version)
						throw new InvalidOperationException ();

					if (_index + 1 < _count) {
						_index ++;
						return true;
					}

					return false;
				}

				public void Dispose ()
				{
					_dic = null;
				}

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

				void IEnumerator.Reset ()
				{
					if (_version != _dic._version)
						throw new InvalidOperationException ();
					_index = -1;
				}
			}

		}

		public struct Enumerator : IEnumerator<KeyValuePair<TKey,TValue>>, IDisposable , IDictionaryEnumerator, IEnumerator
		{
			SortedDictionary<TKey,TValue> _dic;
			int _version;
			int _index;
			int _count;

			internal Enumerator (SortedDictionary<TKey,TValue> dic)
			{
				_dic = dic;
				_version = dic._version;
				_index = -1;
				_count = dic._size;
			}

			public KeyValuePair<TKey,TValue> Current {
				get {
					if (_count <= _index)
						throw new InvalidOperationException ();
					return new KeyValuePair<TKey,TValue> (_dic._keys [_index], _dic._values [_index]);
				}
			}

			public bool MoveNext ()
			{
				if (_version != _dic._version)
					throw new InvalidOperationException ();

				if (_index + 1 < _count) {
					_index ++;
					return true;
				}

				return false;
			}

			public void Dispose ()
			{
				_dic = null;
			}

			DictionaryEntry IDictionaryEnumerator.Entry {
				get {
					if (_count <= _index)
						throw new InvalidOperationException ();
					return new DictionaryEntry (_dic._keys [_index], _dic._values [_index]);
				}
			}

			object IDictionaryEnumerator.Key {
				get {
					if (_count <= _index)
						throw new InvalidOperationException ();
					return _dic._keys [_index];
				}
			}

			object IDictionaryEnumerator.Value {
				get {
					if (_count <= _index)
						throw new InvalidOperationException ();
					return _dic._values [_index];
				}
			}

			object IEnumerator.Current {
				get {
					if (_count <= _index)
						throw new InvalidOperationException ();
					return new DictionaryEntry (_dic._keys [_index], _dic._values [_index]);
				}
			}

			void IEnumerator.Reset ()
			{
				if (_version != _dic._version)
					throw new InvalidOperationException ();

				_index = -1;
			}
		}
	}
}
#endif