List.cs 15 KB

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