ExposedList.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. //
  2. // System.Collections.Generic.List
  3. //
  4. // Authors:
  5. // Ben Maurer (bmaurer@ximian.com)
  6. // Martin Baulig (martin@ximian.com)
  7. // Carlos Alberto Cortez (calberto.cortez@gmail.com)
  8. // David Waite (mass@akuma.org)
  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. using System;
  33. using System.Collections;
  34. using System.Collections.Generic;
  35. using System.Diagnostics;
  36. namespace Spine {
  37. [DebuggerDisplay("Count={Count}")]
  38. public class ExposedList<T> : IEnumerable<T> {
  39. public T[] Items;
  40. public int Count;
  41. private const int DefaultCapacity = 4;
  42. private static readonly T[] EmptyArray = new T[0];
  43. private int version;
  44. public ExposedList () {
  45. Items = EmptyArray;
  46. }
  47. public ExposedList (IEnumerable<T> collection) {
  48. CheckCollection(collection);
  49. // initialize to needed size (if determinable)
  50. ICollection<T> c = collection as ICollection<T>;
  51. if (c == null) {
  52. Items = EmptyArray;
  53. AddEnumerable(collection);
  54. } else {
  55. Items = new T[c.Count];
  56. AddCollection(c);
  57. }
  58. }
  59. public ExposedList (int capacity) {
  60. if (capacity < 0)
  61. throw new ArgumentOutOfRangeException("capacity");
  62. Items = new T[capacity];
  63. }
  64. internal ExposedList (T[] data, int size) {
  65. Items = data;
  66. Count = size;
  67. }
  68. public void Add (T item) {
  69. // If we check to see if we need to grow before trying to grow
  70. // we can speed things up by 25%
  71. if (Count == Items.Length)
  72. GrowIfNeeded(1);
  73. Items[Count++] = item;
  74. version++;
  75. }
  76. public void GrowIfNeeded (int addedCount) {
  77. int minimumSize = Count + addedCount;
  78. if (minimumSize > Items.Length)
  79. Capacity = Math.Max(Math.Max(Capacity * 2, DefaultCapacity), minimumSize);
  80. }
  81. public ExposedList<T> Resize (int newSize) {
  82. int itemsLength = Items.Length;
  83. var oldItems = Items;
  84. if (newSize > itemsLength) {
  85. Array.Resize(ref Items, newSize);
  86. } else if (newSize < itemsLength) {
  87. // Allow nulling of T reference type to allow GC.
  88. for (int i = newSize; i < itemsLength; i++)
  89. oldItems[i] = default(T);
  90. }
  91. Count = newSize;
  92. return this;
  93. }
  94. public void EnsureCapacity (int min) {
  95. if (Items.Length < min) {
  96. int newCapacity = Items.Length == 0 ? DefaultCapacity : Items.Length * 2;
  97. //if ((uint)newCapacity > Array.MaxArrayLength) newCapacity = Array.MaxArrayLength;
  98. if (newCapacity < min) newCapacity = min;
  99. Capacity = newCapacity;
  100. }
  101. }
  102. private void CheckRange (int index, int count) {
  103. if (index < 0)
  104. throw new ArgumentOutOfRangeException("index");
  105. if (count < 0)
  106. throw new ArgumentOutOfRangeException("count");
  107. if ((uint)index + (uint)count > (uint)Count)
  108. throw new ArgumentException("index and count exceed length of list");
  109. }
  110. private void AddCollection (ICollection<T> collection) {
  111. int collectionCount = collection.Count;
  112. if (collectionCount == 0)
  113. return;
  114. GrowIfNeeded(collectionCount);
  115. collection.CopyTo(Items, Count);
  116. Count += collectionCount;
  117. }
  118. private void AddEnumerable (IEnumerable<T> enumerable) {
  119. foreach (T t in enumerable) {
  120. Add(t);
  121. }
  122. }
  123. // Additional overload provided because ExposedList<T> only implements IEnumerable<T>,
  124. // leading to sub-optimal behavior: It grows multiple times as it assumes not
  125. // to know the final size ahead of insertion.
  126. public void AddRange (ExposedList<T> list) {
  127. CheckCollection(list);
  128. int collectionCount = list.Count;
  129. if (collectionCount == 0)
  130. return;
  131. GrowIfNeeded(collectionCount);
  132. list.CopyTo(Items, Count);
  133. Count += collectionCount;
  134. version++;
  135. }
  136. public void AddRange (IEnumerable<T> collection) {
  137. CheckCollection(collection);
  138. ICollection<T> c = collection as ICollection<T>;
  139. if (c != null)
  140. AddCollection(c);
  141. else
  142. AddEnumerable(collection);
  143. version++;
  144. }
  145. public int BinarySearch (T item) {
  146. return Array.BinarySearch<T>(Items, 0, Count, item);
  147. }
  148. public int BinarySearch (T item, IComparer<T> comparer) {
  149. return Array.BinarySearch<T>(Items, 0, Count, item, comparer);
  150. }
  151. public int BinarySearch (int index, int count, T item, IComparer<T> comparer) {
  152. CheckRange(index, count);
  153. return Array.BinarySearch<T>(Items, index, count, item, comparer);
  154. }
  155. public void Clear (bool clearArray = true) {
  156. if (clearArray)
  157. Array.Clear(Items, 0, Items.Length);
  158. Count = 0;
  159. version++;
  160. }
  161. public bool Contains (T item) {
  162. return Array.IndexOf<T>(Items, item, 0, Count) != -1;
  163. }
  164. public ExposedList<TOutput> ConvertAll<TOutput> (Converter<T, TOutput> converter) {
  165. if (converter == null)
  166. throw new ArgumentNullException("converter");
  167. ExposedList<TOutput> u = new ExposedList<TOutput>(Count);
  168. u.Count = Count;
  169. T[] items = Items;
  170. TOutput[] uItems = u.Items;
  171. for (int i = 0; i < Count; i++)
  172. uItems[i] = converter(items[i]);
  173. return u;
  174. }
  175. public void CopyTo (T[] array) {
  176. Array.Copy(Items, 0, array, 0, Count);
  177. }
  178. public void CopyTo (T[] array, int arrayIndex) {
  179. Array.Copy(Items, 0, array, arrayIndex, Count);
  180. }
  181. public void CopyTo (int index, T[] array, int arrayIndex, int count) {
  182. CheckRange(index, count);
  183. Array.Copy(Items, index, array, arrayIndex, count);
  184. }
  185. public bool Exists (Predicate<T> match) {
  186. CheckMatch(match);
  187. return GetIndex(0, Count, match) != -1;
  188. }
  189. public T Find (Predicate<T> match) {
  190. CheckMatch(match);
  191. int i = GetIndex(0, Count, match);
  192. return (i != -1) ? Items[i] : default(T);
  193. }
  194. private static void CheckMatch (Predicate<T> match) {
  195. if (match == null)
  196. throw new ArgumentNullException("match");
  197. }
  198. public ExposedList<T> FindAll (Predicate<T> match) {
  199. CheckMatch(match);
  200. return FindAllList(match);
  201. }
  202. private ExposedList<T> FindAllList (Predicate<T> match) {
  203. ExposedList<T> results = new ExposedList<T>();
  204. for (int i = 0; i < Count; i++)
  205. if (match(Items[i]))
  206. results.Add(Items[i]);
  207. return results;
  208. }
  209. public int FindIndex (Predicate<T> match) {
  210. CheckMatch(match);
  211. return GetIndex(0, Count, match);
  212. }
  213. public int FindIndex (int startIndex, Predicate<T> match) {
  214. CheckMatch(match);
  215. CheckIndex(startIndex);
  216. return GetIndex(startIndex, Count - startIndex, match);
  217. }
  218. public int FindIndex (int startIndex, int count, Predicate<T> match) {
  219. CheckMatch(match);
  220. CheckRange(startIndex, count);
  221. return GetIndex(startIndex, count, match);
  222. }
  223. private int GetIndex (int startIndex, int count, Predicate<T> match) {
  224. int end = startIndex + count;
  225. for (int i = startIndex; i < end; i++)
  226. if (match(Items[i]))
  227. return i;
  228. return -1;
  229. }
  230. public T FindLast (Predicate<T> match) {
  231. CheckMatch(match);
  232. int i = GetLastIndex(0, Count, match);
  233. return i == -1 ? default(T) : Items[i];
  234. }
  235. public int FindLastIndex (Predicate<T> match) {
  236. CheckMatch(match);
  237. return GetLastIndex(0, Count, match);
  238. }
  239. public int FindLastIndex (int startIndex, Predicate<T> match) {
  240. CheckMatch(match);
  241. CheckIndex(startIndex);
  242. return GetLastIndex(0, startIndex + 1, match);
  243. }
  244. public int FindLastIndex (int startIndex, int count, Predicate<T> match) {
  245. CheckMatch(match);
  246. int start = startIndex - count + 1;
  247. CheckRange(start, count);
  248. return GetLastIndex(start, count, match);
  249. }
  250. private int GetLastIndex (int startIndex, int count, Predicate<T> match) {
  251. // unlike FindLastIndex, takes regular params for search range
  252. for (int i = startIndex + count; i != startIndex;)
  253. if (match(Items[--i]))
  254. return i;
  255. return -1;
  256. }
  257. public void ForEach (Action<T> action) {
  258. if (action == null)
  259. throw new ArgumentNullException("action");
  260. for (int i = 0; i < Count; i++)
  261. action(Items[i]);
  262. }
  263. public Enumerator GetEnumerator () {
  264. return new Enumerator(this);
  265. }
  266. public ExposedList<T> GetRange (int index, int count) {
  267. CheckRange(index, count);
  268. T[] tmpArray = new T[count];
  269. Array.Copy(Items, index, tmpArray, 0, count);
  270. return new ExposedList<T>(tmpArray, count);
  271. }
  272. public int IndexOf (T item) {
  273. return Array.IndexOf<T>(Items, item, 0, Count);
  274. }
  275. public int IndexOf (T item, int index) {
  276. CheckIndex(index);
  277. return Array.IndexOf<T>(Items, item, index, Count - index);
  278. }
  279. public int IndexOf (T item, int index, int count) {
  280. if (index < 0)
  281. throw new ArgumentOutOfRangeException("index");
  282. if (count < 0)
  283. throw new ArgumentOutOfRangeException("count");
  284. if ((uint)index + (uint)count > (uint)Count)
  285. throw new ArgumentOutOfRangeException("index and count exceed length of list");
  286. return Array.IndexOf<T>(Items, item, index, count);
  287. }
  288. private void Shift (int start, int delta) {
  289. if (delta < 0)
  290. start -= delta;
  291. if (start < Count)
  292. Array.Copy(Items, start, Items, start + delta, Count - start);
  293. Count += delta;
  294. if (delta < 0)
  295. Array.Clear(Items, Count, -delta);
  296. }
  297. private void CheckIndex (int index) {
  298. if (index < 0 || (uint)index > (uint)Count)
  299. throw new ArgumentOutOfRangeException("index");
  300. }
  301. public void Insert (int index, T item) {
  302. CheckIndex(index);
  303. if (Count == Items.Length)
  304. GrowIfNeeded(1);
  305. Shift(index, 1);
  306. Items[index] = item;
  307. version++;
  308. }
  309. private void CheckCollection (IEnumerable<T> collection) {
  310. if (collection == null)
  311. throw new ArgumentNullException("collection");
  312. }
  313. public void InsertRange (int index, IEnumerable<T> collection) {
  314. CheckCollection(collection);
  315. CheckIndex(index);
  316. if (collection == this) {
  317. T[] buffer = new T[Count];
  318. CopyTo(buffer, 0);
  319. GrowIfNeeded(Count);
  320. Shift(index, buffer.Length);
  321. Array.Copy(buffer, 0, Items, index, buffer.Length);
  322. } else {
  323. ICollection<T> c = collection as ICollection<T>;
  324. if (c != null)
  325. InsertCollection(index, c);
  326. else
  327. InsertEnumeration(index, collection);
  328. }
  329. version++;
  330. }
  331. private void InsertCollection (int index, ICollection<T> collection) {
  332. int collectionCount = collection.Count;
  333. GrowIfNeeded(collectionCount);
  334. Shift(index, collectionCount);
  335. collection.CopyTo(Items, index);
  336. }
  337. private void InsertEnumeration (int index, IEnumerable<T> enumerable) {
  338. foreach (T t in enumerable)
  339. Insert(index++, t);
  340. }
  341. public int LastIndexOf (T item) {
  342. return Array.LastIndexOf<T>(Items, item, Count - 1, Count);
  343. }
  344. public int LastIndexOf (T item, int index) {
  345. CheckIndex(index);
  346. return Array.LastIndexOf<T>(Items, item, index, index + 1);
  347. }
  348. public int LastIndexOf (T item, int index, int count) {
  349. if (index < 0)
  350. throw new ArgumentOutOfRangeException("index", index, "index is negative");
  351. if (count < 0)
  352. throw new ArgumentOutOfRangeException("count", count, "count is negative");
  353. if (index - count + 1 < 0)
  354. throw new ArgumentOutOfRangeException("count", count, "count is too large");
  355. return Array.LastIndexOf<T>(Items, item, index, count);
  356. }
  357. public bool Remove (T item) {
  358. int loc = IndexOf(item);
  359. if (loc != -1)
  360. RemoveAt(loc);
  361. return loc != -1;
  362. }
  363. public int RemoveAll (Predicate<T> match) {
  364. CheckMatch(match);
  365. int i = 0;
  366. int j = 0;
  367. // Find the first item to remove
  368. for (i = 0; i < Count; i++)
  369. if (match(Items[i]))
  370. break;
  371. if (i == Count)
  372. return 0;
  373. version++;
  374. // Remove any additional items
  375. for (j = i + 1; j < Count; j++) {
  376. if (!match(Items[j]))
  377. Items[i++] = Items[j];
  378. }
  379. if (j - i > 0)
  380. Array.Clear(Items, i, j - i);
  381. Count = i;
  382. return (j - i);
  383. }
  384. public void RemoveAt (int index) {
  385. if (index < 0 || (uint)index >= (uint)Count)
  386. throw new ArgumentOutOfRangeException("index");
  387. Shift(index, -1);
  388. Array.Clear(Items, Count, 1);
  389. version++;
  390. }
  391. // Spine Added Method
  392. // Based on Stack<T>.Pop(); https://referencesource.microsoft.com/#mscorlib/system/collections/stack.cs
  393. /// <summary>Pops the last item of the list. If the list is empty, Pop throws an InvalidOperationException.</summary>
  394. public T Pop () {
  395. if (Count == 0)
  396. throw new InvalidOperationException("List is empty. Nothing to pop.");
  397. int i = Count - 1;
  398. T item = Items[i];
  399. Items[i] = default(T);
  400. Count--;
  401. version++;
  402. return item;
  403. }
  404. public void RemoveRange (int index, int count) {
  405. CheckRange(index, count);
  406. if (count > 0) {
  407. Shift(index, -count);
  408. Array.Clear(Items, Count, count);
  409. version++;
  410. }
  411. }
  412. public void Reverse () {
  413. Array.Reverse(Items, 0, Count);
  414. version++;
  415. }
  416. public void Reverse (int index, int count) {
  417. CheckRange(index, count);
  418. Array.Reverse(Items, index, count);
  419. version++;
  420. }
  421. public void Sort () {
  422. Array.Sort<T>(Items, 0, Count, Comparer<T>.Default);
  423. version++;
  424. }
  425. public void Sort (IComparer<T> comparer) {
  426. Array.Sort<T>(Items, 0, Count, comparer);
  427. version++;
  428. }
  429. public void Sort (Comparison<T> comparison) {
  430. Array.Sort<T>(Items, comparison);
  431. version++;
  432. }
  433. public void Sort (int index, int count, IComparer<T> comparer) {
  434. CheckRange(index, count);
  435. Array.Sort<T>(Items, index, count, comparer);
  436. version++;
  437. }
  438. public T[] ToArray () {
  439. T[] t = new T[Count];
  440. Array.Copy(Items, t, Count);
  441. return t;
  442. }
  443. public void TrimExcess () {
  444. Capacity = Count;
  445. }
  446. public bool TrueForAll (Predicate<T> match) {
  447. CheckMatch(match);
  448. for (int i = 0; i < Count; i++)
  449. if (!match(Items[i]))
  450. return false;
  451. return true;
  452. }
  453. public int Capacity {
  454. get {
  455. return Items.Length;
  456. }
  457. set {
  458. if ((uint)value < (uint)Count)
  459. throw new ArgumentOutOfRangeException();
  460. Array.Resize(ref Items, value);
  461. }
  462. }
  463. #region Interface implementations.
  464. IEnumerator<T> IEnumerable<T>.GetEnumerator () {
  465. return GetEnumerator();
  466. }
  467. IEnumerator IEnumerable.GetEnumerator () {
  468. return GetEnumerator();
  469. }
  470. #endregion
  471. public struct Enumerator : IEnumerator<T>, IDisposable {
  472. private ExposedList<T> l;
  473. private int next;
  474. private int ver;
  475. private T current;
  476. internal Enumerator (ExposedList<T> l)
  477. : this() {
  478. this.l = l;
  479. ver = l.version;
  480. }
  481. public void Dispose () {
  482. l = null;
  483. }
  484. private void VerifyState () {
  485. if (l == null)
  486. throw new ObjectDisposedException(GetType().FullName);
  487. if (ver != l.version)
  488. throw new InvalidOperationException(
  489. "Collection was modified; enumeration operation may not execute.");
  490. }
  491. public bool MoveNext () {
  492. VerifyState();
  493. if (next < 0)
  494. return false;
  495. if (next < l.Count) {
  496. current = l.Items[next++];
  497. return true;
  498. }
  499. next = -1;
  500. return false;
  501. }
  502. public T Current {
  503. get {
  504. return current;
  505. }
  506. }
  507. void IEnumerator.Reset () {
  508. VerifyState();
  509. next = 0;
  510. }
  511. object IEnumerator.Current {
  512. get {
  513. VerifyState();
  514. if (next <= 0)
  515. throw new InvalidOperationException();
  516. return current;
  517. }
  518. }
  519. }
  520. }
  521. }