This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / corlib / System.Collections.Generic / List.cs
1 // -*- Mode: csharp; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 //
3 // System.Collections.Generic.List
4 //
5 // Author:
6 //    Martin Baulig (martin@ximian.com)
7 //
8 // (C) 2004 Novell, Inc.
9 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 #if NET_2_0
35 using System;
36 using System.Collections;
37 using System.Runtime.InteropServices;
38
39 namespace System.Collections.Generic
40 {
41         [CLSCompliant(false)]
42         [ComVisible(false)]
43         public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>,
44                 IList, ICollection, IEnumerable
45         {
46                 protected int count;
47                 protected int capacity;
48                 protected T[] contents;
49                 protected int modified;
50
51                 public List ()
52                 {
53                 }
54
55                 public List (int capacity)
56                 {
57                         this.capacity = capacity;
58                         contents = new T [capacity];
59                 }
60
61                 public List (ICollection collection)
62                         : this (collection.Count)
63                 {
64                         collection.CopyTo (contents, 0);
65                         count = collection.Count;
66                 }
67
68                 protected void Resize (int size)
69                 {
70                         if (size < capacity)
71                                 return;
72
73                         if (size < 10)
74                                 size = 10;
75
76                         T[] ncontents = new T [size];
77                         if (count > 0)
78                                 Array.Copy (contents, 0, ncontents, 0, count);
79
80                         modified++;
81                         contents = ncontents;
82                         capacity = size;
83                 }
84
85                 public int Add (T item)
86                 {
87                         if (count >= capacity)
88                                 Resize (2 * capacity);
89
90                         contents [count] = item;
91                         return count++;
92                 }
93
94                 int IList.Add (object item)
95                 {
96                         return Add ((T) item);
97                 }
98
99                 public void Clear ()
100                 {
101                         count = 0;
102                 }
103
104                 public bool Contains (T item)
105                 {
106                         for (int i = 0; i < count; i++)
107                                 if (contents [i] == item)
108                                         return true;
109
110                         return false;
111                 }
112
113                 bool IList.Contains (object item)
114                 {
115                         return Contains ((T) item);
116                 }
117
118                 public int IndexOf (T item)
119                 {
120                         for (int i = 0; i < count; i++)
121                                 if (contents [i] == item)
122                                         return i;
123
124                         return -1;
125                 }
126
127                 int IList.IndexOf (object item)
128                 {
129                         return IndexOf ((T) item);
130                 }
131
132                 public void Insert (int index, T item)
133                 {
134                         if (index < 0)
135                                 throw new ArgumentException ();
136                         if (index > count)
137                                 index = count;
138
139                         Resize (index);
140                         int rest = count - index;
141                         if (rest > 0)
142                                 Array.Copy (contents, index, contents, index+1, rest);
143                         contents [index] = item;
144                 }
145
146                 void IList.Insert (int index, object item)
147                 {
148                         Insert (index, (T) item);
149                 }
150
151                 public void Remove (T item)
152                 {
153                         int index = IndexOf (item);
154                         if (index >= 0)
155                                 RemoveAt (index);
156                 }
157
158                 void IList.Remove (object item)
159                 {
160                         Remove ((T) item);
161                 }
162
163                 public void RemoveAt (int index)
164                 {
165                         if ((index < 0) || (count == 0))
166                                 throw new ArgumentException ();
167                         if (index > count)
168                                 index = count;
169
170                         int rest = count - index;
171                         if (rest > 0)
172                                 Array.Copy (contents, index+1, contents, index, rest);
173
174                         count--;
175                 }
176
177                 public bool IsFixedSize {
178                         get {
179                                 return false;
180                         }
181                 }
182
183                 public bool IsReadOnly {
184                         get {
185                                 return false;
186                         }
187                 }
188
189                 public T this [int index] {
190                         get {
191                                 return contents [index];
192                         }
193
194                         set {
195                                 contents [index] = value;
196                         }
197                 }
198
199                 object IList.this [int index] {
200                         get {
201                                 return contents [index];
202                         }
203
204                         set {
205                                 // contents [index] = (T) value;
206                         }
207                 }
208
209                 public void CopyTo (T[] array, int arrayIndex)
210                 {
211                         if (count > 0)
212                                 Array.Copy (contents, 0, array, arrayIndex, count);
213                 }
214
215                 void ICollection.CopyTo (Array array, int arrayIndex)
216                 {
217                         if (count > 0)
218                                 Array.Copy (contents, 0, array, arrayIndex, count);
219                 }
220
221                 public int Count {
222                         get {
223                                 return count;
224                         }
225                 }
226
227                 public bool IsSynchronized {
228                         get { return false; }
229                 }
230
231                 public object SyncRoot {
232                         get { return this; }
233                 }
234
235                 public IEnumerator<T> GetEnumerator ()
236                 {
237                         return new Enumerator (this);
238                 }
239
240                 IEnumerator IEnumerable.GetEnumerator ()
241                 {
242                         return new Enumerator (this);
243                 }
244
245                 protected class Enumerator : IEnumerator<T>, IEnumerator
246                 {
247                         List<T> list;
248                         int modified;
249                         int current;
250
251                         public Enumerator (List<T> list)
252                         {
253                                 this.list = list;
254                                 this.modified = list.modified;
255                                 this.current = -1;
256                         }
257
258                         public T Current {
259                                 get {
260                                         if (list.modified != modified)
261                                                 throw new InvalidOperationException ();
262                                         if (current < 0)
263                                                 current = 0;
264                                         if (current > list.count)
265                                                 throw new ArgumentException ();
266                                         return list.contents [current];
267                                 }
268                         }
269
270                         object IEnumerator.Current {
271                                 get {
272                                         return Current;
273                                 }
274                         }
275
276                         public bool MoveNext ()
277                         {
278                                 if (list.modified != modified)
279                                         throw new InvalidOperationException ();
280
281                                 current++;
282                                 return current < list.count;
283                         }
284
285                         public void Reset ()
286                         {
287                                 if (list.modified != modified)
288                                         throw new InvalidOperationException ();
289
290                                 current = -1;
291                         }
292
293                         public void Dispose ()
294                         {
295                                 modified = -1;
296                         }
297                 }
298         }
299 }
300 #endif