| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690 |
- //
- // System.Collections.Generic.List
- //
- // Authors:
- // Ben Maurer ([email protected])
- // Martin Baulig ([email protected])
- // Carlos Alberto Cortez ([email protected])
- // David Waite ([email protected])
- //
- // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
- // Copyright (C) 2005 David Waite
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- #if NET_2_0
- using System.Collections.ObjectModel;
- using System.Runtime.InteropServices;
- namespace System.Collections.Generic {
- [Serializable]
- public class List <T> : IList <T>, IList, ICollection {
- T [] _items;
- int _size;
- int _version;
-
- static readonly T [] EmptyArray = new T [0];
- const int DefaultCapacity = 4;
-
- public List ()
- {
- _items = EmptyArray;
- }
-
- public List (IEnumerable <T> collection)
- {
- CheckCollection (collection);
- // initialize to needed size (if determinable)
- ICollection <T> c = collection as ICollection <T>;
- if (c == null)
- {
- _items = EmptyArray;
- AddEnumerable (collection);
- }
- else
- {
- _items = new T [c.Count];
- AddCollection (c);
- }
- }
-
- public List (int capacity)
- {
- if (capacity < 0)
- throw new ArgumentOutOfRangeException ("capacity");
- _items = new T [capacity];
- }
-
- internal List (T [] data, int size)
- {
- _items = data;
- _size = size;
- }
- public void Add (T item)
- {
- GrowIfNeeded (1);
- _items [_size ++] = item;
- _version++;
- }
-
- void GrowIfNeeded (int newCount)
- {
- int minimumSize = _size + newCount;
- if (minimumSize > _items.Length)
- Capacity = Math.Max (Math.Max (Capacity * 2, DefaultCapacity), minimumSize);
- }
-
- void CheckRange (int idx, int count)
- {
- if (idx < 0)
- throw new ArgumentOutOfRangeException ("index");
-
- if (count < 0)
- throw new ArgumentOutOfRangeException ("count");
- if ((uint) idx + (uint) count > (uint) _size)
- throw new ArgumentException ("index and count exceed length of list");
- }
-
- void AddCollection (ICollection <T> collection)
- {
- int collectionCount = collection.Count;
- GrowIfNeeded (collectionCount);
- collection.CopyTo (_items, _size);
- _size += collectionCount;
- }
- void AddEnumerable (IEnumerable <T> enumerable)
- {
- foreach (T t in enumerable)
- {
- Add (t);
- }
- }
- public void AddRange (IEnumerable <T> collection)
- {
- CheckCollection (collection);
-
- ICollection <T> c = collection as ICollection <T>;
- if (c != null)
- AddCollection (c);
- else
- AddEnumerable (collection);
- _version++;
- }
-
- public ReadOnlyCollection <T> AsReadOnly ()
- {
- return new ReadOnlyCollection <T> (this);
- }
-
- public int BinarySearch (T item)
- {
- return Array.BinarySearch <T> (_items, 0, _size, item);
- }
-
- public int BinarySearch (T item, IComparer <T> comparer)
- {
- return Array.BinarySearch <T> (_items, 0, _size, item, comparer);
- }
-
- public int BinarySearch (int index, int count, T item, IComparer <T> comparer)
- {
- CheckRange (index, count);
- return Array.BinarySearch <T> (_items, index, count, item, comparer);
- }
-
- public void Clear ()
- {
- Array.Clear (_items, 0, _items.Length);
- _size = 0;
- _version++;
- }
-
- public bool Contains (T item)
- {
- EqualityComparer<T> equalityComparer = EqualityComparer<T>.Default;
- for (int i = 0; i < _size; i++)
- if (equalityComparer.Equals (_items[i], item))
- return true;
- return false;
- }
-
- public List <TOutput> ConvertAll <TOutput> (Converter <T, TOutput> converter)
- {
- if (converter == null)
- throw new ArgumentNullException ("converter");
- List <TOutput> u = new List <TOutput> (_size);
- foreach (T t in this)
- u.Add (converter (t));
- return u;
- }
-
- public void CopyTo (T [] array)
- {
- Array.Copy (_items, 0, array, 0, _size);
- }
-
- public void CopyTo (T [] array, int arrayIndex)
- {
- Array.Copy (_items, 0, array, arrayIndex, _size);
- }
-
- public void CopyTo (int index, T [] array, int arrayIndex, int count)
- {
- CheckRange (index, count);
- Array.Copy (_items, index, array, arrayIndex, count);
- }
- public bool Exists (Predicate <T> match)
- {
- return FindIndex (match) != -1;
- }
-
- public T Find (Predicate <T> match)
- {
- int i = FindIndex (match);
- return (i != -1) ? _items [i] : default (T);
- }
- void CheckMatch (Predicate <T> match)
- {
- if (match == null)
- throw new ArgumentNullException ("match");
- }
-
- // Maybe we could make this faster. For example, you could
- // make a bit set with stackalloc for which elements to copy
- // then you could size the array correctly.
- public List <T> FindAll (Predicate <T> match)
- {
- CheckMatch (match);
- List <T> f = new List <T> ();
-
- foreach (T t in this)
- if (match (t))
- f.Add (t);
-
- return f;
- }
-
- public int FindIndex (Predicate <T> match)
- {
- CheckMatch (match);
- return GetIndex (0, _size, match);
- }
-
- public int FindIndex (int startIndex, Predicate <T> match)
- {
- CheckMatch (match);
- CheckIndex (startIndex);
- return GetIndex (startIndex, _size - startIndex, match);
- }
- public int FindIndex (int startIndex, int count, Predicate <T> match)
- {
- CheckMatch (match);
- CheckRange (startIndex, count);
- return GetIndex (startIndex, count, match);
- }
- int GetIndex (int startIndex, int count, Predicate <T> match)
- {
- for (int i = startIndex; i < startIndex + count; i ++)
- if (match (_items [i]))
- return i;
-
- return -1;
- }
-
- public T FindLast (Predicate <T> match)
- {
- CheckMatch (match);
- int i = GetLastIndex (0, _size, match);
- return i == -1 ? default (T) : this [i];
- }
-
- public int FindLastIndex (Predicate <T> match)
- {
- CheckMatch (match);
- return GetLastIndex (0, _size, match);
- }
-
- public int FindLastIndex (int startIndex, Predicate <T> match)
- {
- CheckMatch (match);
- CheckIndex (startIndex);
- return GetLastIndex (0, startIndex + 1, match);
- }
-
- public int FindLastIndex (int startIndex, int count, Predicate <T> match)
- {
- CheckMatch (match);
- int start = startIndex - count + 1;
- CheckRange (start, count);
- return GetLastIndex (start, count, match);
- }
- int GetLastIndex (int startIndex, int count, Predicate <T> match)
- {
- // unlike FindLastIndex, takes regular params for search range
- for (int i = startIndex + count; i != startIndex;)
- if (match (_items [--i]))
- return i;
- return -1;
- }
-
- public void ForEach (Action <T> action)
- {
- if (action == null)
- throw new ArgumentNullException ("action");
- foreach (T t in this)
- action (t);
- }
-
- public Enumerator GetEnumerator ()
- {
- return new Enumerator (this);
- }
-
- public List <T> GetRange (int index, int count)
- {
- CheckRange (index, count);
- T [] tmpArray = new T [count];
- Array.Copy (_items, index, tmpArray, 0, count);
- return new List <T> (tmpArray, count);
- }
-
- public int IndexOf (T item)
- {
- return Array.IndexOf<T> (_items, item, 0, _size);
- }
-
- public int IndexOf (T item, int index)
- {
- CheckIndex (index);
- return Array.IndexOf<T> (_items, item, index, _size - index);
- }
-
- public int IndexOf (T item, int index, int count)
- {
- if (index < 0)
- throw new ArgumentOutOfRangeException ("index");
-
- if (count < 0)
- throw new ArgumentOutOfRangeException ("count");
- if ((uint) index + (uint) count > (uint) _size)
- throw new ArgumentOutOfRangeException ("index and count exceed length of list");
- return Array.IndexOf<T> (_items, item, index, count);
- }
-
- void Shift (int start, int delta)
- {
- if (delta < 0)
- start -= delta;
-
- Array.Copy (_items, start, _items, start + delta, _size - start);
-
- _size += delta;
- }
- void CheckIndex (int index)
- {
- if (index < 0 || (uint) index > (uint) _size)
- throw new ArgumentOutOfRangeException ("index");
- }
-
- public void Insert (int index, T item)
- {
- CheckIndex (index);
- GrowIfNeeded (1);
- Shift (index, 1);
- this [index] = item;
- _version++;
- }
- void CheckCollection (IEnumerable <T> collection)
- {
- if (collection == null)
- throw new ArgumentNullException ("collection");
- }
-
- public void InsertRange (int index, IEnumerable <T> collection)
- {
- CheckCollection (collection);
- CheckIndex (index);
- if (collection == this) {
- T[] buffer = new T[_size];
- CopyTo (buffer, 0);
- GrowIfNeeded (_size);
- Shift (index, buffer.Length);
- Array.Copy (buffer, 0, _items, index, buffer.Length);
- } else {
- ICollection <T> c = collection as ICollection <T>;
- if (c != null)
- InsertCollection (index, c);
- else
- InsertEnumeration (index, collection);
- }
- _version++;
- }
- void InsertCollection (int index, ICollection <T> collection)
- {
- int collectionCount = collection.Count;
- GrowIfNeeded (collectionCount);
-
- Shift (index, collectionCount);
- collection.CopyTo (_items, index);
- }
- void InsertEnumeration (int index, IEnumerable <T> enumerable)
- {
- foreach (T t in enumerable)
- Insert (index++, t);
- }
- public int LastIndexOf (T item)
- {
- return Array.LastIndexOf<T> (_items, item, _size - 1, _size);
- }
-
- public int LastIndexOf (T item, int index)
- {
- CheckIndex (index);
- return Array.LastIndexOf<T> (_items, item, index, index + 1);
- }
-
- public int LastIndexOf (T item, int index, int count)
- {
- if (index < 0)
- throw new ArgumentOutOfRangeException ("index", index, "index is negative");
- if (count < 0)
- throw new ArgumentOutOfRangeException ("count", count, "count is negative");
- if (index - count + 1 < 0)
- throw new ArgumentOutOfRangeException ("cound", count, "count is too large");
- return Array.LastIndexOf<T> (_items, item, index, count);
- }
-
- public bool Remove (T item)
- {
- int loc = IndexOf (item);
- if (loc != -1)
- RemoveAt (loc);
-
- return loc != -1;
- }
-
- // FIXME: this could probably be made faster.
- public int RemoveAll (Predicate <T> match)
- {
- CheckMatch (match);
- int index = 0;
- int c = 0;
- while ((index = GetIndex (index, _size - index, match)) != -1) {
- RemoveAt (index);
- c ++;
- }
-
- Array.Clear (_items, _size, c);
- return c;
- }
-
- public void RemoveAt (int index)
- {
- CheckIndex (index);
- Shift (index, -1);
- Array.Clear (_items, _size, 0);
- _version++;
- }
-
- public void RemoveRange (int index, int count)
- {
- CheckRange (index, count);
- if (count > 0) {
- Shift (index, -count);
- Array.Clear (_items, _size, count);
- _version++;
- }
- }
-
- public void Reverse ()
- {
- Array.Reverse (_items, 0, _size);
- _version++;
- }
- public void Reverse (int index, int count)
- {
- CheckRange (index, count);
- Array.Reverse (_items, index, count);
- _version++;
- }
-
- public void Sort ()
- {
- Array.Sort<T> (_items, 0, _size, Comparer <T>.Default);
- _version++;
- }
- public void Sort (IComparer <T> comparer)
- {
- Array.Sort<T> (_items, 0, _size, comparer);
- _version++;
- }
- public void Sort (Comparison <T> comparison)
- {
- Array.Sort<T> (_items, _size, comparison);
- _version++;
- }
-
- public void Sort (int index, int count, IComparer <T> comparer)
- {
- CheckRange (index, count);
- Array.Sort<T> (_items, index, count, comparer);
- _version++;
- }
- public T [] ToArray ()
- {
- T [] t = new T [_size];
- Array.Copy (_items, t, _size);
-
- return t;
- }
-
- public void TrimExcess ()
- {
- Capacity = _size;
- }
-
- public bool TrueForAll (Predicate <T> match)
- {
- CheckMatch (match);
- foreach (T t in this)
- if (!match (t))
- return false;
-
- return true;
- }
-
- public int Capacity {
- get {
- return _items.Length;
- }
- set {
- if ((uint) value < (uint) _size)
- throw new ArgumentOutOfRangeException ();
-
- Array.Resize (ref _items, value);
- }
- }
-
- public int Count {
- get { return _size; }
- }
-
- public T this [int index] {
- get {
- if ((uint) index >= (uint) _size)
- throw new ArgumentOutOfRangeException ("index");
- return _items [index];
- }
- set {
- CheckIndex (index);
- _items [index] = value;
- }
- }
-
- #region Interface implementations.
- IEnumerator <T> IEnumerable <T>.GetEnumerator ()
- {
- return GetEnumerator ();
- }
-
- void ICollection.CopyTo (Array array, int arrayIndex)
- {
- Array.Copy (_items, 0, array, arrayIndex, _size);
- }
-
- IEnumerator IEnumerable.GetEnumerator ()
- {
- return GetEnumerator ();
- }
-
- int IList.Add (object item)
- {
- Add ((T) item);
- return _size - 1;
- }
-
- bool IList.Contains (object item)
- {
- return Contains ((T) item);
- }
-
- int IList.IndexOf (object item)
- {
- return IndexOf ((T) item);
- }
-
- void IList.Insert (int index, object item)
- {
- Insert (index, (T) item);
- }
-
- void IList.Remove (object item)
- {
- Remove ((T) item);
- }
-
- bool ICollection <T>.IsReadOnly {
- get { return false; }
- }
- bool ICollection.IsSynchronized {
- get { return false; }
- }
-
- object ICollection.SyncRoot {
- get { return this; }
- }
- bool IList.IsFixedSize {
- get { return false; }
- }
-
- bool IList.IsReadOnly {
- get { return false; }
- }
-
- object IList.this [int index] {
- get { return this [index]; }
- set { this [index] = (T) value; }
- }
- #endregion
-
- [Serializable]
- public struct Enumerator : IEnumerator <T>, IDisposable {
- const int NOT_STARTED = -2;
-
- // this MUST be -1, because we depend on it in move next.
- // we just decr the size, so, 0 - 1 == FINISHED
- const int FINISHED = -1;
-
- List <T> l;
- int idx;
- int ver;
-
- internal Enumerator (List <T> l)
- {
- this.l = l;
- idx = NOT_STARTED;
- ver = l._version;
- }
-
- // for some fucked up reason, MSFT added a useless dispose to this class
- // It means that in foreach, we must still do a try/finally. Broken, very
- // broken.
- public void Dispose ()
- {
- idx = NOT_STARTED;
- }
-
- public bool MoveNext ()
- {
- if (ver != l._version)
- throw new InvalidOperationException ("Collection was modified;"
- + "enumeration operation may not execute.");
-
- if (idx == NOT_STARTED)
- idx = l._size;
-
- return idx != FINISHED && -- idx != FINISHED;
- }
-
- public T Current {
- get {
- if (idx < 0)
- throw new InvalidOperationException ();
-
- return l._items [l._size - 1 - idx];
- }
- }
-
- void IEnumerator.Reset ()
- {
- if (ver != l._version)
- throw new InvalidOperationException ("Collection was modified;"
- + "enumeration operation may not execute.");
-
- idx = NOT_STARTED;
- }
-
- object IEnumerator.Current {
- get { return Current; }
- }
- }
- }
- }
- #endif
|