List.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. //
  2. // System.Collections.Generic.List
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. // Martin Baulig ([email protected])
  7. // Carlos Alberto Cortez ([email protected])
  8. // David Waite ([email protected])
  9. //
  10. // (C) 2004 Novell, Inc.
  11. // (C) 2005 David Waite
  12. //
  13. //
  14. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  15. // Copyright (C) 2005 David Waite
  16. //
  17. // Permission is hereby granted, free of charge, to any person obtaining
  18. // a copy of this software and associated documentation files (the
  19. // "Software"), to deal in the Software without restriction, including
  20. // without limitation the rights to use, copy, modify, merge, publish,
  21. // distribute, sublicense, and/or sell copies of the Software, and to
  22. // permit persons to whom the Software is furnished to do so, subject to
  23. // the following conditions:
  24. //
  25. // The above copyright notice and this permission notice shall be
  26. // included in all copies or substantial portions of the Software.
  27. //
  28. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  29. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  30. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  31. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  32. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  33. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  34. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  35. //
  36. #if NET_2_0
  37. using System;
  38. using System.Collections;
  39. using System.Collections.ObjectModel;
  40. using System.Runtime.InteropServices;
  41. namespace System.Collections.Generic {
  42. [Serializable]
  43. public class List <T> : IList <T>, IList, ICollection {
  44. T [] data;
  45. int size;
  46. int version;
  47. static readonly T [] EmptyArray = new T [0];
  48. const int DefaultCapacity = 4;
  49. public List ()
  50. {
  51. data = EmptyArray;
  52. }
  53. public List (IEnumerable <T> collection)
  54. {
  55. CheckCollection (collection);
  56. // initialize to needed size (if determinable)
  57. ICollection <T> c = collection as ICollection <T>;
  58. if (c == null)
  59. {
  60. data = EmptyArray;
  61. AddEnumerable (collection);
  62. }
  63. else
  64. {
  65. data = new T [c.Count];
  66. AddCollection (c);
  67. }
  68. }
  69. public List (int capacity)
  70. {
  71. if (capacity < 0)
  72. throw new ArgumentOutOfRangeException ("capacity");
  73. data = new T [capacity];
  74. }
  75. internal List (T [] data, int size)
  76. {
  77. this.data = data;
  78. this.size = size;
  79. }
  80. public void Add (T item)
  81. {
  82. GrowIfNeeded (1);
  83. data [size ++] = item;
  84. }
  85. void GrowIfNeeded (int newCount)
  86. {
  87. int minimumSize = size + newCount;
  88. if (minimumSize > data.Length)
  89. Capacity = Math.Max (Math.Max (Capacity * 2, DefaultCapacity), minimumSize);
  90. }
  91. void CheckRange (int idx, int count)
  92. {
  93. if (idx < 0)
  94. throw new ArgumentOutOfRangeException ("index");
  95. if (count < 0)
  96. throw new ArgumentOutOfRangeException ("count");
  97. if ((uint) idx + (uint) count > (uint) size)
  98. throw new ArgumentException ("index and count exceed length of list");
  99. }
  100. void AddCollection (ICollection <T> collection)
  101. {
  102. int collectionCount = collection.Count;
  103. GrowIfNeeded (collectionCount);
  104. collection.CopyTo (data, size);
  105. size += collectionCount;
  106. }
  107. void AddEnumerable (IEnumerable <T> enumerable)
  108. {
  109. foreach (T t in enumerable)
  110. {
  111. Add (t);
  112. }
  113. }
  114. public void AddRange (IEnumerable <T> collection)
  115. {
  116. CheckCollection (collection);
  117. ICollection <T> c = collection as ICollection <T>;
  118. if (c != null)
  119. AddCollection (c);
  120. else
  121. AddEnumerable (collection);
  122. }
  123. public ReadOnlyCollection <T> AsReadOnly ()
  124. {
  125. return new ReadOnlyCollection <T> (this);
  126. }
  127. public int BinarySearch (T item)
  128. {
  129. return Array.BinarySearch (data, item);
  130. }
  131. public int BinarySearch (T item, IComparer <T> comparer)
  132. {
  133. return Array.BinarySearch (data, item, comparer);
  134. }
  135. public int BinarySearch (int index, int count, T item, IComparer <T> comparer)
  136. {
  137. CheckRange (index, count);
  138. return Array.BinarySearch (data, index, size, item, comparer);
  139. }
  140. public void Clear ()
  141. {
  142. Array.Clear (data, 0, data.Length);
  143. size = 0;
  144. }
  145. public bool Contains (T item)
  146. {
  147. return IndexOf (item) != -1;
  148. }
  149. public List <TOutput> ConvertAll <TOutput> (Converter <T, TOutput> converter)
  150. {
  151. if (converter == null)
  152. throw new ArgumentNullException ("converter");
  153. List <TOutput> u = new List <TOutput> (size);
  154. foreach (T t in this)
  155. u.Add (converter (t));
  156. return u;
  157. }
  158. public void CopyTo (T [] array)
  159. {
  160. Array.Copy (data, 0, array, 0, size);
  161. }
  162. public void CopyTo (T [] array, int arrayIndex)
  163. {
  164. Array.Copy (data, 0, array, arrayIndex, size);
  165. }
  166. public void CopyTo (int index, T [] array, int arrayIndex, int count)
  167. {
  168. CheckRange (index, count);
  169. Array.Copy (data, index, array, arrayIndex, count);
  170. }
  171. public bool Exists (Predicate <T> match)
  172. {
  173. return FindIndex (match) != -1;
  174. }
  175. public T Find (Predicate <T> match)
  176. {
  177. int i = FindIndex (match);
  178. return (i != -1) ? data [i] : default (T);
  179. }
  180. void CheckMatch (Predicate <T> match)
  181. {
  182. if (match == null)
  183. throw new ArgumentNullException ("match");
  184. }
  185. // Maybe we could make this faster. For example, you could
  186. // make a bit set with stackalloc for which elements to copy
  187. // then you could size the array correctly.
  188. public List <T> FindAll (Predicate <T> match)
  189. {
  190. CheckMatch (match);
  191. List <T> f = new List <T> ();
  192. foreach (T t in this)
  193. if (match (t))
  194. f.Add (t);
  195. return f;
  196. }
  197. public int FindIndex (Predicate <T> match)
  198. {
  199. CheckMatch (match);
  200. return GetIndex (0, size, match);
  201. }
  202. public int FindIndex (int startIndex, Predicate <T> match)
  203. {
  204. CheckMatch (match);
  205. CheckIndex (startIndex);
  206. return GetIndex (startIndex, size - startIndex, match);
  207. }
  208. public int FindIndex (int startIndex, int count, Predicate <T> match)
  209. {
  210. CheckMatch (match);
  211. CheckRange (startIndex, count);
  212. return GetIndex (startIndex, count, match);
  213. }
  214. int GetIndex (int startIndex, int count, Predicate <T> match)
  215. {
  216. for (int i = startIndex; i < startIndex + count; i ++)
  217. if (match (data [i]))
  218. return i;
  219. return -1;
  220. }
  221. public T FindLast (Predicate <T> match)
  222. {
  223. CheckMatch (match);
  224. int i = GetLastIndex (0, size, match);
  225. return i == -1 ? default (T) : this [i];
  226. }
  227. public int FindLastIndex (Predicate <T> match)
  228. {
  229. CheckMatch (match);
  230. return GetLastIndex (0, size, match);
  231. }
  232. public int FindLastIndex (int startIndex, Predicate <T> match)
  233. {
  234. CheckMatch (match);
  235. CheckIndex (startIndex);
  236. return GetLastIndex (0, startIndex + 1, match);
  237. }
  238. public int FindLastIndex (int startIndex, int count, Predicate <T> match)
  239. {
  240. CheckMatch (match);
  241. int start = startIndex - count + 1;
  242. CheckRange (start, count);
  243. return GetLastIndex (start, count, match);
  244. }
  245. int GetLastIndex (int startIndex, int count, Predicate <T> match)
  246. {
  247. // unlike FindLastIndex, takes regular params for search range
  248. for (int i = startIndex + count; i != startIndex;)
  249. if (match (data [--i]))
  250. return i;
  251. return -1;
  252. }
  253. public void ForEach (Action <T> action)
  254. {
  255. if (action == null)
  256. throw new ArgumentNullException ("action");
  257. foreach (T t in this)
  258. action (t);
  259. }
  260. public Enumerator GetEnumerator ()
  261. {
  262. return new Enumerator (this);
  263. }
  264. public List <T> GetRange (int index, int count)
  265. {
  266. CheckRange (index, count);
  267. T [] tmpArray = new T [count];
  268. Array.Copy (data, index, tmpArray, 0, count);
  269. return new List <T> (tmpArray, count);
  270. }
  271. public int IndexOf (T item)
  272. {
  273. return Array.IndexOf (data, item, 0, size);
  274. }
  275. public int IndexOf (T item, int index)
  276. {
  277. CheckIndex (index);
  278. return Array.IndexOf (data, item, index, size - index);
  279. }
  280. public int IndexOf (T item, int index, int count)
  281. {
  282. CheckRange (index, count);
  283. return Array.IndexOf (data, item, index, count);
  284. }
  285. void Shift (int start, int delta)
  286. {
  287. if (delta < 0)
  288. start -= delta;
  289. Array.Copy (data, start, data, start + delta, size - start);
  290. size += delta;
  291. }
  292. void CheckIndex (int index)
  293. {
  294. if ((uint) index >= (uint) size)
  295. throw new ArgumentOutOfRangeException ("index");
  296. }
  297. public void Insert (int index, T item)
  298. {
  299. if ((uint) index > (uint) size)
  300. throw new ArgumentOutOfRangeException ("index");
  301. GrowIfNeeded (1);
  302. Shift (index, 1);
  303. this [index] = item;
  304. }
  305. void CheckCollection (IEnumerable <T> collection)
  306. {
  307. if (collection == null)
  308. throw new ArgumentNullException ("collection");
  309. }
  310. public void InsertRange (int index, IEnumerable <T> collection)
  311. {
  312. CheckCollection (collection);
  313. CheckIndex (index);
  314. ICollection <T> c = collection as ICollection <T>;
  315. if (c != null)
  316. InsertCollection (index, c);
  317. else
  318. InsertEnumeration (index, collection);
  319. }
  320. void InsertCollection (int index, ICollection <T> collection)
  321. {
  322. int collectionCount = collection.Count;
  323. GrowIfNeeded (collectionCount);
  324. Shift (index, collectionCount);
  325. collection.CopyTo (data, index);
  326. }
  327. void InsertEnumeration (int index, IEnumerable <T> enumerable)
  328. {
  329. foreach (T t in enumerable)
  330. Insert (index++, t);
  331. }
  332. public int LastIndexOf (T item)
  333. {
  334. return Array.LastIndexOf (data, item, 0, size);
  335. }
  336. public int LastIndexOf (T item, int index)
  337. {
  338. CheckIndex (index);
  339. return Array.LastIndexOf (data, item, index, size - index);
  340. }
  341. public int LastIndexOf (T item, int index, int count)
  342. {
  343. CheckRange (index, count);
  344. return Array.LastIndexOf (data, item, index, count);
  345. }
  346. public bool Remove (T item)
  347. {
  348. int loc = IndexOf (item);
  349. if (loc != -1)
  350. RemoveAt (loc);
  351. return loc != -1;
  352. }
  353. [MonoTODO ("I can make it faster than this...")]
  354. public int RemoveAll (Predicate <T> match)
  355. {
  356. CheckMatch (match);
  357. int index = 0;
  358. int c = 0;
  359. while ((index = GetIndex (index, size - index, match)) != -1) {
  360. RemoveAt (index);
  361. c ++;
  362. }
  363. Array.Clear (data, size, c);
  364. return c;
  365. }
  366. public void RemoveAt (int index)
  367. {
  368. CheckIndex (index);
  369. Shift (index, -1);
  370. Array.Clear (data, size, 0);
  371. }
  372. public void RemoveRange (int index, int count)
  373. {
  374. CheckRange (index, count);
  375. Shift (index, -count);
  376. Array.Clear (data, size, count);
  377. }
  378. public void Reverse ()
  379. {
  380. Array.Reverse (data, 0, size);
  381. }
  382. public void Reverse (int index, int count)
  383. {
  384. CheckRange (index, count);
  385. Array.Reverse (data, index, count);
  386. }
  387. public void Sort ()
  388. {
  389. Array.Sort (data, 0, size, (IComparer) Comparer <T>.Default);
  390. }
  391. public void Sort (IComparer <T> comparer)
  392. {
  393. Array.Sort (data, 0, size, (IComparer) comparer);
  394. }
  395. // Waiting on Array
  396. [MonoTODO]
  397. public void Sort (Comparison <T> comparison)
  398. {
  399. throw new NotImplementedException ();
  400. }
  401. public void Sort (int index, int count, IComparer <T> comparer)
  402. {
  403. CheckRange (index, count);
  404. Array.Sort (data, index, count, (IComparer) comparer);
  405. }
  406. public T [] ToArray ()
  407. {
  408. T [] t = new T [size];
  409. Array.Copy (data, t, size);
  410. return t;
  411. }
  412. public void TrimExcess ()
  413. {
  414. Capacity = size;
  415. }
  416. public bool TrueForAll (Predicate <T> match)
  417. {
  418. CheckMatch (match);
  419. foreach (T t in this)
  420. if (!match (t))
  421. return false;
  422. return true;
  423. }
  424. public int Capacity {
  425. get {
  426. return data.Length;
  427. }
  428. set {
  429. if ((uint) value < (uint) size)
  430. throw new ArgumentOutOfRangeException ();
  431. Array.Resize (ref data, value);
  432. }
  433. }
  434. public int Count {
  435. get { return size; }
  436. }
  437. public T this [int index] {
  438. get {
  439. if ((uint) index >= (uint) size)
  440. throw new ArgumentOutOfRangeException ("index");
  441. return data [index];
  442. }
  443. set {
  444. CheckIndex (index);
  445. data [index] = value;
  446. }
  447. }
  448. #region Interface implementations.
  449. IEnumerator <T> IEnumerable <T>.GetEnumerator ()
  450. {
  451. return GetEnumerator ();
  452. }
  453. void ICollection.CopyTo (Array array, int arrayIndex)
  454. {
  455. Array.Copy (data, 0, array, arrayIndex, size);
  456. }
  457. IEnumerator IEnumerable.GetEnumerator ()
  458. {
  459. return GetEnumerator ();
  460. }
  461. int IList.Add (object item)
  462. {
  463. Add ((T) item);
  464. return size - 1;
  465. }
  466. bool IList.Contains (object item)
  467. {
  468. return Contains ((T) item);
  469. }
  470. int IList.IndexOf (object item)
  471. {
  472. return IndexOf ((T) item);
  473. }
  474. void IList.Insert (int index, object item)
  475. {
  476. Insert (index, (T) item);
  477. }
  478. void IList.Remove (object item)
  479. {
  480. Remove ((T) item);
  481. }
  482. bool ICollection <T>.IsReadOnly {
  483. get { return false; }
  484. }
  485. bool ICollection.IsSynchronized {
  486. get { return false; }
  487. }
  488. object ICollection.SyncRoot {
  489. get { return this; }
  490. }
  491. bool IList.IsFixedSize {
  492. get { return false; }
  493. }
  494. bool IList.IsReadOnly {
  495. get { return false; }
  496. }
  497. object IList.this [int index] {
  498. get { return this [index]; }
  499. set { this [index] = (T) value; }
  500. }
  501. #endregion
  502. public struct Enumerator : IEnumerator <T>, IDisposable {
  503. const int NOT_STARTED = -2;
  504. // this MUST be -1, because we depend on it in move next.
  505. // we just decr the size, so, 0 - 1 == FINISHED
  506. const int FINISHED = -1;
  507. List <T> l;
  508. int idx;
  509. int ver;
  510. internal Enumerator (List <T> l)
  511. {
  512. this.l = l;
  513. idx = NOT_STARTED;
  514. ver = l.version;
  515. }
  516. // for some fucked up reason, MSFT added a useless dispose to this class
  517. // It means that in foreach, we must still do a try/finally. Broken, very
  518. // broken.
  519. public void Dispose ()
  520. {
  521. idx = NOT_STARTED;
  522. }
  523. public bool MoveNext ()
  524. {
  525. if (ver != l.version)
  526. throw new InvalidOperationException ();
  527. if (idx == NOT_STARTED)
  528. idx = l.size;
  529. return idx != FINISHED && -- idx != FINISHED;
  530. }
  531. public T Current {
  532. get {
  533. if (idx < 0)
  534. throw new InvalidOperationException ();
  535. return l.data [l.size - 1 - idx];
  536. }
  537. }
  538. void IEnumerator.Reset ()
  539. {
  540. if (ver != l.version)
  541. throw new InvalidOperationException ();
  542. idx = NOT_STARTED;
  543. }
  544. object IEnumerator.Current {
  545. get { return Current; }
  546. }
  547. }
  548. }
  549. }
  550. #endif