Make a copy of the old ZipLib
[mono.git] / mcs / class / corlib / System.Collections / ArrayList.cs
1 // ArrayList.cs
2 // 
3 // Implementation of the ECMA ArrayList.
4 //
5 // Copyright (c) 2003 Thong (Tum) Nguyen [tum@veridicus.com]
6 //
7 // http://www.opensource.org/licenses/mit-license.html
8 // 
9 // Permission is hereby granted, free of charge, to any person obtaining a copy of this
10 // software and associated documentation files (the "Software"), to deal in the 
11 // Software without restriction, including without limitation the rights to use, copy,
12 // modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
13 // and to permit persons to whom the Software is furnished to do so, subject to the
14 // following conditions:
15 //
16 // The above copyright notice and this permission notice shall be included in all copies
17 // or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
27 //
28 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
29 //
30 // Permission is hereby granted, free of charge, to any person obtaining
31 // a copy of this software and associated documentation files (the
32 // "Software"), to deal in the Software without restriction, including
33 // without limitation the rights to use, copy, modify, merge, publish,
34 // distribute, sublicense, and/or sell copies of the Software, and to
35 // permit persons to whom the Software is furnished to do so, subject to
36 // the following conditions:
37 // 
38 // The above copyright notice and this permission notice shall be
39 // included in all copies or substantial portions of the Software.
40 // 
41 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
42 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
43 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
44 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
45 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
46 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
47 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
48 //
49
50 using System;
51 using System.Collections;
52 using System.Runtime.InteropServices;
53
54 namespace System.Collections 
55 {
56 #if NET_2_0
57         [ComVisible(true)]
58 #endif
59         [Serializable]
60         public class ArrayList
61                 : IList, ICloneable, ICollection, IEnumerable 
62         {
63                 #region Enumerator
64
65                 private sealed class ArrayListEnumerator
66                         : IEnumerator, ICloneable 
67                 {                       
68                         private int m_Pos;
69                         private int m_Index;
70                         private int m_Count;
71                         private object m_Current;
72                         private ArrayList m_List;
73                         private int m_ExpectedStateChanges;
74
75                         public ArrayListEnumerator(ArrayList list)
76                                 : this(list, 0, list.Count) 
77                         {                               
78                         }
79
80                         public object Clone() 
81                         {
82                                 return this.MemberwiseClone();
83                         }
84
85                         public ArrayListEnumerator(ArrayList list, int index, int count) 
86                         {
87                                 m_List = list;
88                                 m_Index = index;
89                                 m_Count = count;
90                                 m_Pos = m_Index - 1;
91                                 m_Current = null;
92                                 m_ExpectedStateChanges = list._version;
93                         }
94
95                         public object Current 
96                         {
97                                 get 
98                                 {
99                                         if (m_Pos == m_Index - 1) {
100                                                 throw new InvalidOperationException("Enumerator unusable (Reset pending, or past end of array.");
101                                         }
102
103                                         return m_Current;
104                                 }
105                         }
106
107                         public bool MoveNext() 
108                         {
109                                 if (m_List._version != m_ExpectedStateChanges) 
110                                 {
111                                         throw new InvalidOperationException("List has changed.");
112                                 }
113
114                                 m_Pos++;
115
116                                 if (m_Pos - m_Index < m_Count) 
117                                 {
118                                         m_Current = m_List[m_Pos];
119
120                                         return true;
121                                 }
122
123                                 return false;
124                         }
125
126                         public void Reset() 
127                         {
128                                 m_Current = null;
129                                 m_Pos = m_Index - 1;
130                         }
131                 }
132                 
133                 sealed class SimpleEnumerator : IEnumerator, ICloneable
134                 {
135                         ArrayList list;
136                         int index;
137                         int version;
138                         object currentElement;
139                         static object endFlag = new object ();
140                                                         
141                         public SimpleEnumerator (ArrayList list)
142                         {
143                                 this.list = list;
144                                 index = -1;
145                                 version = list._version;
146                                 currentElement = endFlag;
147                         }
148
149                         public object Clone ()
150                         {
151                                 return MemberwiseClone ();
152                         }
153         
154                         public bool MoveNext ()
155                         {
156                                 if (version != list._version)
157                                         throw new InvalidOperationException("List has changed.");
158                                 
159                                 if (++index < list.Count) {
160                                         currentElement = list [index];
161                                         return true;
162                                 } else {
163                                         currentElement = endFlag;
164                                         return false;
165                                 }
166                         }
167         
168                         public object Current {
169                                 get {
170                                         if (currentElement == endFlag) {
171                                                 if (index == -1)
172                                                         throw new InvalidOperationException ("Enumerator not started");
173                                                 else
174                                                         throw new InvalidOperationException ("Enumerator ended");                                                
175                                         }
176                                         
177                                         return currentElement;
178                                 }
179                         }
180         
181                         public void Reset ()
182                         {
183                                 if (version != list._version)
184                                         throw new InvalidOperationException ("List has changed.");
185                                 
186                                 currentElement = endFlag;
187                                 index = -1;
188                         }
189                 }
190
191                 #endregion
192
193                 #region ArrayListAdapter
194
195                 /// <summary>
196                 /// Adapts various ILists into an ArrayList.
197                 /// </summary>
198                 [Serializable]
199                 private sealed class ArrayListAdapter
200                         : ArrayList 
201                 {
202                         private sealed class EnumeratorWithRange
203                                 : IEnumerator, ICloneable 
204                         {
205                                 private int m_StartIndex;
206                                 private int m_Count;
207                                 private int m_MaxCount;
208                                 private IEnumerator m_Enumerator;
209
210                                 public EnumeratorWithRange(IEnumerator enumerator, int index, int count) 
211                                 {
212                                         m_Count = 0;
213                                         m_StartIndex = index;
214                                         m_MaxCount = count;
215                                         m_Enumerator = enumerator;
216
217                                         Reset();
218                                 }
219
220                                 public object Clone() 
221                                 {
222                                         return this.MemberwiseClone();
223                                 }
224
225                                 public object Current 
226                                 {
227                                         get 
228                                         {
229                                                 return m_Enumerator.Current;
230                                         }
231                                 }
232
233                                 public bool MoveNext() 
234                                 {
235                                         if (m_Count >= m_MaxCount) 
236                                         {
237                                                 return false;
238                                         }
239                                         
240                                         m_Count++;
241
242                                         return m_Enumerator.MoveNext();
243                                 }
244
245                                 public void Reset() 
246                                 {
247                                         m_Count = 0;                            
248                                         m_Enumerator.Reset();
249
250                                         for (int i = 0; i < m_StartIndex; i++) 
251                                         {
252                                                 m_Enumerator.MoveNext();
253                                         }
254                                 }
255                         }
256
257                         private IList m_Adaptee;
258
259                         public ArrayListAdapter(IList adaptee)
260                                 : base(0, true) 
261                         {
262                                 m_Adaptee = adaptee;
263                         }
264
265                         public override object this[int index] 
266                         {
267                                 get 
268                                 {
269                                         return m_Adaptee[index];
270                                 }
271
272                                 set 
273                                 {
274                                         m_Adaptee[index] = value;
275                                 }
276                         }       
277
278                         public override int Count 
279                         {
280                                 get 
281                                 {
282                                         return m_Adaptee.Count;
283                                 }
284                         }
285
286                         public override int Capacity 
287                         {
288                                 get 
289                                 {
290                                         return m_Adaptee.Count;
291                                 }
292
293                                 set 
294                                 {
295                                         if (value < m_Adaptee.Count) 
296                                         {
297                                                 throw new ArgumentException("capacity");
298                                         }
299                                 }
300                         }
301
302                         public override bool IsFixedSize 
303                         {
304                                 get 
305                                 {
306                                         return m_Adaptee.IsFixedSize;
307                                 }
308                         }
309
310                         public override bool IsReadOnly 
311                         {
312                                 get 
313                                 {
314                                         return m_Adaptee.IsReadOnly;
315                                 }
316                         }
317
318                         public override object SyncRoot 
319                         {
320                                 get 
321                                 {
322                                         return m_Adaptee.SyncRoot;
323                                 }
324                         }
325
326                         public override int Add(object value) 
327                         {
328                                 return m_Adaptee.Add(value);
329                         }
330
331                         public override void Clear() 
332                         {
333                                 m_Adaptee.Clear();
334                         }
335
336                         public override bool Contains(object value) 
337                         {
338                                 return m_Adaptee.Contains(value);
339                         }
340
341                         public override int IndexOf(object value) 
342                         {
343                                 return m_Adaptee.IndexOf(value);
344                         }
345
346                         public override int IndexOf(object value, int startIndex) 
347                         {
348                                 return IndexOf(value, startIndex, m_Adaptee.Count - startIndex);
349                         }
350
351                         public override int IndexOf(object value, int startIndex, int count) 
352                         {
353                                 if (startIndex < 0 || startIndex > m_Adaptee.Count) 
354                                 {
355                                         throw new ArgumentOutOfRangeException("startIndex", startIndex,
356                                                 "Does not specify valid index.");
357                                 }
358
359                                 if (count < 0) 
360                                 {
361                                         throw new ArgumentOutOfRangeException("count", count,
362                                                 "Can't be less than 0.");
363                                 }
364
365                                 // re-ordered to avoid possible integer overflow
366                                 if (startIndex > m_Adaptee.Count - count) {
367                                         // LAMESPEC: Every other method throws ArgumentException
368                                         throw new ArgumentOutOfRangeException("count",
369                                                 "Start index and count do not specify a valid range.");
370                                 }
371
372                                 if (value == null) 
373                                 {
374                                         for (int i = startIndex; i < startIndex + count; i++) 
375                                         {
376                                                 if (m_Adaptee[i] == null) 
377                                                 {
378                                                         return i;
379                                                 }
380                                         }
381                                 }
382                                 else 
383                                 {
384                                         for (int i = startIndex; i < startIndex + count; i++) 
385                                         {
386                                                 if (value.Equals(m_Adaptee[i])) 
387                                                 {
388                                                         return i;
389                                                 }
390                                         }
391                                 }
392
393                                 return -1;
394                         }
395
396                         public override int LastIndexOf(object value) 
397                         {
398                                 return LastIndexOf(value, m_Adaptee.Count - 1);
399                         }
400
401                         public override int LastIndexOf(object value, int startIndex) 
402                         {
403                                 return LastIndexOf(value, startIndex, startIndex + 1);
404                         }
405
406                         public override int LastIndexOf(object value, int startIndex, int count) 
407                         {
408                                 if (startIndex < 0) 
409                                 {
410                                         throw new ArgumentOutOfRangeException("startIndex", startIndex, "< 0");
411                                 }
412
413                                 if (count < 0) 
414                                 {
415                                         throw new ArgumentOutOfRangeException("count", count, "count is negative.");
416                                 }
417
418                                 if (startIndex - count  + 1 < 0) 
419                                 {
420                                         throw new ArgumentOutOfRangeException("count", count, "count is too large.");
421                                 }
422
423                                 if (value == null) 
424                                 {
425                                         for (int i = startIndex; i > startIndex - count; i--) 
426                                         {
427                                                 if (m_Adaptee[i] == null) 
428                                                 {
429                                                         return i;
430                                                 }
431                                         }
432                                 }
433                                 else 
434                                 {
435                                         for (int i = startIndex; i > startIndex - count; i--) 
436                                         {
437                                                 if (value.Equals(m_Adaptee[i])) 
438                                                 {
439                                                         return i;
440                                                 }
441                                         }
442                                 }
443
444                                 return -1;
445                         }
446
447                         public override void Insert(int index, object value) 
448                         {
449                                 m_Adaptee.Insert(index, value);
450                         }
451
452                         public override void InsertRange(int index, ICollection c) 
453                         {
454                                 if (c == null) 
455                                 {
456                                         throw new ArgumentNullException("c");
457                                 }
458
459                                 if (index > m_Adaptee.Count) 
460                                 {
461                                         throw new ArgumentOutOfRangeException("index", index,
462                                                 "Index must be >= 0 and <= Count.");
463                                 }
464
465                                 foreach (object value in c) 
466                                 {
467                                         m_Adaptee.Insert(index++, value);
468                                 }
469                         }
470
471                         public override void Remove(object value) 
472                         {
473                                 m_Adaptee.Remove(value);
474                         }
475
476                         public override void RemoveAt(int index) 
477                         {
478                                 m_Adaptee.RemoveAt(index);
479                         }
480
481                         public override void RemoveRange(int index, int count) 
482                         {
483                                 CheckRange(index, count, m_Adaptee.Count);
484
485                                 for (int i = 0; i < count; i++) 
486                                 {
487                                         m_Adaptee.RemoveAt(index);
488                                 }                       
489                         }
490
491                         public override void Reverse() 
492                         {
493                                 Reverse(0, m_Adaptee.Count);
494                         }
495
496                         public override void Reverse(int index, int count) 
497                         {
498                                 object tmp;
499
500                                 CheckRange(index, count, m_Adaptee.Count);
501                         
502                                 for (int i = 0; i < count / 2; i++) 
503                                 {
504                                         tmp = m_Adaptee[i + index];
505                                         m_Adaptee[i + index] = m_Adaptee[(index + count) - i + index - 1];
506                                         m_Adaptee[(index + count) - i + index - 1] = tmp;                               
507                                 }
508                         }
509
510                         public override void SetRange(int index, ICollection c) 
511                         {
512                                 if (c == null) 
513                                 {
514                                         throw new ArgumentNullException("c");
515                                 }
516
517                                 if (index < 0 || index + c.Count > m_Adaptee.Count) 
518                                 {
519                                         throw new ArgumentOutOfRangeException("index");
520                                 }
521
522                                 int x = index;
523
524                                 foreach (object value in c) 
525                                 {
526                                         m_Adaptee[x++] = value;
527                                 }
528                         }
529
530                         public override void CopyTo(System.Array array) 
531                         {
532                                 m_Adaptee.CopyTo(array, 0);
533                         }
534
535                         public override void CopyTo(System.Array array, int index) 
536                         {
537                                 m_Adaptee.CopyTo(array, index);
538                         }
539
540                         public override void CopyTo(int index, System.Array array, int arrayIndex, int count) 
541                         {
542                                 if (index < 0) 
543                                 {
544                                         throw new ArgumentOutOfRangeException("index", index,
545                                                 "Can't be less than zero.");
546                                 }
547
548                                 if (arrayIndex < 0) 
549                                 {
550                                         throw new ArgumentOutOfRangeException("arrayIndex", arrayIndex,
551                                                 "Can't be less than zero.");
552                                 }
553
554                                 if (count < 0) 
555                                 {
556                                         throw new ArgumentOutOfRangeException("index", index,
557                                                 "Can't be less than zero.");
558                                 }
559
560                                 if (index >= m_Adaptee.Count) 
561                                 {
562                                         throw new ArgumentException("Can't be more or equal to list count.",
563                                                 "index");
564                                 }
565
566                                 if (array.Rank > 1) 
567                                 {
568                                         throw new ArgumentException("Can't copy into multi-dimensional array.");
569                                 }
570
571                                 if (arrayIndex >= array.Length) 
572                                 {
573                                         throw new ArgumentException("arrayIndex can't be greater than array.Length - 1.");
574                                 }
575
576                                 if (array.Length - arrayIndex + 1 < count) 
577                                 {
578                                         throw new ArgumentException("Destination array is too small.");
579                                 }
580
581                                 // re-ordered to avoid possible integer overflow
582                                 if (index > m_Adaptee.Count - count) {
583                                         throw new ArgumentException("Index and count do not denote a valid range of elements.", "index");
584                                 }
585
586                                 for (int i = 0; i < count; i++) 
587                                 {
588                                         array.SetValue(m_Adaptee[index + i], arrayIndex + i); 
589                                 }
590                         }
591
592                         public override bool IsSynchronized 
593                         {
594                                 get 
595                                 {
596                                         return m_Adaptee.IsSynchronized;
597                                 }
598                         }
599
600                         public override IEnumerator GetEnumerator() 
601                         {
602                                 return m_Adaptee.GetEnumerator();
603                         }
604                         
605                         public override IEnumerator GetEnumerator(int index, int count) 
606                         {
607                                 CheckRange(index, count, m_Adaptee.Count);
608
609                                 return new EnumeratorWithRange(m_Adaptee.GetEnumerator(), index, count);
610                         }
611
612                         public override void AddRange(ICollection c) 
613                         {
614                                 foreach (object value in c) 
615                                 {
616                                         m_Adaptee.Add(value);
617                                 }
618                         }
619
620                         public override int BinarySearch(object value) 
621                         {
622                                 return BinarySearch(value, null);
623                         }
624
625                         public override int BinarySearch(object value, IComparer comparer) 
626                         {
627                                 return BinarySearch(0, m_Adaptee.Count, value, comparer);
628                         }
629
630                         public override int BinarySearch(int index, int count, object value, IComparer comparer) 
631                         {
632                                 int r, x, y, z;
633
634                                 // Doing a direct BinarySearch on the adaptee will perform poorly if the adaptee is a linked-list.
635                                 // Alternatives include copying the adaptee to a temporary array first.
636
637                                 CheckRange(index, count, m_Adaptee.Count);
638
639                                 if (comparer == null) 
640                                 {
641                                         comparer = Comparer.Default;
642                                 }
643
644                                 x = index;
645                                 y = index + count - 1;
646
647                                 while (x <= y) 
648                                 {
649                                         z = (x + y) / 2;
650                                 
651                                         r = comparer.Compare(value, m_Adaptee[z]);
652                                 
653                                         if (r < 0) 
654                                         {
655                                                 y = z - 1;
656                                         }
657                                         else if (r > 0) 
658                                         {
659                                                 x = z + 1;
660                                         }       
661                                         else 
662                                         {
663                                                 return z;
664                                         }
665                                 }
666
667                                 return ~x;
668                         }
669
670                         public override object Clone() 
671                         {
672                                 return new ArrayList.ArrayListAdapter(m_Adaptee);
673                         }
674
675                         public override ArrayList GetRange(int index, int count) 
676                         {
677                                 CheckRange(index, count, m_Adaptee.Count);
678                                 
679                                 return new RangedArrayList(this, index, count);
680                         }
681
682                         public override void TrimToSize() 
683                         {
684                                 // N/A
685                         }
686
687                         public override void Sort() 
688                         {
689                                 Sort(Comparer.Default);
690                         }
691
692                         public override void Sort(IComparer comparer) 
693                         {
694                                 Sort(0, m_Adaptee.Count, comparer);
695                         }
696
697                         public override void Sort(int index, int count, IComparer comparer) 
698                         {
699                                 CheckRange(index, count, m_Adaptee.Count);
700
701                                 if (comparer == null) 
702                                 {
703                                         comparer = Comparer.Default;
704                                 }
705
706                                 // Doing a direct sort on the adaptee will perform poorly if the adaptee is a linked-list.
707                                 // Alternatives include copying the adaptee into a temporary array first.
708
709                                 QuickSort(m_Adaptee, index, index + count - 1, comparer);
710                         }
711
712
713                         /// <summary>
714                         /// Swaps two items in a list at the specified indexes.
715                         /// </summary>
716                         private static void Swap(IList list, int x, int y) 
717                         {
718                                 object tmp;
719                                 
720                                 tmp = list[x];
721                                 list[x] = list[y];
722                                 list[y] = tmp;
723                         }
724
725                         /// <summary>
726                         /// Quicksort for lists.
727                         /// </summary>
728                         /// <remarks>
729                         /// This function acts as both qsort() and partition().
730                         /// </remarks>
731                         internal static void QuickSort(IList list, int left, int right, IComparer comparer) 
732                         {
733                                 int i, j, middle;
734                                 object pivot;
735                                         
736                                 if (left >= right) 
737                                 {
738                                         return;
739                                 }
740
741                                 // Pick the pivot using the median-of-three strategy.
742
743                                 middle = (left + right) / 2;
744
745                                 if (comparer.Compare(list[middle], list[left]) < 0) 
746                                 {
747                                         Swap(list, middle, left);
748                                 }
749
750                                 if (comparer.Compare(list[right], list[left]) < 0) 
751                                 {
752                                         Swap(list, right, left);
753                                 }
754
755                                 if (comparer.Compare(list[right], list[middle]) < 0) 
756                                 {
757                                         Swap(list, right, middle);
758                                 }
759                 
760                                 if (right - left + 1 <= 3) 
761                                 {
762                                         return;
763                                 }
764                 
765                                 // Put the pivot in right - 1.
766                                 Swap(list, right - 1, middle);
767
768                                 // List should look like:
769                                 //
770                                 // [Small] ..Numbers.. [Middle] ..Numbers.. [Pivot][Large]
771
772                                 pivot = list[right - 1];
773
774                                 // Sort from (left + 1) to (right - 2).
775
776                                 i = left;
777                                 j = right - 1;                  
778                 
779                                 for (;;) 
780                                 {
781                                         while (comparer.Compare(list[++i], pivot) < 0);
782                                         while (comparer.Compare(list[--j], pivot) > 0);
783                         
784                                         if (i < j) 
785                                         {
786                                                 Swap(list, i, j);
787                                         }
788                                         else 
789                                         {
790                                                 break;
791                                         }
792                                 }
793
794                                 // Put pivot into the right position (real middle).
795
796                                 Swap(list, right - 1, i);
797
798                                 // Recursively sort the left and right sub lists.
799
800                                 QuickSort(list, left, i - 1, comparer);
801                                 QuickSort(list, i + 1, right, comparer);                
802                         }
803
804                         public override object[] ToArray() 
805                         {
806                                 object[] retval;
807
808                                 retval = new object[m_Adaptee.Count];
809
810                                 m_Adaptee.CopyTo(retval, 0);
811                         
812                                 return retval;
813                         }
814
815                         public override Array ToArray(Type elementType) 
816                         {
817                                 Array retval;
818
819                                 retval = Array.CreateInstance(elementType, m_Adaptee.Count);
820
821                                 m_Adaptee.CopyTo(retval, 0);
822                         
823                                 return retval;
824                         }
825                 }
826
827                 #endregion // ArrayListAdapter
828
829                 //
830                 // ArrayList wrappers
831                 //
832
833                 #region ArrayListWrapper
834
835                 /// <summary>
836                 /// Base wrapper/decorator for ArrayLists.  Simply delegates all methods to
837                 /// the underlying wrappee.
838                 /// </summary>
839                 [Serializable]
840                 private class ArrayListWrapper
841                         : ArrayList 
842                 {               
843                         protected ArrayList m_InnerArrayList;
844
845                         #region Constructors
846
847                         public ArrayListWrapper(ArrayList innerArrayList) 
848                         {                       
849                                 m_InnerArrayList = innerArrayList;
850                         }               
851
852                         #endregion
853
854                         #region Indexers
855
856                         public override object this[int index] 
857                         {
858                                 get 
859                                 {
860                                         return m_InnerArrayList[index];
861                                 }
862
863                                 set 
864                                 {
865                                         m_InnerArrayList[index] = value;
866                                 }
867                         }
868
869                         #endregion
870
871                         #region Properties
872
873                         public override int Count 
874                         {
875                                 get 
876                                 {
877                                         return m_InnerArrayList.Count;
878                                 }
879                         }
880
881                         public override int Capacity 
882                         {
883                                 get 
884                                 {
885                                         return m_InnerArrayList.Capacity;
886                                 }
887
888                                 set 
889                                 {
890                                         m_InnerArrayList.Capacity = value;
891                                 }
892                         }
893
894                         public override bool IsFixedSize 
895                         {
896                                 get 
897                                 {
898                                         return m_InnerArrayList.IsFixedSize;
899                                 }
900                         }
901
902                         public override bool IsReadOnly 
903                         {
904                                 get 
905                                 {
906                                         return m_InnerArrayList.IsReadOnly;
907                                 }
908                         }
909
910                         public override bool IsSynchronized 
911                         {
912                                 get 
913                                 {
914                                         return m_InnerArrayList.IsSynchronized;
915                                 }
916                         }
917
918                         public override object SyncRoot 
919                         {
920                                 get 
921                                 {
922                                         return m_InnerArrayList.SyncRoot;
923                                 }
924                         }
925
926                         #endregion
927
928                         #region Methods
929
930                         public override int Add(object value) 
931                         {
932                                 return m_InnerArrayList.Add(value);
933                         }
934
935                         public override void Clear() 
936                         {
937                                 m_InnerArrayList.Clear();
938                         }
939
940                         public override bool Contains(object value) 
941                         {
942                                 return m_InnerArrayList.Contains(value);
943                         }
944
945                         public override int IndexOf(object value) 
946                         {
947                                 return m_InnerArrayList.IndexOf(value);
948                         }
949
950                         public override int IndexOf(object value, int startIndex) 
951                         {
952                                 return m_InnerArrayList.IndexOf(value, startIndex);
953                         }
954
955                         public override int IndexOf(object value, int startIndex, int count) 
956                         {
957                                 return m_InnerArrayList.IndexOf(value, startIndex, count);
958                         }
959
960                         public override int LastIndexOf(object value) 
961                         {
962                                 return m_InnerArrayList.LastIndexOf(value);
963                         }
964
965                         public override int LastIndexOf(object value, int startIndex) 
966                         {
967                                 return m_InnerArrayList.LastIndexOf(value, startIndex);
968                         }
969
970                         public override int LastIndexOf(object value, int startIndex, int count) 
971                         {
972                                 return m_InnerArrayList.LastIndexOf(value, startIndex, count);
973                         }
974
975                         public override void Insert(int index, object value) 
976                         {
977                                 m_InnerArrayList.Insert(index, value);
978                         }
979
980                         public override void InsertRange(int index, ICollection c) 
981                         {
982                                 m_InnerArrayList.InsertRange(index, c);
983                         }
984
985                         public override void Remove(object value) 
986                         {
987                                 m_InnerArrayList.Remove(value);
988                         }
989
990                         public override void RemoveAt(int index) 
991                         {
992                                 m_InnerArrayList.RemoveAt(index);
993                         }
994
995                         public override void RemoveRange(int index, int count) 
996                         {
997                                 m_InnerArrayList.RemoveRange(index, count);
998                         }
999
1000                         public override void Reverse() 
1001                         {
1002                                 m_InnerArrayList.Reverse();
1003                         }
1004
1005                         public override void Reverse(int index, int count) 
1006                         {
1007                                 m_InnerArrayList.Reverse(index, count);
1008                         }
1009
1010                         public override void SetRange(int index, ICollection c) 
1011                         {
1012                                 m_InnerArrayList.SetRange(index, c);
1013                         }
1014
1015                         public override void CopyTo(System.Array array) 
1016                         {
1017                                 m_InnerArrayList.CopyTo(array);
1018                         }
1019
1020                         public override void CopyTo(System.Array array, int index) 
1021                         {
1022                                 m_InnerArrayList.CopyTo(array, index);
1023                         }
1024
1025                         public override void CopyTo(int index, System.Array array, int arrayIndex, int count) 
1026                         {
1027                                 m_InnerArrayList.CopyTo(index, array, arrayIndex, count);
1028                         }
1029
1030                         public override IEnumerator GetEnumerator() 
1031                         {
1032                                 return m_InnerArrayList.GetEnumerator();
1033                         }
1034
1035                         public override IEnumerator GetEnumerator(int index, int count) 
1036                         {
1037                                 return m_InnerArrayList.GetEnumerator(index, count);
1038                         }
1039
1040                         public override void AddRange(ICollection c) 
1041                         {
1042                                 m_InnerArrayList.AddRange(c);
1043                         }
1044
1045                         public override int BinarySearch(object value) 
1046                         {
1047                                 return m_InnerArrayList.BinarySearch(value);
1048                         }
1049
1050                         public override int BinarySearch(object value, IComparer comparer) 
1051                         {
1052                                 return m_InnerArrayList.BinarySearch(value, comparer);
1053                         }
1054
1055                         public override int BinarySearch(int index, int count, object value, IComparer comparer) 
1056                         {
1057                                 return m_InnerArrayList.BinarySearch(index, count, value, comparer);
1058                         }
1059
1060                         public override object Clone() 
1061                         {
1062                                 return m_InnerArrayList.Clone();
1063                         }
1064
1065                         public override ArrayList GetRange(int index, int count) 
1066                         {
1067                                 return m_InnerArrayList.GetRange(index, count);
1068                         }
1069
1070                         public override void TrimToSize() 
1071                         {
1072                                 m_InnerArrayList.TrimToSize();
1073                         }
1074
1075                         public override void Sort() 
1076                         {
1077                                 m_InnerArrayList.Sort();
1078                         }
1079
1080                         public override void Sort(IComparer comparer) 
1081                         {
1082                                 m_InnerArrayList.Sort(comparer);
1083                         }
1084
1085                         public override void Sort(int index, int count, IComparer comparer) 
1086                         {
1087                                 m_InnerArrayList.Sort(index, count, comparer);
1088                         }
1089
1090                         public override object[] ToArray() 
1091                         {
1092                                 return m_InnerArrayList.ToArray();
1093                         }
1094
1095                         public override Array ToArray(Type elementType) 
1096                         {
1097                                 return m_InnerArrayList.ToArray(elementType);
1098                         }
1099
1100                         #endregion
1101                 }
1102
1103                 #endregion
1104
1105                 #region SynchronizedArrayListWrapper
1106
1107                 /// <summary>
1108                 /// ArrayListWrapper that synchronizes calls to all methods/properties.
1109                 /// </summary>
1110                 /// <remarks>
1111                 /// Works by just synchronizing all method calls.  In the future careful optimisation
1112                 /// could give better performance...
1113                 /// </remarks>
1114                 [Serializable]
1115                 private sealed class SynchronizedArrayListWrapper
1116                         : ArrayListWrapper 
1117                 {
1118                         private object m_SyncRoot;
1119
1120                         #region Constructors
1121
1122                         /// <summary>
1123                         /// Creates a new synchronized wrapper for the given <see cref="ArrayList"/>.
1124                         /// </summary>
1125                         /// <param name="innerArrayList"></param>
1126                         internal SynchronizedArrayListWrapper(ArrayList innerArrayList)
1127                                 : base(innerArrayList) 
1128                         {
1129                                 m_SyncRoot = innerArrayList.SyncRoot;
1130                         }               
1131
1132                         #endregion
1133
1134                         #region Indexers
1135
1136                         public override object this[int index] 
1137                         {
1138                                 get 
1139                                 {
1140                                         lock (m_SyncRoot) 
1141                                         {
1142                                                 return m_InnerArrayList[index];
1143                                         }
1144                                 }
1145
1146                                 set 
1147                                 {
1148                                         lock (m_SyncRoot) 
1149                                         {
1150                                                 m_InnerArrayList[index] = value;
1151                                         }
1152                                 }
1153                         }       
1154         
1155                         #endregion
1156
1157                         #region Properties
1158                         
1159                         // Some of these properties may be calculated so it's best to synchronize 
1160                         // them even though it might cause a performance hit.
1161                         // Better safe than sorry ;D.
1162
1163                         public override int Count 
1164                         {
1165                                 get 
1166                                 {
1167                                         lock (m_SyncRoot) 
1168                                         {
1169                                                 return m_InnerArrayList.Count;
1170                                         }
1171                                 }
1172                         }
1173
1174                         public override int Capacity 
1175                         {
1176                                 get 
1177                                 {
1178                                         lock (m_SyncRoot) 
1179                                         {
1180                                                 return m_InnerArrayList.Capacity;
1181                                         }
1182                                 }
1183
1184                                 set 
1185                                 {
1186                                         lock (m_SyncRoot) 
1187                                         {
1188                                                 m_InnerArrayList.Capacity = value;
1189                                         }
1190                                 }
1191                         }
1192
1193                         public override bool IsFixedSize 
1194                         {
1195                                 get 
1196                                 {
1197                                         lock (m_SyncRoot) 
1198                                         {
1199                                                 return m_InnerArrayList.IsFixedSize;
1200                                         }
1201                                 }
1202                         }
1203
1204                         public override bool IsReadOnly 
1205                         {
1206                                 get 
1207                                 {
1208                                         lock (m_SyncRoot) 
1209                                         {
1210                                                 return m_InnerArrayList.IsReadOnly;
1211                                         }
1212                                 }
1213                         }
1214
1215                         public override bool IsSynchronized 
1216                         {
1217                                 get 
1218                                 {
1219                                         return true;
1220                                 }
1221                         }
1222
1223                         public override object SyncRoot 
1224                         {
1225                                 get 
1226                                 {
1227                                         return m_SyncRoot;
1228                                 }
1229                         }
1230
1231                         #endregion
1232
1233                         #region Methods
1234
1235                         public override int Add(object value) 
1236                         {
1237                                 lock (m_SyncRoot) 
1238                                 {
1239                                         return m_InnerArrayList.Add(value);
1240                                 }
1241                         }
1242
1243                         public override void Clear() 
1244                         {
1245                                 lock (m_SyncRoot) 
1246                                 {
1247                                         m_InnerArrayList.Clear();
1248                                 }
1249                         }
1250
1251                         public override bool Contains(object value) 
1252                         {
1253                                 lock (m_SyncRoot) 
1254                                 {
1255                                         return m_InnerArrayList.Contains(value);
1256                                 }
1257                         }
1258
1259                         public override int IndexOf(object value) 
1260                         {
1261                                 lock (m_SyncRoot) 
1262                                 {
1263                                         return m_InnerArrayList.IndexOf(value);
1264                                 }
1265                         }
1266
1267                         public override int IndexOf(object value, int startIndex) 
1268                         {
1269                                 lock (m_SyncRoot) 
1270                                 {
1271                                         return m_InnerArrayList.IndexOf(value, startIndex);
1272                                 }
1273                         }
1274
1275                         public override int IndexOf(object value, int startIndex, int count) 
1276                         {
1277                                 lock (m_SyncRoot) 
1278                                 {
1279                                         return m_InnerArrayList.IndexOf(value, startIndex, count);
1280                                 }
1281                         }
1282
1283                         public override int LastIndexOf(object value) 
1284                         {
1285                                 lock (m_SyncRoot) 
1286                                 {
1287                                         return m_InnerArrayList.LastIndexOf(value);                     
1288                                 }
1289                         }
1290
1291                         public override int LastIndexOf(object value, int startIndex) 
1292                         {
1293                                 lock (m_SyncRoot) 
1294                                 {
1295                                         return m_InnerArrayList.LastIndexOf(value, startIndex);
1296                                 }
1297                         }
1298
1299                         public override int LastIndexOf(object value, int startIndex, int count) 
1300                         {
1301                                 lock (m_SyncRoot) 
1302                                 {
1303                                         return m_InnerArrayList.LastIndexOf(value, startIndex, count);
1304                                 }
1305                         }
1306
1307                         public override void Insert(int index, object value) 
1308                         {
1309                                 lock (m_SyncRoot) 
1310                                 {
1311                                         m_InnerArrayList.Insert(index, value);
1312                                 }
1313                         }
1314
1315                         public override void InsertRange(int index, ICollection c) 
1316                         {
1317                                 lock (m_SyncRoot) 
1318                                 {
1319                                         m_InnerArrayList.InsertRange(index, c);
1320                                 }
1321                         }
1322
1323                         public override void Remove(object value) 
1324                         {
1325                                 lock (m_SyncRoot) 
1326                                 {
1327                                         m_InnerArrayList.Remove(value);
1328                                 }
1329                         }
1330
1331                         public override void RemoveAt(int index) 
1332                         {
1333                                 lock (m_SyncRoot) 
1334                                 {
1335                                         m_InnerArrayList.RemoveAt(index);
1336                                 }
1337                         }
1338
1339                         public override void RemoveRange(int index, int count) 
1340                         {
1341                                 lock (m_SyncRoot) 
1342                                 {
1343                                         m_InnerArrayList.RemoveRange(index, count);
1344                                 }
1345                         }
1346
1347                         public override void Reverse() 
1348                         {
1349                                 lock (m_SyncRoot) 
1350                                 {
1351                                         m_InnerArrayList.Reverse();
1352                                 }
1353                         }
1354
1355                         public override void Reverse(int index, int count) 
1356                         {
1357                                 lock (m_SyncRoot) 
1358                                 {
1359                                         m_InnerArrayList.Reverse(index, count);
1360                                 }
1361                         }
1362
1363                         public override void CopyTo(System.Array array) 
1364                         {
1365                                 lock (m_SyncRoot) 
1366                                 {
1367                                         m_InnerArrayList.CopyTo(array);
1368                                 }
1369                         }
1370
1371                         public override void CopyTo(System.Array array, int index) 
1372                         {
1373                                 lock (m_SyncRoot) 
1374                                 {
1375                                         m_InnerArrayList.CopyTo(array, index);
1376                                 }
1377                         }
1378
1379                         public override void CopyTo(int index, System.Array array, int arrayIndex, int count) 
1380                         {
1381                                 lock (m_SyncRoot) 
1382                                 {
1383                                         m_InnerArrayList.CopyTo(index, array, arrayIndex, count);
1384                                 }
1385                         }
1386
1387                         public override IEnumerator GetEnumerator() 
1388                         {
1389                                 lock (m_SyncRoot) 
1390                                 {
1391                                         return m_InnerArrayList.GetEnumerator();
1392                                 }
1393                         }
1394
1395                         public override IEnumerator GetEnumerator(int index, int count) 
1396                         {
1397                                 lock (m_SyncRoot) 
1398                                 {                               
1399                                         return m_InnerArrayList.GetEnumerator(index, count);
1400                                 }
1401                         }
1402
1403                         public override void AddRange(ICollection c) 
1404                         {
1405                                 lock (m_SyncRoot) 
1406                                 {
1407                                         m_InnerArrayList.AddRange(c);
1408                                 }
1409                         }
1410
1411                         public override int BinarySearch(object value) 
1412                         {
1413                                 lock (m_SyncRoot) 
1414                                 {
1415                                         return m_InnerArrayList.BinarySearch(value);
1416                                 }
1417                         }
1418
1419                         public override int BinarySearch(object value, IComparer comparer) 
1420                         {
1421                                 lock (m_SyncRoot) 
1422                                 {
1423                                         return m_InnerArrayList.BinarySearch(value, comparer);
1424                                 }
1425                         }
1426
1427                         public override int BinarySearch(int index, int count, object value, IComparer comparer) 
1428                         {
1429                                 lock (m_SyncRoot) 
1430                                 {
1431                                         return m_InnerArrayList.BinarySearch(index, count, value, comparer);
1432                                 }
1433                         }
1434
1435                         public override object Clone() 
1436                         {
1437                                 lock (m_SyncRoot) 
1438                                 {
1439                                         return m_InnerArrayList.Clone();
1440                                 }
1441                         }
1442
1443                         public override ArrayList GetRange(int index, int count) 
1444                         {
1445                                 lock (m_SyncRoot) 
1446                                 {
1447                                         return m_InnerArrayList.GetRange(index, count);
1448                                 }
1449                         }
1450
1451                         public override void TrimToSize() 
1452                         {
1453                                 lock (m_SyncRoot) 
1454                                 {
1455                                         m_InnerArrayList.TrimToSize();
1456                                 }
1457                         }
1458
1459                         public override void Sort() 
1460                         {
1461                                 lock (m_SyncRoot) 
1462                                 {
1463                                         m_InnerArrayList.Sort();
1464                                 }
1465                         }
1466
1467                         public override void Sort(IComparer comparer) 
1468                         {
1469                                 lock (m_SyncRoot) 
1470                                 {
1471                                         m_InnerArrayList.Sort(comparer);
1472                                 }
1473                         }
1474
1475                         public override void Sort(int index, int count, IComparer comparer) 
1476                         {
1477                                 lock (m_SyncRoot) 
1478                                 {
1479                                         m_InnerArrayList.Sort(index, count, comparer);
1480                                 }
1481                         }
1482
1483                         public override object[] ToArray() 
1484                         {
1485                                 lock (m_SyncRoot) 
1486                                 {
1487                                         return m_InnerArrayList.ToArray();
1488                                 }
1489                         }
1490
1491                         public override Array ToArray(Type elementType) 
1492                         {
1493                                 lock (m_SyncRoot) 
1494                                 {
1495                                         return m_InnerArrayList.ToArray(elementType);
1496                                 }
1497                         }
1498
1499                         #endregion
1500                 }
1501
1502                 #endregion
1503
1504                 #region FixedSizeArrayListWrapper
1505
1506                 [Serializable]
1507                 private class FixedSizeArrayListWrapper
1508                         : ArrayListWrapper 
1509                 {
1510                         #region Constructors
1511
1512                         public FixedSizeArrayListWrapper(ArrayList innerList)
1513                                 : base(innerList) 
1514                         {
1515                         }
1516
1517                         #endregion
1518
1519                         #region Properties
1520                 
1521                         /// <summary>
1522                         /// Gets the error message to display when an readonly/fixedsize related exception is
1523                         /// thrown.
1524                         /// </summary>
1525                         protected virtual string ErrorMessage 
1526                         {
1527                                 get 
1528                                 {
1529                                         return "Can't add or remove from a fixed-size list.";
1530                                 }
1531                         }
1532
1533                         public override int Capacity 
1534                         {
1535                                 get 
1536                                 {
1537                                         return base.Capacity;
1538                                 }
1539
1540                                 set 
1541                                 {
1542                                         throw new NotSupportedException(this.ErrorMessage);
1543                                 }
1544                         }
1545
1546                         public override bool IsFixedSize 
1547                         {
1548                                 get 
1549                                 {
1550                                         return true;
1551                                 }
1552                         }
1553
1554                         #endregion
1555
1556                         #region Methods
1557
1558                         public override int Add(object value) 
1559                         {
1560                                 throw new NotSupportedException(this.ErrorMessage);
1561                         }
1562
1563                         public override void AddRange(ICollection c) 
1564                         {
1565                                 throw new NotSupportedException(this.ErrorMessage);
1566                         }
1567
1568                         public override void Clear() 
1569                         {
1570                                 throw new NotSupportedException(this.ErrorMessage);
1571                         }
1572
1573                         public override void Insert(int index, object value) 
1574                         {
1575                                 throw new NotSupportedException(this.ErrorMessage);
1576                         }
1577
1578                         public override void InsertRange(int index, ICollection c) 
1579                         {
1580                                 throw new NotSupportedException(this.ErrorMessage);
1581                         }
1582
1583                         public override void Remove(object value) 
1584                         {
1585                                 throw new NotSupportedException(this.ErrorMessage);
1586                         }
1587
1588                         public override void RemoveAt(int index) 
1589                         {
1590                                 throw new NotSupportedException(this.ErrorMessage);
1591                         }
1592
1593                         public override void RemoveRange(int index, int count) 
1594                         {
1595                                 throw new NotSupportedException(this.ErrorMessage);
1596                         }
1597                                 
1598                         public override void TrimToSize() 
1599                         {
1600                                 throw new NotSupportedException(this.ErrorMessage);
1601                         }
1602
1603                         #endregion
1604                 }
1605
1606                 #endregion              
1607
1608                 #region ReadOnlyArrayListWrapper
1609
1610                 [Serializable]
1611                 private sealed class ReadOnlyArrayListWrapper
1612                         : FixedSizeArrayListWrapper 
1613                 {
1614                         protected override string ErrorMessage 
1615                         {
1616                                 get 
1617                                 {
1618                                         return "Can't modify a readonly list.";
1619                                 }
1620                         }
1621
1622                         public override bool IsReadOnly 
1623                         {
1624                                 get 
1625                                 {
1626                                         return true;
1627                                 }
1628                         }
1629
1630                         public ReadOnlyArrayListWrapper(ArrayList innerArrayList)
1631                                 : base(innerArrayList) 
1632                         {
1633                         }
1634
1635                         public override object this[int index] 
1636                         {
1637                                 get 
1638                                 {
1639                                         return m_InnerArrayList[index];
1640                                 }
1641
1642                                 set 
1643                                 {
1644                                         throw new NotSupportedException(this.ErrorMessage);
1645                                 }
1646                         }
1647
1648                         public override void Reverse() 
1649                         {
1650                                 throw new NotSupportedException(this.ErrorMessage);
1651                         }
1652
1653                         public override void Reverse(int index, int count) 
1654                         {
1655                                 throw new NotSupportedException(this.ErrorMessage);
1656                         }
1657
1658                         public override void SetRange(int index, ICollection c) 
1659                         {
1660                                 throw new NotSupportedException(this.ErrorMessage);
1661                         }
1662
1663                         public override void Sort() 
1664                         {
1665                                 throw new NotSupportedException(this.ErrorMessage);
1666                         }
1667
1668                         public override void Sort(IComparer comparer) 
1669                         {
1670                                 throw new NotSupportedException(this.ErrorMessage);
1671                         }
1672
1673                         public override void Sort(int index, int count, IComparer comparer) 
1674                         {
1675                                 throw new NotSupportedException(this.ErrorMessage);
1676                         }
1677                 }
1678
1679                 #endregion
1680
1681                 #region RangedArrayList
1682
1683                 [Serializable]
1684                 private sealed class RangedArrayList
1685                         : ArrayListWrapper 
1686                 {
1687                         private int m_InnerIndex;
1688                         private int m_InnerCount;
1689                         private int m_InnerStateChanges;
1690
1691                         public RangedArrayList(ArrayList innerList, int index, int count)
1692                                 : base(innerList) 
1693                         {
1694                                 m_InnerIndex = index;
1695                                 m_InnerCount = count;
1696                                 m_InnerStateChanges = innerList._version;
1697                         }
1698
1699                         #region Indexers
1700
1701                         public override bool IsSynchronized
1702                         {
1703                                 get
1704                                 {
1705                                         return false;
1706                                 }
1707                         }
1708
1709                         public override object this[int index] 
1710                         {
1711                                 get 
1712                                 {
1713                                         if (index < 0 || index > m_InnerCount) 
1714                                         {
1715                                                 throw new ArgumentOutOfRangeException("index");
1716                                         }
1717
1718                                         return m_InnerArrayList[m_InnerIndex + index];
1719                                 }
1720
1721                                 set 
1722                                 {
1723                                         if (index < 0 || index > m_InnerCount) 
1724                                         {
1725                                                 throw new ArgumentOutOfRangeException("index");
1726                                         }
1727
1728                                         m_InnerArrayList[m_InnerIndex + index] = value;
1729                                 }
1730                         }
1731
1732                         #endregion
1733
1734                         #region Properties
1735
1736                         public override int Count 
1737                         {
1738                                 get 
1739                                 {
1740                                         VerifyStateChanges();
1741
1742                                         return m_InnerCount;
1743                                 }
1744                         }
1745
1746                         public override int Capacity 
1747                         {
1748                                 get 
1749                                 {
1750                                         return m_InnerArrayList.Capacity;
1751                                 }
1752
1753                                 set 
1754                                 {
1755                                         if (value < m_InnerCount) 
1756                                         {
1757                                                 throw new ArgumentOutOfRangeException();
1758                                         }
1759                                 }
1760                         }
1761
1762                         #endregion
1763
1764                         #region Methods
1765
1766                         private void VerifyStateChanges() 
1767                         {
1768                                 if (m_InnerStateChanges != m_InnerArrayList._version) 
1769                                 {
1770                                         throw new InvalidOperationException
1771                                                 ("ArrayList view is invalid because the underlying ArrayList was modified.");
1772                                 }
1773                         }
1774
1775                         public override int Add(object value) 
1776                         {
1777                                 VerifyStateChanges();
1778
1779                                 m_InnerArrayList.Insert(m_InnerIndex + m_InnerCount, value);
1780
1781                                 m_InnerStateChanges = m_InnerArrayList._version;
1782
1783                                 return ++m_InnerCount;
1784                         }
1785
1786                         public override void Clear() 
1787                         {
1788                                 VerifyStateChanges();
1789
1790                                 m_InnerArrayList.RemoveRange(m_InnerIndex, m_InnerCount);
1791                                 m_InnerCount = 0;
1792
1793                                 m_InnerStateChanges = m_InnerArrayList._version;
1794                         }
1795
1796                         public override bool Contains(object value) 
1797                         {
1798                                 return m_InnerArrayList.Contains(value, m_InnerIndex, m_InnerCount);
1799                         }
1800
1801                         public override int IndexOf(object value) 
1802                         {
1803                                 return IndexOf(value, 0);
1804                         }
1805
1806                         public override int IndexOf(object value, int startIndex) 
1807                         {
1808                                 return IndexOf(value, startIndex, m_InnerCount - startIndex);
1809                         }
1810
1811                         public override int IndexOf(object value, int startIndex, int count) 
1812                         {
1813                                 if (startIndex < 0 || startIndex > m_InnerCount) 
1814                                 {
1815                                         throw new ArgumentOutOfRangeException("startIndex", startIndex,
1816                                                 "Does not specify valid index.");
1817                                 }
1818
1819                                 if (count < 0) 
1820                                 {
1821                                         throw new ArgumentOutOfRangeException("count", count,
1822                                                 "Can't be less than 0.");
1823                                 }
1824
1825                                 // re-ordered to avoid possible integer overflow
1826                                 if (startIndex > m_InnerCount - count) 
1827                                 {
1828                                         // LAMESPEC: Every other method throws ArgumentException
1829
1830                                         throw new ArgumentOutOfRangeException("count",
1831                                                 "Start index and count do not specify a valid range.");
1832                                 }
1833
1834                                 int retval = m_InnerArrayList.IndexOf(value, m_InnerIndex + startIndex, count);
1835
1836                                 if (retval == -1) 
1837                                 {
1838                                         return -1;
1839                                 }
1840                                 else 
1841                                 {
1842                                         return retval - m_InnerIndex;
1843                                 }
1844                         }
1845
1846                         public override int LastIndexOf(object value) 
1847                         {
1848                                 return LastIndexOf(value, m_InnerCount - 1);
1849                         }
1850
1851                         public override int LastIndexOf(object value, int startIndex) 
1852                         {
1853                                 return LastIndexOf(value, startIndex, startIndex + 1);
1854                         }
1855
1856                         public override int LastIndexOf(object value, int startIndex, int count) 
1857                         {
1858                                 if (startIndex < 0) 
1859                                 {
1860                                         throw new ArgumentOutOfRangeException("startIndex", startIndex, "< 0");
1861                                 }
1862
1863                                 if (count < 0) 
1864                                 {
1865                                         throw new ArgumentOutOfRangeException("count", count, "count is negative.");
1866                                 }
1867
1868                                 int retval = m_InnerArrayList.LastIndexOf(value, m_InnerIndex + startIndex, count);
1869
1870                                 if (retval == -1) 
1871                                 {
1872                                         return -1;
1873                                 }
1874                                 else 
1875                                 {
1876                                         return retval - m_InnerIndex;
1877                                 }
1878                         }
1879
1880                         public override void Insert(int index, object value) 
1881                         {
1882                                 VerifyStateChanges();
1883                                 
1884                                 if (index < 0 || index > m_InnerCount) 
1885                                 {
1886                                         throw new ArgumentOutOfRangeException("index", index,
1887                                                 "Index must be >= 0 and <= Count.");
1888                                 }
1889
1890                                 m_InnerArrayList.Insert(m_InnerIndex + index, value);
1891
1892                                 m_InnerCount++;
1893
1894                                 m_InnerStateChanges = m_InnerArrayList._version;
1895                         }
1896
1897                         public override void InsertRange(int index, ICollection c) 
1898                         {
1899                                 VerifyStateChanges();
1900
1901                                 if (index < 0 || index > m_InnerCount) 
1902                                 {
1903                                         throw new ArgumentOutOfRangeException("index", index,
1904                                                 "Index must be >= 0 and <= Count.");
1905                                 }
1906
1907                                 m_InnerArrayList.InsertRange(m_InnerIndex + index, c);
1908
1909                                 m_InnerCount += c.Count;
1910
1911                                 m_InnerStateChanges = m_InnerArrayList._version;
1912                         }
1913
1914                         public override void Remove(object value) 
1915                         {
1916                                 VerifyStateChanges();
1917
1918                                 int x = IndexOf(value);
1919
1920                                 if (x > -1) 
1921                                 {
1922                                         RemoveAt(x);
1923                                 }
1924
1925                                 m_InnerStateChanges = m_InnerArrayList._version;
1926                         }
1927
1928                         public override void RemoveAt(int index) 
1929                         {
1930                                 VerifyStateChanges();
1931
1932                                 if (index < 0 || index > m_InnerCount) 
1933                                 {
1934                                         throw new ArgumentOutOfRangeException("index", index,
1935                                                 "Index must be >= 0 and <= Count.");
1936                                 }
1937
1938                                 m_InnerArrayList.RemoveAt(m_InnerIndex + index);
1939
1940                                 m_InnerCount--;
1941                                 m_InnerStateChanges = m_InnerArrayList._version;
1942                         }
1943
1944                         public override void RemoveRange(int index, int count) 
1945                         {
1946                                 VerifyStateChanges();
1947
1948                                 CheckRange(index, count, m_InnerCount);                         
1949
1950                                 m_InnerArrayList.RemoveRange(m_InnerIndex + index, count);
1951
1952                                 m_InnerCount -= count;
1953
1954                                 m_InnerStateChanges = m_InnerArrayList._version;
1955                         }
1956
1957                         public override void Reverse() 
1958                         {
1959                                 Reverse(0, m_InnerCount);
1960                         }
1961
1962                         public override void Reverse(int index, int count) 
1963                         {
1964                                 VerifyStateChanges();
1965
1966                                 CheckRange(index, count, m_InnerCount);                         
1967
1968                                 m_InnerArrayList.Reverse(m_InnerIndex + index, count);
1969
1970                                 m_InnerStateChanges = m_InnerArrayList._version;
1971                         }
1972
1973                         public override void SetRange(int index, ICollection c) 
1974                         {
1975                                 VerifyStateChanges();
1976
1977                                 if (index < 0 || index > m_InnerCount) 
1978                                 {
1979                                         throw new ArgumentOutOfRangeException("index", index,
1980                                                 "Index must be >= 0 and <= Count.");
1981                                 }
1982
1983                                 m_InnerArrayList.SetRange(m_InnerIndex + index, c);
1984
1985                                 m_InnerStateChanges = m_InnerArrayList._version;
1986                         }
1987
1988                         public override void CopyTo(System.Array array) 
1989                         {
1990                                 CopyTo(array, 0);
1991                         }
1992
1993                         public override void CopyTo(System.Array array, int index) 
1994                         {
1995                                 CopyTo(0, array, index, m_InnerCount);
1996                         }
1997
1998                         public override void CopyTo(int index, System.Array array, int arrayIndex, int count) 
1999                         {
2000                                 CheckRange(index, count, m_InnerCount);                         
2001
2002                                 m_InnerArrayList.CopyTo(m_InnerIndex + index, array, arrayIndex, count);
2003                         }
2004
2005                         public override IEnumerator GetEnumerator() 
2006                         {
2007                                 return GetEnumerator(0, m_InnerCount);
2008                         }
2009
2010                         public override IEnumerator GetEnumerator(int index, int count) 
2011                         {
2012                                 CheckRange(index, count, m_InnerCount);
2013
2014                                 return m_InnerArrayList.GetEnumerator(m_InnerIndex + index, count);
2015                         }
2016
2017                         public override void AddRange(ICollection c) 
2018                         {
2019                                 VerifyStateChanges();
2020
2021                                 m_InnerArrayList.InsertRange(m_InnerCount, c);
2022
2023                                 m_InnerCount += c.Count;
2024
2025                                 m_InnerStateChanges = m_InnerArrayList._version;
2026                         }
2027
2028                         public override int BinarySearch(object value) 
2029                         {
2030                                 return BinarySearch(0, m_InnerCount, value, Comparer.Default);
2031                         }
2032
2033                         public override int BinarySearch(object value, IComparer comparer) 
2034                         {
2035                                 return BinarySearch(0, m_InnerCount, value, comparer);
2036                         }
2037
2038                         public override int BinarySearch(int index, int count, object value, IComparer comparer) 
2039                         {
2040                                 CheckRange(index, count, m_InnerCount);
2041
2042                                 return m_InnerArrayList.BinarySearch(m_InnerIndex + index, count, value, comparer);
2043                         }
2044
2045                         public override object Clone() 
2046                         {
2047                                 return new RangedArrayList((ArrayList)m_InnerArrayList.Clone(), m_InnerIndex, m_InnerCount);
2048                         }
2049
2050                         public override ArrayList GetRange(int index, int count) 
2051                         {
2052                                 CheckRange(index, count, m_InnerCount);
2053
2054                                 return new RangedArrayList(this, index, count);
2055                         }
2056
2057                         public override void TrimToSize() 
2058                         {
2059                                 throw new NotSupportedException();
2060                         }
2061
2062                         public override void Sort() 
2063                         {
2064                                 Sort(Comparer.Default);
2065                         }
2066
2067                         public override void Sort(IComparer comparer) 
2068                         {
2069                                 Sort(0, m_InnerCount, comparer);
2070                         }
2071
2072                         public override void Sort(int index, int count, IComparer comparer) 
2073                         {
2074                                 VerifyStateChanges();
2075
2076                                 CheckRange(index, count, m_InnerCount);
2077
2078                                 m_InnerArrayList.Sort(m_InnerIndex + index, count, comparer);
2079
2080                                 m_InnerStateChanges = m_InnerArrayList._version;
2081                         }
2082
2083                         public override object[] ToArray() 
2084                         {
2085                                 object[] array;
2086
2087                                 array = new object[m_InnerCount];
2088
2089                                 m_InnerArrayList.CopyTo (m_InnerIndex, array, 0, m_InnerCount);
2090
2091                                 return array;
2092                         }
2093
2094                         public override Array ToArray(Type elementType) 
2095                         {
2096                                 Array array;
2097
2098                                 array = Array.CreateInstance(elementType, m_InnerCount);
2099
2100                                 m_InnerArrayList.CopyTo(m_InnerIndex, array, 0, m_InnerCount);
2101
2102                                 return array;
2103                         }
2104
2105                         #endregion
2106                 }
2107
2108                 #endregion
2109
2110                 //
2111                 // List wrappers
2112                 //
2113
2114                 #region SynchronizedListWrapper
2115
2116                 [Serializable]
2117                 private sealed class SynchronizedListWrapper
2118                         : ListWrapper 
2119                 {
2120                         private object m_SyncRoot;
2121
2122                         public SynchronizedListWrapper(IList innerList)
2123                                 : base(innerList) 
2124                         {
2125                                 m_SyncRoot = innerList.SyncRoot;
2126                         }
2127
2128                         public override int Count 
2129                         {
2130                                 get 
2131                                 {
2132                                         lock (m_SyncRoot) 
2133                                         {
2134                                                 return m_InnerList.Count;
2135                                         }
2136                                 }
2137                         }
2138
2139                         public override bool IsSynchronized 
2140                         {
2141                                 get 
2142                                 {
2143                                         return true;
2144                                 }
2145                         }
2146
2147                         public override object SyncRoot 
2148                         {
2149                                 get 
2150                                 {
2151                                         lock (m_SyncRoot) 
2152                                         {
2153                                                 return m_InnerList.SyncRoot;
2154                                         }
2155                                 }
2156                         }
2157
2158                         public override bool IsFixedSize 
2159                         {
2160                                 get 
2161                                 {
2162                                         lock (m_SyncRoot) 
2163                                         {
2164                                                 return m_InnerList.IsFixedSize;
2165                                         }
2166                                 }
2167                         }
2168
2169                         public override bool IsReadOnly 
2170                         {
2171                                 get 
2172                                 {
2173                                         lock (m_SyncRoot) 
2174                                         {
2175                                                 return m_InnerList.IsReadOnly;
2176                                         }
2177                                 }
2178                         }
2179
2180                         public override object this[int index] 
2181                         {
2182                                 get 
2183                                 {
2184                                         lock (m_SyncRoot) 
2185                                         {
2186                                                 return m_InnerList[index];
2187                                         }
2188                                 }
2189
2190                                 set 
2191                                 {
2192                                         lock (m_SyncRoot) 
2193                                         {
2194                                                 m_InnerList[index] = value;
2195                                         }
2196                                 }
2197                         }
2198
2199                         public override int Add(object value) 
2200                         {
2201                                 lock (m_SyncRoot) 
2202                                 {
2203                                         return m_InnerList.Add(value);
2204                                 }
2205                         }
2206
2207                         public override void Clear() 
2208                         {
2209                                 lock (m_SyncRoot) 
2210                                 {
2211                                         m_InnerList.Clear();
2212                                 }
2213                         }
2214
2215                         public override bool Contains(object value) 
2216                         {
2217                                 lock (m_SyncRoot) 
2218                                 {
2219                                         return m_InnerList.Contains(value);
2220                                 }
2221                         }
2222
2223                         public override int IndexOf(object value) 
2224                         {
2225                                 lock (m_SyncRoot) 
2226                                 {
2227                                         return m_InnerList.IndexOf(value);
2228                                 }
2229                         }
2230
2231                         public override void Insert(int index, object value) 
2232                         {
2233                                 lock (m_SyncRoot) 
2234                                 {
2235                                         m_InnerList.Insert(index, value);
2236                                 }
2237                         }
2238
2239                         public override void Remove(object value) 
2240                         {
2241                                 lock (m_SyncRoot) 
2242                                 {
2243                                         m_InnerList.Remove(value);
2244                                 }
2245                         }
2246
2247                         public override void RemoveAt(int index) 
2248                         {
2249                                 lock (m_SyncRoot) 
2250                                 {
2251                                         m_InnerList.RemoveAt(index);
2252                                 }
2253                         }
2254
2255                         public override void CopyTo(Array array, int index) 
2256                         {
2257                                 lock (m_SyncRoot) 
2258                                 {
2259                                         m_InnerList.CopyTo(array, index);
2260                                 }
2261                         }
2262
2263                         public override IEnumerator GetEnumerator() 
2264                         {
2265                                 lock (m_SyncRoot) 
2266                                 {
2267                                         return m_InnerList.GetEnumerator();
2268                                 }
2269                         }
2270                 }
2271
2272                 #endregion
2273
2274                 #region FixedSizeListWrapper
2275
2276                 [Serializable]
2277                         private class FixedSizeListWrapper
2278                         : ListWrapper 
2279                 {
2280                         protected virtual string ErrorMessage 
2281                         {               
2282                                 get 
2283                                 {
2284                                         return "List is fixed-size.";
2285                                 }
2286                         }
2287
2288                         public override bool IsFixedSize 
2289                         {
2290                                 get 
2291                                 {
2292                                         return true;
2293                                 }
2294                         }
2295
2296                         public FixedSizeListWrapper(IList innerList)
2297                                 : base(innerList) 
2298                         {
2299                         }
2300
2301                         public override int Add(object value) 
2302                         {
2303                                 throw new NotSupportedException(this.ErrorMessage);
2304                         }
2305
2306                         public override void Clear() 
2307                         {
2308                                 throw new NotSupportedException(this.ErrorMessage);
2309                         }
2310
2311                         public override void Insert(int index, object value) 
2312                         {
2313                                 throw new NotSupportedException(this.ErrorMessage);
2314                         }
2315
2316                         public override void Remove(object value) 
2317                         {
2318                                 throw new NotSupportedException(this.ErrorMessage);
2319                         }
2320
2321                         public override void RemoveAt(int index) 
2322                         {
2323                                 throw new NotSupportedException(this.ErrorMessage);
2324                         }
2325                 }
2326
2327                 #endregion
2328
2329                 #region ReadOnlyListWrapper
2330
2331                 [Serializable]
2332                 private sealed class ReadOnlyListWrapper
2333                         : FixedSizeListWrapper 
2334                 {
2335                         protected override string ErrorMessage 
2336                         {               
2337                                 get 
2338                                 {
2339                                         return "List is read-only.";
2340                                 }
2341                         }
2342
2343                         public override bool IsReadOnly 
2344                         {
2345                                 get 
2346                                 {
2347                                         return true;
2348                                 }
2349                         }
2350
2351                         public ReadOnlyListWrapper(IList innerList)
2352                                 : base(innerList) 
2353                         {
2354                         }
2355
2356                         public override object this[int index] 
2357                         {
2358                                 get 
2359                                 {
2360                                         return m_InnerList[index];
2361                                 }
2362
2363                                 set 
2364                                 {
2365                                         throw new NotSupportedException(this.ErrorMessage);
2366                                 }
2367                         }
2368                 }
2369
2370                 #endregion
2371
2372                 #region ListWrapper
2373
2374                 /// <summary>
2375                 /// Decorates/Wraps any <c>IList</c> implementing object.
2376                 /// </summary>
2377                 [Serializable]
2378                 private class ListWrapper
2379                         : IList 
2380                 {
2381                         #region Fields
2382
2383                         protected IList m_InnerList;
2384
2385                         #endregion
2386
2387                         #region Constructors
2388
2389                         public ListWrapper(IList innerList) 
2390                         {                       
2391                                 m_InnerList = innerList;
2392                         }
2393
2394                         #endregion
2395
2396                         #region Indexers
2397
2398                         public virtual object this[int index] 
2399                         {
2400                                 get 
2401                                 {
2402                                         return m_InnerList[index];
2403                                 }
2404
2405                                 set 
2406                                 {
2407                                         m_InnerList[index] = value;
2408                                 }
2409                         }
2410
2411                         #endregion
2412
2413                         #region Properties
2414
2415                         public virtual int Count 
2416                         {
2417                                 get 
2418                                 {
2419                                         return m_InnerList.Count;
2420                                 }
2421                         }
2422
2423                         public virtual bool IsSynchronized 
2424                         {
2425                                 get 
2426                                 {
2427                                         return m_InnerList.IsSynchronized;
2428                                 }
2429                         }
2430
2431                         public virtual object SyncRoot 
2432                         {
2433                                 get 
2434                                 {
2435                                         return m_InnerList.SyncRoot;
2436                                 }
2437                         }
2438
2439                         public virtual bool IsFixedSize 
2440                         {
2441                                 get 
2442                                 {
2443                                         return m_InnerList.IsFixedSize;
2444                                 }
2445                         }
2446
2447                         public virtual bool IsReadOnly 
2448                         {
2449                                 get 
2450                                 {
2451                                         return m_InnerList.IsReadOnly;
2452                                 }
2453                         }
2454
2455                         #endregion
2456
2457                         #region Methods
2458
2459                         public virtual int Add(object value) 
2460                         {
2461                                 return m_InnerList.Add(value);
2462                         }
2463
2464                         public virtual void Clear() 
2465                         {
2466                                 m_InnerList.Clear();
2467                         }
2468
2469                         public virtual bool Contains(object value) 
2470                         {
2471                                 return m_InnerList.Contains(value);
2472                         }
2473
2474                         public virtual int IndexOf(object value) 
2475                         {
2476                                 return m_InnerList.IndexOf(value);
2477                         }
2478
2479                         public virtual void Insert(int index, object value) 
2480                         {
2481                                 m_InnerList.Insert(index, value);
2482                         }
2483
2484                         public virtual void Remove(object value) 
2485                         {
2486                                 m_InnerList.Remove(value);
2487                         }
2488
2489                         public virtual void RemoveAt(int index) 
2490                         {
2491                                 m_InnerList.RemoveAt(index);
2492                         }
2493
2494                         public virtual void CopyTo(Array array, int index) 
2495                         {
2496                                 m_InnerList.CopyTo(array, index);
2497                         }
2498
2499                         public virtual IEnumerator GetEnumerator() 
2500                         {
2501                                 return m_InnerList.GetEnumerator();
2502                         }
2503
2504                         #endregion
2505                 }
2506
2507                 #endregion
2508
2509                 //
2510                 // Start of ArrayList
2511                 //
2512
2513                 #region Fields
2514
2515                 private const int DefaultInitialCapacity = 0x10;
2516                 
2517                 /// <summary>
2518                 /// Number of items in the list.
2519                 /// </summary>
2520                 private int _size;
2521
2522                 /// <summary>
2523                 /// Array to store the items.
2524                 /// </summary>
2525                 private object[] _items;
2526                 
2527                 /// <summary>
2528                 /// Total number of state changes.
2529                 /// </summary>
2530                 private int _version;
2531
2532                 #endregion
2533                 
2534                 #region Constructors
2535
2536                 /// <summary>
2537                 /// Initializes a new instance of the <see cref="ArrayList"/> class that is empty and
2538                 /// has the default initial capacity (16).
2539                 /// </summary>
2540                 public ArrayList()
2541                 {
2542                         _items = new object[DefaultInitialCapacity];
2543                 }               
2544
2545                 /// <summary>
2546                 /// Initializes a new instance of the <see cref="ArrayList"/> class that contains 
2547                 /// elements copied from the specified collection and that has the same initial capacity
2548                 /// as the number of elements copied.
2549                 /// </summary>
2550                 /// <param name="c">
2551                 /// The <see cref="ICollection"/> whose elements are copied into the new list.
2552                 /// </param>
2553                 /// <exception cref="ArgumentNullException">
2554                 /// The argument <c>c</c> is a null reference.
2555                 /// </exception>
2556                 public ArrayList(ICollection c) 
2557                 {
2558                         Array array;
2559
2560                         if (c == null) 
2561                         {
2562                                 throw new ArgumentNullException("c");
2563                         }
2564                                                 
2565                         array = c as Array;
2566
2567                         if (array != null && array.Rank != 1) 
2568                         {
2569                                 throw new RankException();
2570                         }
2571
2572                         _items = new object[c.Count];
2573
2574                         AddRange(c);
2575                 }
2576
2577                 /// <summary>
2578                 /// Initializes a new instance of the <see cref="ArrayList"/> class that is empty and
2579                 /// has the specified initial capacity.
2580                 /// </summary>
2581                 /// <param name="capacity">
2582                 /// The number of elements that hte new list is initially capable of storing.
2583                 /// </param>
2584                 /// <exception cref="ArgumentOutOfRangeException">
2585                 /// The <c>capacity</c> is less than zero.
2586                 /// </exception>
2587                 public ArrayList(int capacity) 
2588                 {
2589                         if (capacity < 0) 
2590                         {
2591                                 throw new ArgumentOutOfRangeException("capacity",
2592                                         capacity, "The initial capacity can't be smaller than zero.");
2593                         }
2594
2595                         if (capacity == 0) 
2596                         {
2597                                 capacity = DefaultInitialCapacity;
2598                         }
2599                         _items = new object [capacity];
2600                 }
2601
2602                 /// <summary>
2603                 /// Used by ArrayListAdapter to allow creation of an ArrayList with no storage buffer.
2604                 /// </summary>
2605                 private ArrayList(int initialCapacity, bool forceZeroSize) 
2606                 {
2607                         if (forceZeroSize) 
2608                         {                               
2609                                 _items = null;
2610                         }
2611                         else 
2612                         {
2613                                 throw new InvalidOperationException("Use ArrayList(int)");
2614                         }
2615                 }
2616
2617                 /// <summary>
2618                 /// Initializes a new array list that contains a copy of the given array and with the
2619                 /// given count.
2620                 /// </summary>
2621                 /// <param name="array"></param>
2622                 private ArrayList(object[] array, int index, int count) 
2623                 {
2624                         if (count == 0) 
2625                         {
2626                                 _items = new object[DefaultInitialCapacity];
2627                         }
2628                         else 
2629                         {
2630                                 _items = new object[count];
2631                         }
2632
2633                         Array.Copy(array, index, _items, 0, count);
2634
2635                         _size = count;
2636                 }
2637
2638                 #endregion
2639
2640                 #region Indexers
2641
2642                 /// <summary>
2643                 /// Gets/Sets an element in the list by index.
2644                 /// </summary>
2645                 /// <exception cref="ArgumentOutOfRangeException">
2646                 /// The index is less than 0 or more then or equal to the list count.
2647                 /// </exception>
2648                 public virtual object this[int index] 
2649                 {
2650                         get 
2651                         {
2652                                 if (index < 0 || index >= _size) 
2653                                 {
2654                                         throw new ArgumentOutOfRangeException("index", index,
2655                                                 "Index is less than 0 or more than or equal to the list count.");
2656                                 }
2657
2658                                 return _items[index];
2659                         }
2660
2661                         set 
2662                         {
2663                                 if (index < 0 || index >= _size) 
2664                                 {
2665                                         throw new ArgumentOutOfRangeException("index", index,
2666                                                 "Index is less than 0 or more than or equal to the list count.");
2667                                 }
2668
2669                                 _items[index] = value;
2670                                 _version++;
2671                         }
2672                 }
2673
2674                 #endregion
2675
2676                 #region Properties
2677
2678                 /// <summary>
2679                 /// Gets the number of elements in the list.
2680                 /// </summary>
2681                 public virtual int Count 
2682                 {
2683                         get 
2684                         {                               
2685                                 return _size;
2686                         }
2687                 }
2688
2689                 /// <summary>
2690                 /// Gets the number of elements the list can carry without needing to expand.
2691                 /// </summary>
2692                 /// <remarks>
2693                 /// ArrayLists automatically double their capacity when the capacity limit is broken.
2694                 /// </remarks>
2695                 /// <exception cref="ArgumentOutOfRangeException">
2696                 /// The capacity is less than the count.
2697                 /// </exception>
2698                 public virtual int Capacity 
2699                 {
2700                         get 
2701                         {
2702                                 return _items.Length;
2703                         }
2704
2705                         set 
2706                         {
2707                                 if (value < _size) 
2708                                 {
2709                                         throw new ArgumentOutOfRangeException("Capacity", value,
2710                                                 "Must be more than count.");
2711                                 }
2712
2713                                 object[] newArray;
2714
2715                                 newArray = new object[value];
2716
2717                                 Array.Copy(_items, 0, newArray, 0, _size);
2718
2719                                 _items = newArray;
2720                         }
2721                 }
2722
2723                 /// <summary>
2724                 /// <see cref="IList.IsFixedSize"/>
2725                 /// </summary>
2726                 /// <remarks/>
2727                 public virtual bool IsFixedSize 
2728                 {
2729                         get 
2730                         {
2731                                 return false;
2732                         }
2733                 }
2734
2735                 /// <summary>
2736                 /// <see cref="IList.IsReadOnly"/>
2737                 /// </summary>
2738                 public virtual bool IsReadOnly 
2739                 {
2740                         get 
2741                         {
2742                                 return false;
2743                         }
2744                 }
2745
2746                 /// <summary>
2747                 /// <see cref="ICollection.IsSynchronized"/>
2748                 /// </summary>
2749                 public virtual bool IsSynchronized 
2750                 {
2751                         get 
2752                         {
2753                                 return false;
2754                         }
2755                 }
2756
2757                 /// <summary>
2758                 /// <see cref="ICollection.SyncRoot"/>
2759                 /// </summary>
2760                 public virtual object SyncRoot 
2761                 {
2762                         get 
2763                         {
2764                                 return this;
2765                         }
2766                 }
2767
2768                 #endregion
2769
2770                 #region Methods
2771
2772                 /// <remarks>
2773                 /// Ensures that the list has the capacity to contain the given <c>count</c> by
2774                 /// automatically expanding the capacity when required.
2775                 /// </remarks>
2776                 private void EnsureCapacity(int count) 
2777                 {
2778                         if (count <= _items.Length) 
2779                         {
2780                                 return;
2781                         }
2782
2783                         int newLength;
2784                         object[] newData;
2785
2786                         newLength = _items.Length << 1;
2787                         if (newLength == 0)
2788                                 newLength = DefaultInitialCapacity;
2789
2790                         while (newLength < count) 
2791                         {
2792                                 newLength <<= 1;
2793                         }
2794
2795                         newData = new object[newLength];
2796
2797                         Array.Copy(_items, 0, newData, 0, _items.Length);
2798
2799                         _items = newData;
2800                 }
2801                 
2802                 /// <summary>
2803                 /// Shifts a section of the list.
2804                 /// </summary>
2805                 /// <param name="index">
2806                 /// The start of the section to shift (the element at index is included in the shift).
2807                 /// </param>
2808                 /// <param name="count">
2809                 /// The number of positions to shift by (can be negative).
2810                 /// </param>
2811                 private void Shift(int index, int count) 
2812                 {
2813                         if (count > 0) 
2814                         {
2815                                 if (_size + count > _items.Length) 
2816                                 {
2817                                         int newLength;
2818                                         object[] newData;
2819                                         
2820                                         newLength = (_items.Length > 0) ? _items.Length << 1 : 1;
2821
2822                                         while (newLength < _size + count) 
2823                                         {
2824                                                 newLength <<= 1;
2825                                         }
2826                                         
2827                                         newData = new object[newLength];
2828
2829                                         Array.Copy(_items, 0, newData, 0, index);
2830                                         Array.Copy(_items, index, newData, index + count, _size - index);
2831
2832                                         _items = newData;
2833                                 }
2834                                 else 
2835                                 {
2836                                         Array.Copy(_items, index, _items, index + count, _size - index);
2837                                 }
2838                         }
2839                         else if (count < 0) 
2840                         {
2841                                 // Remember count is negative so this is actually index + (-count)
2842
2843                                 int x = index - count ;
2844
2845                                 Array.Copy(_items, x, _items, index, _size - x);
2846                         }
2847                 }
2848
2849                 public virtual int Add(object value) 
2850                 {
2851                         // Do a check here in case EnsureCapacity isn't inlined.
2852
2853                         if (_items.Length <= _size /* same as _items.Length < _size + 1) */) 
2854                         {
2855                                 EnsureCapacity(_size + 1);
2856                         }
2857
2858                         _items[_size] = value;
2859                         
2860                         _version++;
2861
2862                         return _size++;
2863                 }
2864
2865                 public virtual void Clear() 
2866                 {
2867                         // Keep the array but null all members so they can be garbage collected.
2868
2869                         Array.Clear(_items, 0, _size);
2870
2871                         _size = 0;
2872                         _version++;
2873                 }
2874
2875                 public virtual bool Contains(object value) 
2876                 {
2877                         return IndexOf(value, 0, _size) > -1;
2878                 }
2879
2880                 internal virtual bool Contains(object value, int startIndex, int count) 
2881                 {
2882                         return IndexOf(value, startIndex, count) > -1;
2883                 }
2884
2885                 public virtual int IndexOf(object value) 
2886                 {                       
2887                         return IndexOf(value, 0);
2888                 }
2889
2890                 public virtual int IndexOf(object value, int startIndex) 
2891                 {
2892                         return IndexOf(value, startIndex, _size - startIndex);
2893                 }
2894
2895                 public virtual int IndexOf(object value, int startIndex, int count) 
2896                 {
2897                         if (startIndex < 0 || startIndex > _size) 
2898                         {
2899                                 throw new ArgumentOutOfRangeException("startIndex", startIndex,
2900                                         "Does not specify valid index.");
2901                         }
2902
2903                         if (count < 0) 
2904                         {
2905                                 throw new ArgumentOutOfRangeException("count", count,
2906                                         "Can't be less than 0.");
2907                         }
2908
2909                         // re-ordered to avoid integer overflow
2910                         if (startIndex > _size - count) 
2911                         {
2912                                 // LAMESPEC: Every other method throws ArgumentException
2913
2914                                 throw new ArgumentOutOfRangeException("count",
2915                                         "Start index and count do not specify a valid range.");
2916                         }
2917
2918                         return Array.IndexOf(_items, value, startIndex, count);
2919                 }
2920
2921                 public virtual int LastIndexOf(object value) 
2922                 {
2923                         return LastIndexOf(value, _size - 1);
2924                 }
2925
2926                 public virtual int LastIndexOf(object value, int startIndex) 
2927                 {
2928                         return LastIndexOf(value, startIndex, startIndex + 1);
2929                 }
2930
2931                 public virtual int LastIndexOf (object value, int startIndex, int count) 
2932                 {
2933                         // Array will catch the exceptions
2934                         return Array.LastIndexOf (_items, value, startIndex, count);
2935                 }
2936
2937                 public virtual void Insert(int index, object value) 
2938                 {
2939                         if (index < 0 || index > _size) 
2940                         {
2941                                 throw new ArgumentOutOfRangeException("index", index,
2942                                         "Index must be >= 0 and <= Count.");
2943                         }
2944
2945                         Shift(index, 1);
2946
2947                         _items[index] = value;
2948                         _size++;
2949                         _version++;
2950                 }
2951
2952                 public virtual void InsertRange(int index, ICollection c) 
2953                 {
2954                         int i;
2955
2956                         if (c == null) 
2957                         {
2958                                 throw new ArgumentNullException("c");
2959                         }
2960
2961                         if (index < 0 || index > _size) 
2962                         {
2963                                 throw new ArgumentOutOfRangeException("index", index,
2964                                         "Index must be >= 0 and <= Count.");
2965                         }
2966
2967                         i = c.Count;
2968                         
2969                         // Do a check here in case EnsureCapacity isn't inlined.
2970
2971                         if (_items.Length < _size + i) 
2972                         {
2973                                 EnsureCapacity(_size + i);
2974                         }
2975
2976                         if (index < _size) 
2977                         {
2978                                 Array.Copy(_items, index, _items, index + i, _size - index);
2979                         }
2980                 
2981                         // Handle inserting a range from a list to itself specially.
2982
2983                         if (this == c.SyncRoot) 
2984                         {
2985                                 // Copy range before the insert point.
2986
2987                                 Array.Copy(_items, 0, _items, index, index);
2988
2989                                 // Copy range after the insert point.
2990
2991                                 Array.Copy(_items, index + i, _items, index << 1, _size - index);
2992                         }
2993                         else 
2994                         {
2995                                 c.CopyTo(_items, index);
2996                         }
2997                 
2998                         _size += c.Count;
2999                         _version++;
3000                 }
3001
3002                 public virtual void Remove(object value) 
3003                 {
3004                         int x;
3005
3006                         x = IndexOf(value);
3007
3008                         if (x > -1) 
3009                         {
3010                                 RemoveAt(x);
3011                         }
3012
3013                         _version++;
3014                 }
3015
3016                 public virtual void RemoveAt(int index) 
3017                 {
3018                         if (index < 0 || index >= _size) 
3019                         {
3020                                 throw new ArgumentOutOfRangeException("index", index,
3021                                         "Less than 0 or more than list count.");
3022                         }
3023
3024                         Shift(index, -1);
3025                         _size--;
3026                         _version++;
3027                 }
3028
3029                 public virtual void RemoveRange(int index, int count) 
3030                 {
3031                         ArrayList.CheckRange(index, count, _size);
3032                                                 
3033                         Shift(index, -count);
3034                         _size -= count;
3035                         _version++;                     
3036                 }
3037
3038                 public virtual void Reverse() 
3039                 {
3040                         Array.Reverse(_items, 0, _size);
3041                         _version++;
3042                 }
3043
3044                 public virtual void Reverse(int index, int count) 
3045                 {
3046                         ArrayList.CheckRange(index, count, _size);
3047
3048                         Array.Reverse(_items, index, count);
3049                         _version++;
3050                 }
3051
3052                 public virtual void CopyTo(System.Array array) 
3053                 {
3054                         Array.Copy(_items, array, _size);
3055                 }
3056
3057                 public virtual void CopyTo(System.Array array, int index) 
3058                 {                       
3059                         CopyTo(0, array, index, _size);
3060                 }
3061
3062                 public virtual void CopyTo(int index, System.Array array, int arrayIndex, int count) 
3063                 {
3064                         if (array == null) 
3065                         {
3066                                 throw new ArgumentNullException("array");
3067                         }
3068
3069                         if (array.Rank != 1) 
3070                         {
3071                                 // LAMESPEC:
3072                                 // This should be a RankException because Array.Copy throws RankException.
3073
3074                                 throw new ArgumentException("Must have only 1 dimensions.", "array");
3075                         }
3076
3077                         Array.Copy(_items, index, array, arrayIndex, count);
3078                 }
3079
3080                 public virtual IEnumerator GetEnumerator() 
3081                 {
3082                         return new SimpleEnumerator(this);
3083                 }
3084
3085                 public virtual IEnumerator GetEnumerator(int index, int count) 
3086                 {
3087                         ArrayList.CheckRange(index, count, _size);
3088
3089                         return new ArrayListEnumerator(this, index, count);
3090                 }
3091
3092                 public virtual void AddRange(ICollection c) 
3093                 {
3094                         InsertRange(_size, c);
3095                 }
3096
3097                 public virtual int BinarySearch(object value) 
3098                 {
3099                         try 
3100                         {
3101                                 return Array.BinarySearch(_items, 0, _size, value);
3102                         }
3103                         catch (InvalidOperationException e) 
3104                         {
3105                                 throw new ArgumentException(e.Message);
3106                         }
3107                 }
3108
3109                 public virtual int BinarySearch(object value, IComparer comparer) 
3110                 {
3111                         try 
3112                         {
3113                                 return Array.BinarySearch(_items, 0, _size, value, comparer);
3114                         }
3115                         catch (InvalidOperationException e) 
3116                         {
3117                                 throw new ArgumentException(e.Message);
3118                         }
3119                 }
3120
3121                 public virtual int BinarySearch(int index, int count, object value, IComparer comparer) 
3122                 {
3123                         try 
3124                         {
3125                                 return Array.BinarySearch(_items, index, count, value, comparer);
3126                         }
3127                         catch (InvalidOperationException e) 
3128                         {
3129                                 throw new ArgumentException(e.Message);
3130                         }
3131                 }
3132                 public virtual ArrayList GetRange(int index, int count) 
3133                 {
3134                         ArrayList.CheckRange(index, count, _size);
3135
3136                         if (this.IsSynchronized)
3137                         {
3138                                 return ArrayList.Synchronized(new RangedArrayList(this, index, count));
3139                         }
3140                         else
3141                         {
3142                                 return new RangedArrayList(this, index, count);
3143                         }
3144                 }
3145
3146                 public virtual void SetRange(int index, ICollection c) 
3147                 {
3148                         if (c == null) 
3149                         {
3150                                 throw new ArgumentNullException("c");
3151                         }
3152
3153                         if (index < 0 || index + c.Count > _size) 
3154                         {
3155                                 throw new ArgumentOutOfRangeException("index");
3156                         }
3157
3158                         c.CopyTo(_items, index);
3159
3160                         _version++;
3161                 }
3162
3163                 public virtual void TrimToSize() 
3164                 {
3165                         if (_items.Length > _size) 
3166                         {
3167                                 object[] newArray;
3168
3169                                 if (_size == 0) 
3170                                 {
3171                                         newArray = new object[DefaultInitialCapacity];
3172                                 }
3173                                 else 
3174                                 {
3175                                         newArray = new object[_size];
3176                                 }
3177                                                                 
3178                                 Array.Copy(_items, 0, newArray, 0, _size);
3179
3180                                 _items = newArray;
3181                         }
3182                 }
3183
3184                 public virtual void Sort() 
3185                 {
3186                         Array.Sort(_items, 0, _size);
3187
3188                         _version++;
3189                 }
3190
3191                 public virtual void Sort(IComparer comparer) 
3192                 {
3193                         Array.Sort(_items, 0, _size, comparer);
3194                 }
3195
3196                 public virtual void Sort(int index, int count, IComparer comparer) 
3197                 {
3198                         ArrayList.CheckRange(index, count, _size);
3199
3200                         Array.Sort(_items, index, count, comparer);
3201                 }
3202
3203                 public virtual object[] ToArray() 
3204                 {
3205                         object[] retval;
3206
3207                         retval = new object[_size];
3208
3209                         CopyTo(retval);
3210                         
3211                         return retval;
3212                 }
3213
3214                 public virtual Array ToArray(Type elementType) 
3215                 {
3216                         Array retval;
3217                         
3218                         retval = Array.CreateInstance(elementType, _size);
3219
3220                         CopyTo(retval);
3221
3222                         return retval;
3223                 }
3224
3225                 public virtual object Clone() 
3226                 {
3227                         return new ArrayList(this._items, 0, this._size);
3228                 }
3229
3230                 #endregion
3231
3232                 #region Static Methods
3233
3234                 /// <summary>
3235                 /// Does a check of the arguments many of the methods in ArrayList use.
3236                 /// </summary>
3237                 /// <remarks>
3238                 /// The choice of exceptions thrown sometimes seem to be arbitrarily chosen so
3239                 /// not all methods actually make use of CheckRange.
3240                 /// </remarks>
3241                 internal static void CheckRange(int index, int count, int listCount) 
3242                 {
3243                         if (index < 0) 
3244                         {
3245                                 throw new ArgumentOutOfRangeException("index", index, "Can't be less than 0.");
3246                         }
3247
3248                         if (count < 0) 
3249                         {
3250                                 throw new ArgumentOutOfRangeException("count", count, "Can't be less than 0.");
3251                         }
3252
3253                         // re-ordered to avoid possible integer overflow
3254                         if (index > listCount - count) 
3255                         {
3256                                 throw new ArgumentException("Index and count do not denote a valid range of elements.", "index");
3257                         }
3258                 }
3259
3260                 public static ArrayList Adapter(IList list) 
3261                 {
3262                         // LAMESPEC: EWWW.  Other lists aren't *Array*Lists.
3263
3264                         if (list == null) 
3265                         {
3266                                 throw new ArgumentNullException("list");
3267                         }
3268
3269                         if (list.IsSynchronized) 
3270                         {
3271                                 return ArrayList.Synchronized(new ArrayListAdapter(list));
3272                         }
3273                         else 
3274                         {
3275                                 return new ArrayListAdapter(list);
3276                         }
3277                 }
3278
3279                 public static ArrayList Synchronized(ArrayList arrayList) 
3280                 {
3281                         if (arrayList == null) 
3282                         {
3283                                 throw new ArgumentNullException("arrayList");
3284                         }
3285
3286                         if (arrayList.IsSynchronized)
3287                         {
3288                                 return arrayList;
3289                         }
3290
3291                         return new SynchronizedArrayListWrapper(arrayList);
3292                 }
3293
3294                 public static IList Synchronized(IList list) 
3295                 {
3296                         if (list == null) 
3297                         {
3298                                 throw new ArgumentNullException("list");
3299                         }
3300
3301                         if (list.IsSynchronized)
3302                         {
3303                                 return list;
3304                         }
3305
3306                         return new SynchronizedListWrapper(list);
3307                 }
3308
3309                 public static ArrayList ReadOnly(ArrayList arrayList) 
3310                 {
3311                         if (arrayList == null) 
3312                         {
3313                                 throw new ArgumentNullException("arrayList");
3314                         }
3315
3316                         if (arrayList.IsReadOnly)
3317                         {
3318                                 return arrayList;
3319                         }
3320
3321                         return new ReadOnlyArrayListWrapper(arrayList);
3322                 }
3323
3324                 public static IList ReadOnly(IList list) 
3325                 {
3326                         if (list == null) 
3327                         {
3328                                 throw new ArgumentNullException("list");
3329                         }
3330
3331                         if (list.IsReadOnly)
3332                         {
3333                                 return list;
3334                         }
3335
3336                         return new ReadOnlyListWrapper(list);
3337                 }
3338
3339                 public static ArrayList FixedSize(ArrayList arrayList) 
3340                 {
3341                         if (arrayList == null) 
3342                         {
3343                                 throw new ArgumentNullException("arrayList");
3344                         }
3345
3346                         if (arrayList.IsFixedSize)
3347                         {
3348                                 return arrayList;
3349                         }
3350
3351                         return new FixedSizeArrayListWrapper(arrayList);
3352                 }
3353
3354                 public static IList FixedSize(IList list) 
3355                 {
3356                         if (list == null) 
3357                         {
3358                                 throw new ArgumentNullException("list");
3359                         }
3360
3361                         if (list.IsFixedSize)
3362                         {
3363                                 return list;
3364                         }
3365
3366                         return new FixedSizeListWrapper(list);
3367                 }
3368
3369                 public static ArrayList Repeat(object value, int count) 
3370                 {
3371                         ArrayList arrayList = new ArrayList(count);
3372
3373                         for (int i = 0; i < count; i++) 
3374                         {
3375                                 arrayList.Add(value);
3376                         }
3377
3378                         return arrayList;
3379                 }
3380
3381                 #endregion
3382         }
3383 }