2e0c451393429907b5825d2fe34acef9c945dbfe
[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 void Add (T item)
86                 {
87                         if (count >= capacity)
88                                 Resize (2 * capacity);
89
90                         contents [count] = item;
91                         count++;
92                 }
93
94                 int IList.Add (object item)
95                 {
96                         if (count >= capacity)
97                                 Resize (2 * capacity);
98
99                         contents [count] = (T) item;
100                         return count++;
101                 }
102
103                 public void Clear ()
104                 {
105                         count = 0;
106                 }
107
108                 public bool Contains (T item)
109                 {
110                         for (int i = 0; i < count; i++)
111                                 if (contents [i] == item)
112                                         return true;
113
114                         return false;
115                 }
116
117                 bool IList.Contains (object item)
118                 {
119                         return Contains ((T) item);
120                 }
121
122                 public int IndexOf (T item)
123                 {
124                         for (int i = 0; i < count; i++)
125                                 if (contents [i] == item)
126                                         return i;
127
128                         return -1;
129                 }
130
131                 int IList.IndexOf (object item)
132                 {
133                         return IndexOf ((T) item);
134                 }
135
136                 public void Insert (int index, T item)
137                 {
138                         if (index < 0)
139                                 throw new ArgumentException ();
140                         if (index > count)
141                                 index = count;
142
143                         Resize (index);
144                         int rest = count - index;
145                         if (rest > 0)
146                                 Array.Copy (contents, index, contents, index+1, rest);
147                         contents [index] = item;
148                 }
149
150                 void IList.Insert (int index, object item)
151                 {
152                         Insert (index, (T) item);
153                 }
154
155                 public void Remove (T item)
156                 {
157                         int index = IndexOf (item);
158                         if (index >= 0)
159                                 RemoveAt (index);
160                 }
161
162                 void IList.Remove (object item)
163                 {
164                         Remove ((T) item);
165                 }
166
167                 public void RemoveAt (int index)
168                 {
169                         if ((index < 0) || (count == 0))
170                                 throw new ArgumentException ();
171                         if (index > count)
172                                 index = count;
173
174                         int rest = count - index;
175                         if (rest > 0)
176                                 Array.Copy (contents, index+1, contents, index, rest);
177
178                         count--;
179                 }
180
181                 public bool IsFixedSize {
182                         get {
183                                 return false;
184                         }
185                 }
186
187                 public bool IsReadOnly {
188                         get {
189                                 return false;
190                         }
191                 }
192
193                 public T this [int index] {
194                         get {
195                                 return contents [index];
196                         }
197
198                         set {
199                                 contents [index] = value;
200                         }
201                 }
202
203                 object IList.this [int index] {
204                         get {
205                                 return contents [index];
206                         }
207
208                         set {
209                                 // contents [index] = (T) value;
210                         }
211                 }
212
213                 public void CopyTo (T[] array, int arrayIndex)
214                 {
215                         if (count > 0)
216                                 Array.Copy (contents, 0, array, arrayIndex, count);
217                 }
218
219                 void ICollection.CopyTo (Array array, int arrayIndex)
220                 {
221                         if (count > 0)
222                                 Array.Copy (contents, 0, array, arrayIndex, count);
223                 }
224
225                 public int Count {
226                         get {
227                                 return count;
228                         }
229                 }
230
231                 public bool IsSynchronized {
232                         get { return false; }
233                 }
234
235                 public object SyncRoot {
236                         get { return this; }
237                 }
238
239                 public IEnumerator<T> GetEnumerator ()
240                 {
241                         return new Enumerator (this);
242                 }
243
244                 IEnumerator IEnumerable.GetEnumerator ()
245                 {
246                         return new Enumerator (this);
247                 }
248
249                 public class Enumerator : IEnumerator<T>, IEnumerator
250                 {
251                         List<T> list;
252                         int modified;
253                         int current;
254
255                         public Enumerator (List<T> list)
256                         {
257                                 this.list = list;
258                                 this.modified = list.modified;
259                                 this.current = -1;
260                         }
261
262                         public T Current {
263                                 get {
264                                         if (list.modified != modified)
265                                                 throw new InvalidOperationException ();
266                                         if (current < 0)
267                                                 current = 0;
268                                         if (current > list.count)
269                                                 throw new ArgumentException ();
270                                         return list.contents [current];
271                                 }
272                         }
273
274                         object IEnumerator.Current {
275                                 get {
276                                         return Current;
277                                 }
278                         }
279
280                         public bool MoveNext ()
281                         {
282                                 if (list.modified != modified)
283                                         throw new InvalidOperationException ();
284
285                                 current++;
286                                 return current < list.count;
287                         }
288
289                         public void Reset ()
290                         {
291                                 if (list.modified != modified)
292                                         throw new InvalidOperationException ();
293
294                                 current = -1;
295                         }
296
297                         public void Dispose ()
298                         {
299                                 modified = -1;
300                         }
301                 }
302         }
303 }
304 #endif