2 // System.Collections.Generic.List
5 // Ben Maurer (bmaurer@ximian.com)
6 // Martin Baulig (martin@ximian.com)
7 // Carlos Alberto Cortez (calberto.cortez@gmail.com)
8 // David Waite (mass@akuma.org)
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
11 // Copyright (C) 2005 David Waite
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Collections.ObjectModel;
36 using System.Runtime.InteropServices;
38 namespace System.Collections.Generic {
40 public class List <T> : IList <T>, IList, ICollection {
45 static readonly T [] EmptyArray = new T [0];
46 const int DefaultCapacity = 4;
53 public List (IEnumerable <T> collection)
55 CheckCollection (collection);
57 // initialize to needed size (if determinable)
58 ICollection <T> c = collection as ICollection <T>;
62 AddEnumerable (collection);
66 _items = new T [c.Count];
71 public List (int capacity)
74 throw new ArgumentOutOfRangeException ("capacity");
75 _items = new T [capacity];
78 internal List (T [] data, int size)
83 public void Add (T item)
86 _items [_size ++] = item;
90 void GrowIfNeeded (int newCount)
92 int minimumSize = _size + newCount;
93 if (minimumSize > _items.Length)
94 Capacity = Math.Max (Math.Max (Capacity * 2, DefaultCapacity), minimumSize);
97 void CheckRange (int idx, int count)
100 throw new ArgumentOutOfRangeException ("index");
103 throw new ArgumentOutOfRangeException ("count");
105 if ((uint) idx + (uint) count > (uint) _size)
106 throw new ArgumentException ("index and count exceed length of list");
109 void AddCollection (ICollection <T> collection)
111 int collectionCount = collection.Count;
112 GrowIfNeeded (collectionCount);
113 collection.CopyTo (_items, _size);
114 _size += collectionCount;
116 void AddEnumerable (IEnumerable <T> enumerable)
118 foreach (T t in enumerable)
124 public void AddRange (IEnumerable <T> collection)
126 CheckCollection (collection);
128 ICollection <T> c = collection as ICollection <T>;
132 AddEnumerable (collection);
136 public ReadOnlyCollection <T> AsReadOnly ()
138 return new ReadOnlyCollection <T> (this);
141 public int BinarySearch (T item)
143 return Array.BinarySearch <T> (_items, 0, _size, item);
146 public int BinarySearch (T item, IComparer <T> comparer)
148 return Array.BinarySearch <T> (_items, 0, _size, item, comparer);
151 public int BinarySearch (int index, int count, T item, IComparer <T> comparer)
153 CheckRange (index, count);
154 return Array.BinarySearch <T> (_items, index, count, item, comparer);
159 Array.Clear (_items, 0, _items.Length);
164 public bool Contains (T item)
166 EqualityComparer<T> equalityComparer = EqualityComparer<T>.Default;
167 for (int i = 0; i < _size; i++)
168 if (equalityComparer.Equals (_items[i], item))
173 public List <TOutput> ConvertAll <TOutput> (Converter <T, TOutput> converter)
175 if (converter == null)
176 throw new ArgumentNullException ("converter");
177 List <TOutput> u = new List <TOutput> (_size);
178 foreach (T t in this)
179 u.Add (converter (t));
183 public void CopyTo (T [] array)
185 Array.Copy (_items, 0, array, 0, _size);
188 public void CopyTo (T [] array, int arrayIndex)
190 Array.Copy (_items, 0, array, arrayIndex, _size);
193 public void CopyTo (int index, T [] array, int arrayIndex, int count)
195 CheckRange (index, count);
196 Array.Copy (_items, index, array, arrayIndex, count);
199 public bool Exists (Predicate <T> match)
201 return FindIndex (match) != -1;
204 public T Find (Predicate <T> match)
206 int i = FindIndex (match);
207 return (i != -1) ? _items [i] : default (T);
209 void CheckMatch (Predicate <T> match)
212 throw new ArgumentNullException ("match");
215 // Maybe we could make this faster. For example, you could
216 // make a bit set with stackalloc for which elements to copy
217 // then you could size the array correctly.
218 public List <T> FindAll (Predicate <T> match)
221 List <T> f = new List <T> ();
223 foreach (T t in this)
230 public int FindIndex (Predicate <T> match)
233 return GetIndex (0, _size, match);
236 public int FindIndex (int startIndex, Predicate <T> match)
239 CheckIndex (startIndex);
240 return GetIndex (startIndex, _size - startIndex, match);
242 public int FindIndex (int startIndex, int count, Predicate <T> match)
245 CheckRange (startIndex, count);
246 return GetIndex (startIndex, count, match);
248 int GetIndex (int startIndex, int count, Predicate <T> match)
250 for (int i = startIndex; i < startIndex + count; i ++)
251 if (match (_items [i]))
257 public T FindLast (Predicate <T> match)
260 int i = GetLastIndex (0, _size, match);
261 return i == -1 ? default (T) : this [i];
264 public int FindLastIndex (Predicate <T> match)
267 return GetLastIndex (0, _size, match);
270 public int FindLastIndex (int startIndex, Predicate <T> match)
273 CheckIndex (startIndex);
274 return GetLastIndex (0, startIndex + 1, match);
277 public int FindLastIndex (int startIndex, int count, Predicate <T> match)
280 int start = startIndex - count + 1;
281 CheckRange (start, count);
282 return GetLastIndex (start, count, match);
285 int GetLastIndex (int startIndex, int count, Predicate <T> match)
287 // unlike FindLastIndex, takes regular params for search range
288 for (int i = startIndex + count; i != startIndex;)
289 if (match (_items [--i]))
294 public void ForEach (Action <T> action)
297 throw new ArgumentNullException ("action");
298 foreach (T t in this)
302 public Enumerator GetEnumerator ()
304 return new Enumerator (this);
307 public List <T> GetRange (int index, int count)
309 CheckRange (index, count);
310 T [] tmpArray = new T [count];
311 Array.Copy (_items, index, tmpArray, 0, count);
312 return new List <T> (tmpArray, count);
315 public int IndexOf (T item)
317 return Array.IndexOf<T> (_items, item, 0, _size);
320 public int IndexOf (T item, int index)
323 return Array.IndexOf<T> (_items, item, index, _size - index);
326 public int IndexOf (T item, int index, int count)
329 throw new ArgumentOutOfRangeException ("index");
332 throw new ArgumentOutOfRangeException ("count");
334 if ((uint) index + (uint) count > (uint) _size)
335 throw new ArgumentOutOfRangeException ("index and count exceed length of list");
337 return Array.IndexOf<T> (_items, item, index, count);
340 void Shift (int start, int delta)
345 Array.Copy (_items, start, _items, start + delta, _size - start);
350 void CheckIndex (int index)
352 if (index < 0 || (uint) index > (uint) _size)
353 throw new ArgumentOutOfRangeException ("index");
356 public void Insert (int index, T item)
365 void CheckCollection (IEnumerable <T> collection)
367 if (collection == null)
368 throw new ArgumentNullException ("collection");
371 public void InsertRange (int index, IEnumerable <T> collection)
373 CheckCollection (collection);
375 ICollection <T> c = collection as ICollection <T>;
377 InsertCollection (index, c);
379 InsertEnumeration (index, collection);
383 void InsertCollection (int index, ICollection <T> collection)
385 int collectionCount = collection.Count;
386 GrowIfNeeded (collectionCount);
388 Shift (index, collectionCount);
389 collection.CopyTo (_items, index);
391 void InsertEnumeration (int index, IEnumerable <T> enumerable)
393 foreach (T t in enumerable)
397 public int LastIndexOf (T item)
399 return Array.LastIndexOf<T> (_items, item, _size - 1, _size);
402 public int LastIndexOf (T item, int index)
405 return Array.LastIndexOf<T> (_items, item, index, index + 1);
408 public int LastIndexOf (T item, int index, int count)
411 throw new ArgumentOutOfRangeException ("index", index, "index is negative");
414 throw new ArgumentOutOfRangeException ("count", count, "count is negative");
416 if (index - count + 1 < 0)
417 throw new ArgumentOutOfRangeException ("cound", count, "count is too large");
419 return Array.LastIndexOf<T> (_items, item, index, count);
422 public bool Remove (T item)
424 int loc = IndexOf (item);
431 // FIXME: this could probably be made faster.
432 public int RemoveAll (Predicate <T> match)
438 while ((index = GetIndex (index, _size - index, match)) != -1) {
443 Array.Clear (_items, _size, c);
447 public void RemoveAt (int index)
451 Array.Clear (_items, _size, 0);
455 public void RemoveRange (int index, int count)
457 CheckRange (index, count);
459 Shift (index, -count);
460 Array.Clear (_items, _size, count);
465 public void Reverse ()
467 Array.Reverse (_items, 0, _size);
470 public void Reverse (int index, int count)
472 CheckRange (index, count);
473 Array.Reverse (_items, index, count);
479 Array.Sort<T> (_items, 0, _size, Comparer <T>.Default);
482 public void Sort (IComparer <T> comparer)
484 Array.Sort<T> (_items, 0, _size, comparer);
488 public void Sort (Comparison <T> comparison)
490 Array.Sort<T> (_items, _size, comparison);
494 public void Sort (int index, int count, IComparer <T> comparer)
496 CheckRange (index, count);
497 Array.Sort<T> (_items, index, count, comparer);
501 public T [] ToArray ()
503 T [] t = new T [_size];
504 Array.Copy (_items, t, _size);
509 public void TrimExcess ()
514 public bool TrueForAll (Predicate <T> match)
518 foreach (T t in this)
525 public int Capacity {
527 return _items.Length;
530 if ((uint) value < (uint) _size)
531 throw new ArgumentOutOfRangeException ();
533 Array.Resize (ref _items, value);
538 get { return _size; }
541 public T this [int index] {
543 if ((uint) index >= (uint) _size)
544 throw new ArgumentOutOfRangeException ("index");
545 return _items [index];
549 _items [index] = value;
553 #region Interface implementations.
554 IEnumerator <T> IEnumerable <T>.GetEnumerator ()
556 return GetEnumerator ();
559 void ICollection.CopyTo (Array array, int arrayIndex)
561 Array.Copy (_items, 0, array, arrayIndex, _size);
564 IEnumerator IEnumerable.GetEnumerator ()
566 return GetEnumerator ();
569 int IList.Add (object item)
575 bool IList.Contains (object item)
577 return Contains ((T) item);
580 int IList.IndexOf (object item)
582 return IndexOf ((T) item);
585 void IList.Insert (int index, object item)
587 Insert (index, (T) item);
590 void IList.Remove (object item)
595 bool ICollection <T>.IsReadOnly {
596 get { return false; }
598 bool ICollection.IsSynchronized {
599 get { return false; }
602 object ICollection.SyncRoot {
605 bool IList.IsFixedSize {
606 get { return false; }
609 bool IList.IsReadOnly {
610 get { return false; }
613 object IList.this [int index] {
614 get { return this [index]; }
615 set { this [index] = (T) value; }
620 public struct Enumerator : IEnumerator <T>, IDisposable {
621 const int NOT_STARTED = -2;
623 // this MUST be -1, because we depend on it in move next.
624 // we just decr the size, so, 0 - 1 == FINISHED
625 const int FINISHED = -1;
631 internal Enumerator (List <T> l)
638 // for some fucked up reason, MSFT added a useless dispose to this class
639 // It means that in foreach, we must still do a try/finally. Broken, very
641 public void Dispose ()
646 public bool MoveNext ()
648 if (ver != l._version)
649 throw new InvalidOperationException ("Collection was modified;"
650 + "enumeration operation may not execute.");
652 if (idx == NOT_STARTED)
655 return idx != FINISHED && -- idx != FINISHED;
661 throw new InvalidOperationException ();
663 return l._items [l._size - 1 - idx];
667 void IEnumerator.Reset ()
669 if (ver != l._version)
670 throw new InvalidOperationException ("Collection was modified;"
671 + "enumeration operation may not execute.");
676 object IEnumerator.Current {
677 get { return Current; }