**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / System.Collections.Generic / List.cs
blobb41f9f4090011ebb224ac7685341dac5d7ec23fe
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 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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.
34 #if NET_2_0
35 using System;
36 using System.Collections;
37 using System.Runtime.InteropServices;
39 namespace System.Collections.Generic
41 [CLSCompliant(false)]
42 [ComVisible(false)]
43 public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>,
44 IList, ICollection, IEnumerable
46 protected int count;
47 protected int capacity;
48 protected T[] contents;
49 protected int modified;
51 public List ()
55 public List (int capacity)
57 this.capacity = capacity;
58 contents = new T [capacity];
61 public List (ICollection collection)
62 : this (collection.Count)
64 collection.CopyTo (contents, 0);
65 count = collection.Count;
68 protected void Resize (int size)
70 if (size < capacity)
71 return;
73 if (size < 10)
74 size = 10;
76 T[] ncontents = new T [size];
77 if (count > 0)
78 Array.Copy (contents, 0, ncontents, 0, count);
80 modified++;
81 contents = ncontents;
82 capacity = size;
85 public void Add (T item)
87 if (count >= capacity)
88 Resize (2 * capacity);
90 contents [count] = item;
91 count++;
94 int IList.Add (object item)
96 if (count >= capacity)
97 Resize (2 * capacity);
99 contents [count] = (T) item;
100 return count++;
103 public void Clear ()
105 count = 0;
108 public bool Contains (T item)
110 for (int i = 0; i < count; i++)
111 if (contents [i] == item)
112 return true;
114 return false;
117 bool IList.Contains (object item)
119 return Contains ((T) item);
122 public int IndexOf (T item)
124 for (int i = 0; i < count; i++)
125 if (contents [i] == item)
126 return i;
128 return -1;
131 int IList.IndexOf (object item)
133 return IndexOf ((T) item);
136 public void Insert (int index, T item)
138 if (index < 0)
139 throw new ArgumentException ();
140 if (index > count)
141 index = count;
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;
150 void IList.Insert (int index, object item)
152 Insert (index, (T) item);
155 public bool Remove (T item)
157 int index = IndexOf (item);
158 if (index < 0)
159 return false;
161 RemoveAt (index);
162 return true;
165 void IList.Remove (object item)
167 Remove ((T) item);
170 public void RemoveAt (int index)
172 if ((index < 0) || (count == 0))
173 throw new ArgumentException ();
174 if (index > count)
175 index = count;
177 int rest = count - index;
178 if (rest > 0)
179 Array.Copy (contents, index+1, contents, index, rest);
181 count--;
184 public bool IsFixedSize {
185 get {
186 return false;
190 public bool IsReadOnly {
191 get {
192 return false;
196 public T this [int index] {
197 get {
198 return contents [index];
201 set {
202 contents [index] = value;
206 object IList.this [int index] {
207 get {
208 return contents [index];
211 set {
212 // contents [index] = (T) value;
216 public void CopyTo (T[] array, int arrayIndex)
218 if (count > 0)
219 Array.Copy (contents, 0, array, arrayIndex, count);
222 void ICollection.CopyTo (Array array, int arrayIndex)
224 if (count > 0)
225 Array.Copy (contents, 0, array, arrayIndex, count);
228 public int Count {
229 get {
230 return count;
234 public bool IsSynchronized {
235 get { return false; }
238 public object SyncRoot {
239 get { return this; }
242 public Enumerator GetEnumerator ()
244 return new Enumerator (this);
247 IEnumerator<T> IEnumerable<T>.GetEnumerator ()
249 return new Enumerator (this);
252 IEnumerator IEnumerable.GetEnumerator ()
254 return new Enumerator (this);
257 public struct Enumerator : IEnumerator<T>, IEnumerator
259 List<T> list;
260 int modified;
261 int current;
263 public Enumerator (List<T> list)
265 this.list = list;
266 this.modified = list.modified;
267 this.current = -1;
270 public T Current {
271 get {
272 if (list.modified != modified)
273 throw new InvalidOperationException ();
274 if (current < 0)
275 current = 0;
276 if (current > list.count)
277 throw new ArgumentException ();
278 return list.contents [current];
282 object IEnumerator.Current {
283 get {
284 return Current;
288 public bool MoveNext ()
290 if (list.modified != modified)
291 throw new InvalidOperationException ();
293 current++;
294 return current < list.count;
297 public void Reset ()
299 if (list.modified != modified)
300 throw new InvalidOperationException ();
302 current = -1;
305 public void Dispose ()
307 modified = -1;
312 #endif