2007-08-20 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / class / corlib / System.Collections.Generic / List.cs
1 //
2 // System.Collections.Generic.List
3 //
4 // Authors:
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)
9 //
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
11 // Copyright (C) 2005 David Waite
12 //
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:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
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.
31 //
32
33 #if NET_2_0
34
35 using System.Collections.ObjectModel;
36 using System.Runtime.InteropServices;
37
38 namespace System.Collections.Generic {
39         [Serializable]
40         public class List <T> : IList <T>, IList, ICollection {
41                 T [] _items;
42                 int _size;
43                 int _version;
44                 
45                 static readonly T [] EmptyArray = new T [0]; 
46                 const int DefaultCapacity = 4;
47                 
48                 public List ()
49                 {
50                         _items = EmptyArray;
51                 }
52                 
53                 public List (IEnumerable <T> collection)
54                 {
55                         CheckCollection (collection);
56
57                         // initialize to needed size (if determinable)
58                         ICollection <T> c = collection as ICollection <T>;
59                         if (c == null)
60                         {
61                                 _items = EmptyArray;
62                                 AddEnumerable (collection);
63                         }
64                         else
65                         {
66                                 _items = new T [c.Count];
67                                 AddCollection (c);
68                         }
69                 }
70                 
71                 public List (int capacity)
72                 {
73                         if (capacity < 0)
74                                 throw new ArgumentOutOfRangeException ("capacity");
75                         _items = new T [capacity];
76                 }
77                 
78                 internal List (T [] data, int size)
79                 {
80                         _items = data;
81                         _size = size;
82                 }
83                 public void Add (T item)
84                 {
85                         // If we check to see if we need to grow before trying to grow
86                         // we can speed things up by 25%
87                         if (_size == _items.Length)
88                                 GrowIfNeeded (1);
89                         _items [_size ++] = item;
90                         _version++;
91                 }
92                 
93                 void GrowIfNeeded (int newCount)
94                 {
95                         int minimumSize = _size + newCount;
96                         if (minimumSize > _items.Length)
97                                 Capacity = Math.Max (Math.Max (Capacity * 2, DefaultCapacity), minimumSize);
98                 }
99                 
100                 void CheckRange (int idx, int count)
101                 {
102                         if (idx < 0)
103                                 throw new ArgumentOutOfRangeException ("index");
104                         
105                         if (count < 0)
106                                 throw new ArgumentOutOfRangeException ("count");
107
108                         if ((uint) idx + (uint) count > (uint) _size)
109                                 throw new ArgumentException ("index and count exceed length of list");
110                 }
111                 
112                 void AddCollection (ICollection <T> collection)
113                 {
114                         int collectionCount = collection.Count;
115                         if (collectionCount == 0)
116                                 return;
117
118                         GrowIfNeeded (collectionCount);                  
119                         collection.CopyTo (_items, _size);
120                         _size += collectionCount;
121                 }
122
123                 void AddEnumerable (IEnumerable <T> enumerable)
124                 {
125                         foreach (T t in enumerable)
126                         {
127                                 Add (t);
128                         }
129                 }
130
131                 public void AddRange (IEnumerable <T> collection)
132                 {
133                         CheckCollection (collection);
134                         
135                         ICollection <T> c = collection as ICollection <T>;
136                         if (c != null)
137                                 AddCollection (c);
138                         else
139                                 AddEnumerable (collection);
140                         _version++;
141                 }
142                 
143                 public ReadOnlyCollection <T> AsReadOnly ()
144                 {
145                         return new ReadOnlyCollection <T> (this);
146                 }
147                 
148                 public int BinarySearch (T item)
149                 {
150                         return Array.BinarySearch <T> (_items, 0, _size, item);
151                 }
152                 
153                 public int BinarySearch (T item, IComparer <T> comparer)
154                 {
155                         return Array.BinarySearch <T> (_items, 0, _size, item, comparer);
156                 }
157                 
158                 public int BinarySearch (int index, int count, T item, IComparer <T> comparer)
159                 {
160                         CheckRange (index, count);
161                         return Array.BinarySearch <T> (_items, index, count, item, comparer);
162                 }
163                 
164                 public void Clear ()
165                 {
166                         Array.Clear (_items, 0, _items.Length);
167                         _size = 0;
168                         _version++;
169                 }
170                 
171                 public bool Contains (T item)
172                 {
173                         return Array.IndexOf<T>(_items, item, 0, _size) != -1;
174                 }
175                 
176                 public List <TOutput> ConvertAll <TOutput> (Converter <T, TOutput> converter)
177                 {
178                         if (converter == null)
179                                 throw new ArgumentNullException ("converter");
180                         List <TOutput> u = new List <TOutput> (_size);
181                         for (int i = 0; i < _size; i++)
182                                 u._items[i] = converter(_items[i]);
183
184                         u._size = _size;
185                         return u;
186                 }
187                 
188                 public void CopyTo (T [] array)
189                 {
190                         Array.Copy (_items, 0, array, 0, _size);
191                 }
192                 
193                 public void CopyTo (T [] array, int arrayIndex)
194                 {
195                         Array.Copy (_items, 0, array, arrayIndex, _size);
196                 }
197                 
198                 public void CopyTo (int index, T [] array, int arrayIndex, int count)
199                 {
200                         CheckRange (index, count);
201                         Array.Copy (_items, index, array, arrayIndex, count);
202                 }
203
204                 public bool Exists (Predicate <T> match)
205                 {
206                         CheckMatch(match);
207                         return GetIndex(0, _size, match) != -1;
208                 }
209                 
210                 public T Find (Predicate <T> match)
211                 {
212                         CheckMatch(match);
213                         int i = GetIndex(0, _size, match);
214                         return (i != -1) ? _items [i] : default (T);
215                 }
216                 
217                 static void CheckMatch (Predicate <T> match)
218                 {
219                         if (match == null)
220                                 throw new ArgumentNullException ("match");
221                 }
222                 
223                 public List <T> FindAll (Predicate <T> match)
224                 {
225                         CheckMatch (match);
226                         if (this._size <= 0x10000) // <= 8 * 1024 * 8 (8k in stack)
227                                 return this.FindAllStackBits (match);
228                         else 
229                                 return this.FindAllList (match);
230                 }
231                 
232                 private List <T> FindAllStackBits (Predicate <T> match)
233                 {
234                         unsafe
235                         {
236                                 uint *bits = stackalloc uint [(this._size / 32) + 1];
237                                 uint *ptr = bits;
238                                 int found = 0;
239                                 uint bitmask = 0x80000000;
240                                 
241                                 for (int i = 0; i < this._size; i++)
242                                 {
243                                         if (match (this._items [i]))
244                                         {
245                                                 (*ptr) = (*ptr) | bitmask;
246                                                 found++;
247                                         }
248                                         
249                                         bitmask = bitmask >> 1;
250                                         if (bitmask == 0)
251                                         {
252                                                 ptr++;
253                                                 bitmask = 0x80000000;
254                                         }
255                                 }
256                                 
257                                 T [] results = new T [found];
258                                 bitmask = 0x80000000;
259                                 ptr = bits;
260                                 int j = 0;
261                                 for (int i = 0; i < this._size && j < found; i++)
262                                 {
263                                         if (((*ptr) & bitmask) == bitmask)
264                                                 results [j++] = this._items [i];
265                                         
266                                         bitmask = bitmask >> 1;
267                                         if (bitmask == 0)
268                                         {
269                                                 ptr++;
270                                                 bitmask = 0x80000000;
271                                         }
272                                 }
273                                 
274                                 return new List <T> (results, found);
275                         }
276                 }
277                 
278                 private List <T> FindAllList (Predicate <T> match)
279                 {
280                         List <T> results = new List <T> ();
281                         for (int i = 0; i < this._size; i++)
282                                 if (match (this._items [i]))
283                                         results.Add (this._items [i]);
284                         
285                         return results;
286                 }
287                 
288                 public int FindIndex (Predicate <T> match)
289                 {
290                         CheckMatch (match);
291                         return GetIndex (0, _size, match);
292                 }
293                 
294                 public int FindIndex (int startIndex, Predicate <T> match)
295                 {
296                         CheckMatch (match);
297                         CheckIndex (startIndex);
298                         return GetIndex (startIndex, _size - startIndex, match);
299                 }
300                 public int FindIndex (int startIndex, int count, Predicate <T> match)
301                 {
302                         CheckMatch (match);
303                         CheckRange (startIndex, count);
304                         return GetIndex (startIndex, count, match);
305                 }
306                 int GetIndex (int startIndex, int count, Predicate <T> match)
307                 {
308                         int end = startIndex + count;
309                         for (int i = startIndex; i < end; i ++)
310                                 if (match (_items [i]))
311                                         return i;
312                                 
313                         return -1;
314                 }
315                 
316                 public T FindLast (Predicate <T> match)
317                 {
318                         CheckMatch (match);
319                         int i = GetLastIndex (0, _size, match);
320                         return i == -1 ? default (T) : this [i];
321                 }
322                 
323                 public int FindLastIndex (Predicate <T> match)
324                 {
325                         CheckMatch (match);
326                         return GetLastIndex (0, _size, match);
327                 }
328                 
329                 public int FindLastIndex (int startIndex, Predicate <T> match)
330                 {
331                         CheckMatch (match);
332                         CheckIndex (startIndex);
333                         return GetLastIndex (0, startIndex + 1, match);
334                 }
335                 
336                 public int FindLastIndex (int startIndex, int count, Predicate <T> match)
337                 {
338                         CheckMatch (match);
339                         int start = startIndex - count + 1;
340                         CheckRange (start, count);
341                         return GetLastIndex (start, count, match);
342                 }
343
344                 int GetLastIndex (int startIndex, int count, Predicate <T> match)
345                 {
346                         // unlike FindLastIndex, takes regular params for search range
347                         for (int i = startIndex + count; i != startIndex;)
348                                 if (match (_items [--i]))
349                                         return i;
350                         return -1;      
351                 }
352                 
353                 public void ForEach (Action <T> action)
354                 {
355                         if (action == null)
356                                 throw new ArgumentNullException ("action");
357                         for(int i=0; i < _size; i++)
358                                 action(_items[i]);
359                 }
360                 
361                 public Enumerator GetEnumerator ()
362                 {
363                         return new Enumerator (this);
364                 }
365                 
366                 public List <T> GetRange (int index, int count)
367                 {
368                         CheckRange (index, count);
369                         T [] tmpArray = new T [count];
370                         Array.Copy (_items, index, tmpArray, 0, count);
371                         return new List <T> (tmpArray, count);
372                 }
373                 
374                 public int IndexOf (T item)
375                 {
376                         return Array.IndexOf<T> (_items, item, 0, _size);
377                 }
378                 
379                 public int IndexOf (T item, int index)
380                 {
381                         CheckIndex (index);
382                         return Array.IndexOf<T> (_items, item, index, _size - index);
383                 }
384                 
385                 public int IndexOf (T item, int index, int count)
386                 {
387                         if (index < 0)
388                                 throw new ArgumentOutOfRangeException ("index");
389                         
390                         if (count < 0)
391                                 throw new ArgumentOutOfRangeException ("count");
392
393                         if ((uint) index + (uint) count > (uint) _size)
394                                 throw new ArgumentOutOfRangeException ("index and count exceed length of list");
395
396                         return Array.IndexOf<T> (_items, item, index, count);
397                 }
398                 
399                 void Shift (int start, int delta)
400                 {
401                         if (delta < 0)
402                                 start -= delta;
403                         
404                         if (start < _size)
405                                 Array.Copy (_items, start, _items, start + delta, _size - start);
406                         
407                         _size += delta;
408                 }
409
410                 void CheckIndex (int index)
411                 {
412                         if (index < 0 || (uint) index > (uint) _size)
413                                 throw new ArgumentOutOfRangeException ("index");
414                 }
415                 
416                 public void Insert (int index, T item)
417                 {
418                         CheckIndex (index);                     
419                         if (_size == _items.Length)
420                                 GrowIfNeeded (1);
421                         Shift (index, 1);
422                         _items[index] = item;
423                         _version++;
424                 }
425
426                 void CheckCollection (IEnumerable <T> collection)
427                 {
428                         if (collection == null)
429                                 throw new ArgumentNullException ("collection");
430                 }
431                 
432                 public void InsertRange (int index, IEnumerable <T> collection)
433                 {
434                         CheckCollection (collection);
435                         CheckIndex (index);
436                         if (collection == this) {
437                                 T[] buffer = new T[_size];
438                                 CopyTo (buffer, 0);
439                                 GrowIfNeeded (_size);
440                                 Shift (index, buffer.Length);
441                                 Array.Copy (buffer, 0, _items, index, buffer.Length);
442                         } else {
443                                 ICollection <T> c = collection as ICollection <T>;
444                                 if (c != null)
445                                         InsertCollection (index, c);
446                                 else
447                                         InsertEnumeration (index, collection);
448                         }
449                         _version++;
450                 }
451
452                 void InsertCollection (int index, ICollection <T> collection)
453                 {
454                         int collectionCount = collection.Count;
455                         GrowIfNeeded (collectionCount);
456                         
457                         Shift (index, collectionCount);
458                         collection.CopyTo (_items, index);
459                 }
460                 void InsertEnumeration (int index, IEnumerable <T> enumerable)
461                 {
462                         foreach (T t in enumerable)
463                                 Insert (index++, t);            
464                 }
465
466                 public int LastIndexOf (T item)
467                 {
468                         return Array.LastIndexOf<T> (_items, item, _size - 1, _size);
469                 }
470                 
471                 public int LastIndexOf (T item, int index)
472                 {
473                         CheckIndex (index);
474                         return Array.LastIndexOf<T> (_items, item, index, index + 1);
475                 }
476                 
477                 public int LastIndexOf (T item, int index, int count)
478                 {
479                         if (index < 0)
480                                 throw new ArgumentOutOfRangeException ("index", index, "index is negative");
481
482                         if (count < 0)
483                                 throw new ArgumentOutOfRangeException ("count", count, "count is negative");
484
485                         if (index - count + 1 < 0)
486                                 throw new ArgumentOutOfRangeException ("cound", count, "count is too large");
487
488                         return Array.LastIndexOf<T> (_items, item, index, count);
489                 }
490                 
491                 public bool Remove (T item)
492                 {
493                         int loc = IndexOf (item);
494                         if (loc != -1)
495                                 RemoveAt (loc);
496                         
497                         return loc != -1;
498                 }
499                 
500                 public int RemoveAll (Predicate <T> match)
501                 {
502                         CheckMatch(match);
503                         int i = 0;
504                         int j = 0;
505
506                         // Find the first item to remove
507                         for (i = 0; i < _size; i++)
508                                 if (match(_items[i]))
509                                         break;
510
511                         if (i == _size)
512                                 return 0;
513
514                         _version++;
515
516                         // Remove any additional items
517                         for (j = i + 1; j < _size; j++)
518                         {
519                                 if (!match(_items[j]))
520                                         _items[i++] = _items[j];
521                         }
522
523                         _size = i;
524                         return (j - i);
525                 }
526                 
527                 public void RemoveAt (int index)
528                 {
529                         if (index < 0 || (uint)index >= (uint)_size)
530                                 throw new ArgumentOutOfRangeException("index");
531                         Shift (index, -1);
532                         Array.Clear (_items, _size, 1);
533                         _version++;
534                 }
535                 
536                 public void RemoveRange (int index, int count)
537                 {
538                         CheckRange (index, count);
539                         if (count > 0) {
540                                 Shift (index, -count);
541                                 Array.Clear (_items, _size, count);
542                                 _version++;
543                         }
544                 }
545                 
546                 public void Reverse ()
547                 {
548                         Array.Reverse (_items, 0, _size);
549                         _version++;
550                 }
551                 public void Reverse (int index, int count)
552                 {
553                         CheckRange (index, count);
554                         Array.Reverse (_items, index, count);
555                         _version++;
556                 }
557                 
558                 public void Sort ()
559                 {
560                         Array.Sort<T> (_items, 0, _size, Comparer <T>.Default);
561                         _version++;
562                 }
563                 public void Sort (IComparer <T> comparer)
564                 {
565                         Array.Sort<T> (_items, 0, _size, comparer);
566                         _version++;
567                 }
568
569                 public void Sort (Comparison <T> comparison)
570                 {
571                         Array.Sort<T> (_items, _size, comparison);
572                         _version++;
573                 }
574                 
575                 public void Sort (int index, int count, IComparer <T> comparer)
576                 {
577                         CheckRange (index, count);
578                         Array.Sort<T> (_items, index, count, comparer);
579                         _version++;
580                 }
581
582                 public T [] ToArray ()
583                 {
584                         T [] t = new T [_size];
585                         Array.Copy (_items, t, _size);
586                         
587                         return t;
588                 }
589                 
590                 public void TrimExcess ()
591                 {
592                         Capacity = _size;
593                 }
594                 
595                 public bool TrueForAll (Predicate <T> match)
596                 {
597                         CheckMatch (match);
598
599                         for (int i = 0; i < _size; i++)
600                                 if (!match(_items[i]))
601                                         return false;
602                                 
603                         return true;
604                 }
605                 
606                 public int Capacity {
607                         get { 
608                                 return _items.Length;
609                         }
610                         set {
611                                 if ((uint) value < (uint) _size)
612                                         throw new ArgumentOutOfRangeException ();
613                                 
614                                 Array.Resize (ref _items, value);
615                         }
616                 }
617                 
618                 public int Count {
619                         get { return _size; }
620                 }
621                 
622                 public T this [int index] {
623                         get {
624                                 if ((uint) index >= (uint) _size)
625                                         throw new ArgumentOutOfRangeException ("index");
626                                 return _items [index];
627                         }
628                         set {
629                                 CheckIndex (index);
630                                 if ((uint) index == (uint) _size)
631                                         throw new ArgumentOutOfRangeException ("index");
632                                 _items [index] = value;
633                         }
634                 }
635                 
636 #region Interface implementations.
637                 IEnumerator <T> IEnumerable <T>.GetEnumerator ()
638                 {
639                         return GetEnumerator ();
640                 }
641                 
642                 void ICollection.CopyTo (Array array, int arrayIndex)
643                 {
644                         Array.Copy (_items, 0, array, arrayIndex, _size);
645                 }
646                 
647                 IEnumerator IEnumerable.GetEnumerator ()
648                 {
649                         return GetEnumerator ();
650                 }
651                 
652                 int IList.Add (object item)
653                 {
654                         try {
655                                 Add ((T) item);
656                         } catch (InvalidCastException) {
657                                 throw new ArgumentException("item");
658                         }
659                         return _size - 1;
660                 }
661                 
662                 bool IList.Contains (object item)
663                 {
664                         try {
665                                 return Contains ((T) item);
666                         } catch (InvalidCastException) {
667                                 return false;
668                         }
669                 }
670                 
671                 int IList.IndexOf (object item)
672                 {
673                         try {
674                                 return IndexOf((T) item);
675                         } catch (InvalidCastException) {
676                                 return -1;
677                         }
678                 }
679                 
680                 void IList.Insert (int index, object item)
681                 {
682                         // We need to check this first because, even if the
683                         // item is null or not the correct type, we need to
684                         // return an ArgumentOutOfRange exception if the
685                         // index is out of range
686                         CheckIndex (index);
687                         try {
688                                 Insert (index, (T) item);
689                         } catch (InvalidCastException) {
690                                 throw new ArgumentException("item");
691                         }
692                 }
693                 
694                 void IList.Remove (object item)
695                 {
696                         try {
697                                 Remove ((T) item);
698                         } catch (InvalidCastException) {
699                                 // Swallow the exception--if we
700                                 // can't cast to the correct type
701                                 // then we've already "succeeded"
702                                 // in removing the item from the
703                                 // List.
704                         }
705                 }
706                 
707                 bool ICollection <T>.IsReadOnly {
708                         get { return false; }
709                 }
710                 bool ICollection.IsSynchronized {
711                         get { return false; }
712                 }
713                 
714                 object ICollection.SyncRoot {
715                         get { return this; }
716                 }
717                 bool IList.IsFixedSize {
718                         get { return false; }
719                 }
720                 
721                 bool IList.IsReadOnly {
722                         get { return false; }
723                 }
724                 
725                 object IList.this [int index] {
726                         get { return this [index]; }
727                         set { this [index] = (T) value; }
728                 }
729 #endregion
730                                 
731                 [Serializable]
732                 public struct Enumerator : IEnumerator <T>, IDisposable {
733                         const int NOT_STARTED = -2;
734                         
735                         // this MUST be -1, because we depend on it in move next.
736                         // we just decr the size, so, 0 - 1 == FINISHED
737                         const int FINISHED = -1;
738                         
739                         List <T> l;
740                         int idx;
741                         int ver;
742                         
743                         internal Enumerator (List <T> l)
744                         {
745                                 this.l = l;
746                                 idx = NOT_STARTED;
747                                 ver = l._version;
748                         }
749                         
750                         // for some fucked up reason, MSFT added a useless dispose to this class
751                         // It means that in foreach, we must still do a try/finally. Broken, very
752                         // broken.
753                         public void Dispose ()
754                         {
755                                 idx = NOT_STARTED;
756                         }
757                         
758                         public bool MoveNext ()
759                         {
760                                 if (ver != l._version)
761                                         throw new InvalidOperationException ("Collection was modified;"
762                                                 + "enumeration operation may not execute.");
763                                 
764                                 if (idx == NOT_STARTED)
765                                         idx = l._size;
766                                 
767                                 return idx != FINISHED && -- idx != FINISHED;
768                         }
769                         
770                         public T Current {
771                                 get {
772                                         if (idx < 0)
773                                                 throw new InvalidOperationException ();
774                                         
775                                         return l._items [l._size - 1 - idx];
776                                 }
777                         }
778                         
779                         void IEnumerator.Reset ()
780                         {
781                                 if (ver != l._version)
782                                         throw new InvalidOperationException ("Collection was modified;"
783                                                 + "enumeration operation may not execute.");
784                                 
785                                 idx = NOT_STARTED;
786                         }
787                         
788                         object IEnumerator.Current {
789                                 get { return Current; }
790                         }
791                 }
792         }
793 }
794 #endif