File: List.cs

package info (click to toggle)
mono 2.6.7-5.1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 327,344 kB
  • ctags: 413,649
  • sloc: cs: 2,471,883; xml: 1,768,594; ansic: 350,665; sh: 13,644; makefile: 8,640; perl: 1,784; asm: 717; cpp: 209; python: 146; sql: 81; sed: 16
file content (816 lines) | stat: -rw-r--r-- 18,129 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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
//
// System.Collections.Generic.List
//
// Authors:
//    Ben Maurer (bmaurer@ximian.com)
//    Martin Baulig (martin@ximian.com)
//    Carlos Alberto Cortez (calberto.cortez@gmail.com)
//    David Waite (mass@akuma.org)
//
// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
// Copyright (C) 2005 David Waite
//
// 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.Collections.ObjectModel;
using System.Runtime.InteropServices;

namespace System.Collections.Generic {
	[Serializable]
	public class List <T> : IList <T>, IList, ICollection {
		T [] _items;
		int _size;
		int _version;
		
		static readonly T [] EmptyArray = new T [0]; 
		const int DefaultCapacity = 4;
		
		public List ()
		{
			_items = EmptyArray;
		}
		
		public List (IEnumerable <T> collection)
		{
			CheckCollection (collection);

			// initialize to needed size (if determinable)
			ICollection <T> c = collection as ICollection <T>;
			if (c == null)
			{
				_items = EmptyArray;
				AddEnumerable (collection);
			}
			else
			{
				_items = new T [c.Count];
				AddCollection (c);
			}
		}
		
		public List (int capacity)
		{
			if (capacity < 0)
				throw new ArgumentOutOfRangeException ("capacity");
			_items = new T [capacity];
		}
		
		internal List (T [] data, int size)
		{
			_items = data;
			_size = size;
		}
		public void Add (T item)
		{
			// If we check to see if we need to grow before trying to grow
			// we can speed things up by 25%
			if (_size == _items.Length)
				GrowIfNeeded (1);
			_items [_size ++] = item;
			_version++;
		}
		
		void GrowIfNeeded (int newCount)
		{
			int minimumSize = _size + newCount;
			if (minimumSize > _items.Length)
				Capacity = Math.Max (Math.Max (Capacity * 2, DefaultCapacity), minimumSize);
		}
		
		void CheckRange (int idx, int count)
		{
			if (idx < 0)
				throw new ArgumentOutOfRangeException ("index");
			
			if (count < 0)
				throw new ArgumentOutOfRangeException ("count");

			if ((uint) idx + (uint) count > (uint) _size)
				throw new ArgumentException ("index and count exceed length of list");
		}
		
		void AddCollection (ICollection <T> collection)
		{
			int collectionCount = collection.Count;
			if (collectionCount == 0)
				return;

			GrowIfNeeded (collectionCount);			 
			collection.CopyTo (_items, _size);
			_size += collectionCount;
		}

		void AddEnumerable (IEnumerable <T> enumerable)
		{
			foreach (T t in enumerable)
			{
				Add (t);
			}
		}

		public void AddRange (IEnumerable <T> collection)
		{
			CheckCollection (collection);
			
			ICollection <T> c = collection as ICollection <T>;
			if (c != null)
				AddCollection (c);
			else
				AddEnumerable (collection);
			_version++;
		}
		
		public ReadOnlyCollection <T> AsReadOnly ()
		{
			return new ReadOnlyCollection <T> (this);
		}
		
		public int BinarySearch (T item)
		{
			return Array.BinarySearch <T> (_items, 0, _size, item);
		}
		
		public int BinarySearch (T item, IComparer <T> comparer)
		{
			return Array.BinarySearch <T> (_items, 0, _size, item, comparer);
		}
		
		public int BinarySearch (int index, int count, T item, IComparer <T> comparer)
		{
			CheckRange (index, count);
			return Array.BinarySearch <T> (_items, index, count, item, comparer);
		}
		
		public void Clear ()
		{
			Array.Clear (_items, 0, _items.Length);
			_size = 0;
			_version++;
		}
		
		public bool Contains (T item)
		{
			return Array.IndexOf<T>(_items, item, 0, _size) != -1;
		}
		
		public List <TOutput> ConvertAll <TOutput> (Converter <T, TOutput> converter)
		{
			if (converter == null)
				throw new ArgumentNullException ("converter");
			List <TOutput> u = new List <TOutput> (_size);
			for (int i = 0; i < _size; i++)
				u._items[i] = converter(_items[i]);

			u._size = _size;
			return u;
		}
		
		public void CopyTo (T [] array)
		{
			Array.Copy (_items, 0, array, 0, _size);
		}
		
		public void CopyTo (T [] array, int arrayIndex)
		{
			Array.Copy (_items, 0, array, arrayIndex, _size);
		}
		
		public void CopyTo (int index, T [] array, int arrayIndex, int count)
		{
			CheckRange (index, count);
			Array.Copy (_items, index, array, arrayIndex, count);
		}

		public bool Exists (Predicate <T> match)
		{
			CheckMatch(match);
			return GetIndex(0, _size, match) != -1;
		}
		
		public T Find (Predicate <T> match)
		{
			CheckMatch(match);
			int i = GetIndex(0, _size, match);
			return (i != -1) ? _items [i] : default (T);
		}
		
		static void CheckMatch (Predicate <T> match)
		{
			if (match == null)
				throw new ArgumentNullException ("match");
		}
		
		public List <T> FindAll (Predicate <T> match)
		{
			CheckMatch (match);
			if (this._size <= 0x10000) // <= 8 * 1024 * 8 (8k in stack)
				return this.FindAllStackBits (match);
			else 
				return this.FindAllList (match);
		}
		
		private List <T> FindAllStackBits (Predicate <T> match)
		{
			unsafe
			{
				uint *bits = stackalloc uint [(this._size / 32) + 1];
				uint *ptr = bits;
				int found = 0;
				uint bitmask = 0x80000000;
				
				for (int i = 0; i < this._size; i++)
				{
					if (match (this._items [i]))
					{
						(*ptr) = (*ptr) | bitmask;
						found++;
					}
					
					bitmask = bitmask >> 1;
					if (bitmask == 0)
					{
						ptr++;
						bitmask = 0x80000000;
					}
				}
			    	
				T [] results = new T [found];
				bitmask = 0x80000000;
				ptr = bits;
				int j = 0;
				for (int i = 0; i < this._size && j < found; i++)
				{
					if (((*ptr) & bitmask) == bitmask)
						results [j++] = this._items [i];
					
					bitmask = bitmask >> 1;
					if (bitmask == 0)
					{
						ptr++;
						bitmask = 0x80000000;
					}
				}
				
				return new List <T> (results, found);
			}
		}
		
		private List <T> FindAllList (Predicate <T> match)
		{
			List <T> results = new List <T> ();
			for (int i = 0; i < this._size; i++)
				if (match (this._items [i]))
					results.Add (this._items [i]);
			
			return results;
		}
		
		public int FindIndex (Predicate <T> match)
		{
			CheckMatch (match);
			return GetIndex (0, _size, match);
		}
		
		public int FindIndex (int startIndex, Predicate <T> match)
		{
			CheckMatch (match);
			CheckIndex (startIndex);
			return GetIndex (startIndex, _size - startIndex, match);
		}
		public int FindIndex (int startIndex, int count, Predicate <T> match)
		{
			CheckMatch (match);
			CheckRange (startIndex, count);
			return GetIndex (startIndex, count, match);
		}
		int GetIndex (int startIndex, int count, Predicate <T> match)
		{
			int end = startIndex + count;
			for (int i = startIndex; i < end; i ++)
				if (match (_items [i]))
					return i;
				
			return -1;
		}
		
		public T FindLast (Predicate <T> match)
		{
			CheckMatch (match);
			int i = GetLastIndex (0, _size, match);
			return i == -1 ? default (T) : this [i];
		}
		
		public int FindLastIndex (Predicate <T> match)
		{
			CheckMatch (match);
			return GetLastIndex (0, _size, match);
		}
		
		public int FindLastIndex (int startIndex, Predicate <T> match)
		{
			CheckMatch (match);
			CheckIndex (startIndex);
			return GetLastIndex (0, startIndex + 1, match);
		}
		
		public int FindLastIndex (int startIndex, int count, Predicate <T> match)
		{
			CheckMatch (match);
			int start = startIndex - count + 1;
			CheckRange (start, count);
			return GetLastIndex (start, count, match);
		}

		int GetLastIndex (int startIndex, int count, Predicate <T> match)
		{
			// unlike FindLastIndex, takes regular params for search range
			for (int i = startIndex + count; i != startIndex;)
				if (match (_items [--i]))
					return i;
			return -1;	
		}
		
		public void ForEach (Action <T> action)
		{
			if (action == null)
				throw new ArgumentNullException ("action");
			for(int i=0; i < _size; i++)
				action(_items[i]);
		}
		
		public Enumerator GetEnumerator ()
		{
			return new Enumerator (this);
		}
		
		public List <T> GetRange (int index, int count)
		{
			CheckRange (index, count);
			T [] tmpArray = new T [count];
			Array.Copy (_items, index, tmpArray, 0, count);
			return new List <T> (tmpArray, count);
		}
		
		public int IndexOf (T item)
		{
			return Array.IndexOf<T> (_items, item, 0, _size);
		}
		
		public int IndexOf (T item, int index)
		{
			CheckIndex (index);
			return Array.IndexOf<T> (_items, item, index, _size - index);
		}
		
		public int IndexOf (T item, int index, int count)
		{
			if (index < 0)
				throw new ArgumentOutOfRangeException ("index");
			
			if (count < 0)
				throw new ArgumentOutOfRangeException ("count");

			if ((uint) index + (uint) count > (uint) _size)
				throw new ArgumentOutOfRangeException ("index and count exceed length of list");

			return Array.IndexOf<T> (_items, item, index, count);
		}
		
		void Shift (int start, int delta)
		{
			if (delta < 0)
				start -= delta;
			
			if (start < _size)
				Array.Copy (_items, start, _items, start + delta, _size - start);
			
			_size += delta;

			if (delta < 0)
				Array.Clear (_items, _size, -delta);
		}

		void CheckIndex (int index)
		{
			if (index < 0 || (uint) index > (uint) _size)
				throw new ArgumentOutOfRangeException ("index");
		}
		
		public void Insert (int index, T item)
		{
			CheckIndex (index);			
			if (_size == _items.Length)
				GrowIfNeeded (1);
			Shift (index, 1);
			_items[index] = item;
			_version++;
		}

		void CheckCollection (IEnumerable <T> collection)
		{
			if (collection == null)
				throw new ArgumentNullException ("collection");
		}
		
		public void InsertRange (int index, IEnumerable <T> collection)
		{
			CheckCollection (collection);
			CheckIndex (index);
			if (collection == this) {
				T[] buffer = new T[_size];
				CopyTo (buffer, 0);
				GrowIfNeeded (_size);
				Shift (index, buffer.Length);
				Array.Copy (buffer, 0, _items, index, buffer.Length);
			} else {
				ICollection <T> c = collection as ICollection <T>;
				if (c != null)
					InsertCollection (index, c);
				else
					InsertEnumeration (index, collection);
			}
			_version++;
		}

		void InsertCollection (int index, ICollection <T> collection)
		{
			int collectionCount = collection.Count;
			GrowIfNeeded (collectionCount);
			
			Shift (index, collectionCount);
			collection.CopyTo (_items, index);
		}
		void InsertEnumeration (int index, IEnumerable <T> enumerable)
		{
			foreach (T t in enumerable)
				Insert (index++, t);		
		}

		public int LastIndexOf (T item)
		{
			return Array.LastIndexOf<T> (_items, item, _size - 1, _size);
		}
		
		public int LastIndexOf (T item, int index)
		{
			CheckIndex (index);
			return Array.LastIndexOf<T> (_items, item, index, index + 1);
		}
		
		public int LastIndexOf (T item, int index, int count)
		{
			if (index < 0)
				throw new ArgumentOutOfRangeException ("index", index, "index is negative");

			if (count < 0)
				throw new ArgumentOutOfRangeException ("count", count, "count is negative");

			if (index - count + 1 < 0)
				throw new ArgumentOutOfRangeException ("cound", count, "count is too large");

			return Array.LastIndexOf<T> (_items, item, index, count);
		}
		
		public bool Remove (T item)
		{
			int loc = IndexOf (item);
			if (loc != -1)
				RemoveAt (loc);
			
			return loc != -1;
		}
		
		public int RemoveAll (Predicate <T> match)
		{
			CheckMatch(match);
			int i = 0;
			int j = 0;

			// Find the first item to remove
			for (i = 0; i < _size; i++)
				if (match(_items[i]))
					break;

			if (i == _size)
				return 0;

			_version++;

			// Remove any additional items
			for (j = i + 1; j < _size; j++)
			{
				if (!match(_items[j]))
					_items[i++] = _items[j];
			}
			if (j - i > 0)
				Array.Clear (_items, i, j - i);

			_size = i;
			return (j - i);
		}
		
		public void RemoveAt (int index)
		{
			if (index < 0 || (uint)index >= (uint)_size)
				throw new ArgumentOutOfRangeException("index");
			Shift (index, -1);
			Array.Clear (_items, _size, 1);
			_version++;
		}
		
		public void RemoveRange (int index, int count)
		{
			CheckRange (index, count);
			if (count > 0) {
				Shift (index, -count);
				Array.Clear (_items, _size, count);
				_version++;
			}
		}
		
		public void Reverse ()
		{
			Array.Reverse (_items, 0, _size);
			_version++;
		}
		public void Reverse (int index, int count)
		{
			CheckRange (index, count);
			Array.Reverse (_items, index, count);
			_version++;
		}
		
		public void Sort ()
		{
			Array.Sort<T> (_items, 0, _size, Comparer <T>.Default);
			_version++;
		}
		public void Sort (IComparer <T> comparer)
		{
			Array.Sort<T> (_items, 0, _size, comparer);
			_version++;
		}

		public void Sort (Comparison <T> comparison)
		{
			Array.Sort<T> (_items, _size, comparison);
			_version++;
		}
		
		public void Sort (int index, int count, IComparer <T> comparer)
		{
			CheckRange (index, count);
			Array.Sort<T> (_items, index, count, comparer);
			_version++;
		}

		public T [] ToArray ()
		{
			T [] t = new T [_size];
			Array.Copy (_items, t, _size);
			
			return t;
		}
		
		public void TrimExcess ()
		{
			Capacity = _size;
		}
		
		public bool TrueForAll (Predicate <T> match)
		{
			CheckMatch (match);

			for (int i = 0; i < _size; i++)
				if (!match(_items[i]))
					return false;
				
			return true;
		}
		
		public int Capacity {
			get { 
				return _items.Length;
			}
			set {
				if ((uint) value < (uint) _size)
					throw new ArgumentOutOfRangeException ();
				
				Array.Resize (ref _items, value);
			}
		}
		
		public int Count {
			get { return _size; }
		}
		
		public T this [int index] {
			get {
				if ((uint) index >= (uint) _size)
					throw new ArgumentOutOfRangeException ("index");
				return _items [index];
			}
			set {
				CheckIndex (index);
				if ((uint) index == (uint) _size)
					throw new ArgumentOutOfRangeException ("index");
				_items [index] = value;
			}
		}
		
#region Interface implementations.
		IEnumerator <T> IEnumerable <T>.GetEnumerator ()
		{
			return GetEnumerator ();
		}
		
		void ICollection.CopyTo (Array array, int arrayIndex)
		{
			Array.Copy (_items, 0, array, arrayIndex, _size);
		}
		
		IEnumerator IEnumerable.GetEnumerator ()
		{
			return GetEnumerator ();
		}
		
		int IList.Add (object item)
		{
			try {
				Add ((T) item);
				return _size - 1;
			} catch (NullReferenceException) {
			} catch (InvalidCastException) {
			}
			throw new ArgumentException ("item");
		}
		
		bool IList.Contains (object item)
		{
			try {
				return Contains ((T) item);
			} catch (NullReferenceException) {
			} catch (InvalidCastException) {
			}
			return false;
		}
		
		int IList.IndexOf (object item)
		{
			try {
				return IndexOf ((T) item);
			} catch (NullReferenceException) {
			} catch (InvalidCastException) {
			}
			return -1;
		}
		
		void IList.Insert (int index, object item)
		{
			// We need to check this first because, even if the
			// item is null or not the correct type, we need to
			// return an ArgumentOutOfRange exception if the
			// index is out of range
			CheckIndex (index);
			try {
				Insert (index, (T) item);
				return;
			} catch (NullReferenceException) {
			} catch (InvalidCastException) {
			}
			throw new ArgumentException ("item");
		}
		
		void IList.Remove (object item)
		{
			try {
				Remove ((T) item);
				return;
			} catch (NullReferenceException) {
			} catch (InvalidCastException) {
			}
			// Swallow the exception--if we can't cast to the
			// correct type then we've already "succeeded" in
			// removing the item from the List.
		}
		
		bool ICollection <T>.IsReadOnly {
			get { return false; }
		}
		bool ICollection.IsSynchronized {
			get { return false; }
		}
		
		object ICollection.SyncRoot {
			get { return this; }
		}
		bool IList.IsFixedSize {
			get { return false; }
		}
		
		bool IList.IsReadOnly {
			get { return false; }
		}
		
		object IList.this [int index] {
			get { return this [index]; }
			set {
				try {
					this [index] = (T) value;
					return;
				} catch (NullReferenceException) {
					// can happen when 'value' is null and T is a valuetype
				} catch (InvalidCastException) {
				}
				throw new ArgumentException ("value");
			}
		}
#endregion
				
		[Serializable]
		public struct Enumerator : IEnumerator <T>, IDisposable {
			List <T> l;
			int next;
			int ver;

			T current;

			internal Enumerator (List <T> l)
				: this ()
			{
				this.l = l;
				ver = l._version;
			}
			
			public void Dispose ()
			{
				l = null;
			}

			void VerifyState ()
			{
				if (l == null)
					throw new ObjectDisposedException (GetType ().FullName);
				if (ver != l._version)
					throw new InvalidOperationException (
						"Collection was modified; enumeration operation may not execute.");
			}
			
			public bool MoveNext ()
			{
				VerifyState ();

				if (next < 0)
					return false;

				if (next < l._size) {
					current = l._items [next++];
					return true;
				}

				next = -1;
				return false;
			}
			
			public T Current {
				get { return current; }
			}
			
			void IEnumerator.Reset ()
			{
				VerifyState ();
				next = 0;
			}
			
			object IEnumerator.Current {
				get {
					VerifyState ();
					if (next <= 0)
						throw new InvalidOperationException ();
					return current;
				}
			}
		}
	}
}
#endif