Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FasterList.cs 18KB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. using UnityEngine;
  6. namespace Svelto.DataStructures
  7. {
  8. public struct FasterListEnumerator<T> : IEnumerator<T>
  9. {
  10. public T Current
  11. {
  12. get { return _current; }
  13. }
  14. public FasterListEnumerator(T[] buffer, int size)
  15. {
  16. _size = size;
  17. _counter = 0;
  18. _buffer = buffer;
  19. _current = default(T);
  20. }
  21. object IEnumerator.Current
  22. {
  23. get { return _current; }
  24. }
  25. T IEnumerator<T>.Current
  26. {
  27. get { return _current; }
  28. }
  29. public void Dispose()
  30. {
  31. _buffer = null;
  32. }
  33. public bool MoveNext()
  34. {
  35. if (_counter < _size)
  36. {
  37. _current = _buffer[_counter++];
  38. return true;
  39. }
  40. _current = default(T);
  41. return false;
  42. }
  43. public void Reset()
  44. {
  45. _counter = 0;
  46. }
  47. bool IEnumerator.MoveNext()
  48. {
  49. return MoveNext();
  50. }
  51. void IEnumerator.Reset()
  52. {
  53. Reset();
  54. }
  55. T[] _buffer;
  56. int _counter;
  57. int _size;
  58. T _current;
  59. }
  60. public struct FasterListEnumeratorCast<T, U> : IEnumerator<T> where T:U
  61. {
  62. public T Current
  63. {
  64. get { return (T)_buffer.Current; }
  65. }
  66. public FasterListEnumeratorCast(FasterListEnumerator<U> buffer)
  67. {
  68. _buffer = buffer;
  69. }
  70. object IEnumerator.Current
  71. {
  72. get { return (T)_buffer.Current; }
  73. }
  74. T IEnumerator<T>.Current
  75. {
  76. get { return (T)_buffer.Current; }
  77. }
  78. public void Dispose()
  79. {}
  80. public bool MoveNext()
  81. {
  82. return _buffer.MoveNext();
  83. }
  84. public void Reset()
  85. {
  86. _buffer.Reset();
  87. }
  88. bool IEnumerator.MoveNext()
  89. {
  90. return MoveNext();
  91. }
  92. void IEnumerator.Reset()
  93. {
  94. Reset();
  95. }
  96. FasterListEnumerator<U> _buffer;
  97. }
  98. public struct FasterReadOnlyList<T> : IList<T>
  99. {
  100. public int Count { get { return _list.Count; } }
  101. public bool IsReadOnly { get { return true; } }
  102. public FasterReadOnlyList(FasterList<T> list)
  103. {
  104. _list = list;
  105. }
  106. public T this[int index] { get { return _list[index]; } set { throw new NotImplementedException(); } }
  107. public FasterListEnumerator<T> GetEnumerator()
  108. {
  109. return _list.GetEnumerator();
  110. }
  111. public void Add(T item)
  112. {
  113. throw new NotImplementedException();
  114. }
  115. public void Clear()
  116. {
  117. throw new NotImplementedException();
  118. }
  119. public bool Contains(T item)
  120. {
  121. return _list.Contains(item);
  122. }
  123. public void CopyTo(T[] array, int arrayIndex)
  124. {
  125. _list.CopyTo(array, arrayIndex);
  126. }
  127. public bool Remove(T item)
  128. {
  129. throw new NotImplementedException();
  130. }
  131. public int IndexOf(T item)
  132. {
  133. return _list.IndexOf(item);
  134. }
  135. public void Insert(int index, T item)
  136. {
  137. throw new NotImplementedException();
  138. }
  139. public void RemoveAt(int index)
  140. {
  141. throw new NotImplementedException();
  142. }
  143. IEnumerator<T> IEnumerable<T>.GetEnumerator()
  144. {
  145. return GetEnumerator();
  146. }
  147. IEnumerator IEnumerable.GetEnumerator()
  148. {
  149. return GetEnumerator();
  150. }
  151. readonly FasterList<T> _list;
  152. }
  153. public struct FasterListThreadSafe<T> : IList<T>
  154. {
  155. public FasterListThreadSafe(FasterList<T> list)
  156. {
  157. if (list == null) throw new ArgumentException("invalid list");
  158. _list = list;
  159. _lockQ = new ReaderWriterLockSlim();
  160. }
  161. public int Count
  162. {
  163. get
  164. {
  165. _lockQ.EnterReadLock();
  166. try
  167. {
  168. return _list.Count;
  169. }
  170. finally
  171. {
  172. _lockQ.ExitReadLock();
  173. }
  174. }
  175. }
  176. public bool IsReadOnly { get { return false; } }
  177. public T this[int index]
  178. {
  179. get
  180. {
  181. _lockQ.EnterReadLock();
  182. try
  183. {
  184. return _list[index];
  185. }
  186. finally
  187. {
  188. _lockQ.ExitReadLock();
  189. }
  190. }
  191. set
  192. {
  193. _lockQ.EnterWriteLock();
  194. try
  195. {
  196. _list[index] = value;
  197. }
  198. finally
  199. {
  200. _lockQ.ExitWriteLock();
  201. }
  202. }
  203. }
  204. public FasterListEnumerator<T> GetEnumerator()
  205. {
  206. throw new NotImplementedException();
  207. }
  208. public void Add(T item)
  209. {
  210. _lockQ.EnterWriteLock();
  211. try
  212. {
  213. _list.Add(item);
  214. }
  215. finally
  216. {
  217. _lockQ.ExitWriteLock();
  218. }
  219. }
  220. public void Clear()
  221. {
  222. _lockQ.EnterWriteLock();
  223. try
  224. {
  225. _list.Clear();
  226. }
  227. finally
  228. {
  229. _lockQ.ExitWriteLock();
  230. }
  231. }
  232. public bool Contains(T item)
  233. {
  234. _lockQ.EnterReadLock();
  235. try
  236. {
  237. return _list.Contains(item);
  238. }
  239. finally
  240. {
  241. _lockQ.ExitReadLock();
  242. }
  243. }
  244. public void CopyTo(T[] array, int arrayIndex)
  245. {
  246. _lockQ.EnterReadLock();
  247. try
  248. {
  249. _list.CopyTo(array, arrayIndex);
  250. }
  251. finally
  252. {
  253. _lockQ.ExitReadLock();
  254. }
  255. }
  256. public bool Remove(T item)
  257. {
  258. _lockQ.EnterWriteLock();
  259. try
  260. {
  261. return _list.Remove(item);
  262. }
  263. finally
  264. {
  265. _lockQ.ExitWriteLock();
  266. }
  267. }
  268. public int IndexOf(T item)
  269. {
  270. _lockQ.EnterReadLock();
  271. try
  272. {
  273. return _list.IndexOf(item);
  274. }
  275. finally
  276. {
  277. _lockQ.ExitReadLock();
  278. }
  279. }
  280. public void Insert(int index, T item)
  281. {
  282. _lockQ.EnterWriteLock();
  283. try
  284. {
  285. _list.Insert(index, item);
  286. }
  287. finally
  288. {
  289. _lockQ.ExitWriteLock();
  290. }
  291. }
  292. public void RemoveAt(int index)
  293. {
  294. _lockQ.EnterWriteLock();
  295. try
  296. {
  297. _list.RemoveAt(index);
  298. }
  299. finally
  300. {
  301. _lockQ.ExitWriteLock();
  302. }
  303. }
  304. public void UnorderedRemoveAt(int index)
  305. {
  306. _lockQ.EnterWriteLock();
  307. try
  308. {
  309. _list.UnorderedRemoveAt(index);
  310. }
  311. finally
  312. {
  313. _lockQ.ExitWriteLock();
  314. }
  315. }
  316. IEnumerator<T> IEnumerable<T>.GetEnumerator()
  317. {
  318. throw new NotImplementedException();
  319. }
  320. IEnumerator IEnumerable.GetEnumerator()
  321. {
  322. throw new NotImplementedException();
  323. }
  324. readonly FasterList<T> _list;
  325. readonly ReaderWriterLockSlim _lockQ;
  326. }
  327. public struct FasterReadOnlyListCast<T, U> : IList<U> where U:T
  328. {
  329. public int Count { get { return _list.Count; } }
  330. public bool IsReadOnly { get { return true; } }
  331. public FasterReadOnlyListCast(FasterList<T> list)
  332. {
  333. _list = list;
  334. }
  335. public U this[int index] { get { return (U)_list[index]; } set { throw new NotImplementedException(); } }
  336. public FasterListEnumeratorCast<U, T> GetEnumerator()
  337. {
  338. return new FasterListEnumeratorCast<U, T>(_list.GetEnumerator());
  339. }
  340. public void Add(U item)
  341. {
  342. throw new NotImplementedException();
  343. }
  344. public void Clear()
  345. {
  346. throw new NotImplementedException();
  347. }
  348. public bool Contains(U item)
  349. {
  350. return _list.Contains(item);
  351. }
  352. public void CopyTo(U[] array, int arrayIndex)
  353. {
  354. Array.Copy(_list.ToArrayFast(), 0, array, arrayIndex, _list.Count);
  355. }
  356. public bool Remove(U item)
  357. {
  358. throw new NotImplementedException();
  359. }
  360. public int IndexOf(U item)
  361. {
  362. return _list.IndexOf(item);
  363. }
  364. public void Insert(int index, U item)
  365. {
  366. throw new NotImplementedException();
  367. }
  368. public void RemoveAt(int index)
  369. {
  370. throw new NotImplementedException();
  371. }
  372. IEnumerator<U> IEnumerable<U>.GetEnumerator()
  373. {
  374. return GetEnumerator();
  375. }
  376. IEnumerator IEnumerable.GetEnumerator()
  377. {
  378. return GetEnumerator();
  379. }
  380. readonly FasterList<T> _list;
  381. }
  382. public interface IFasterList
  383. {}
  384. public class FasterList<T> : IList<T>, IFasterList
  385. {
  386. public static FasterList<T> DefaultList = new FasterList<T>();
  387. const int MIN_SIZE = 4;
  388. public int Count
  389. {
  390. get { return _count; }
  391. }
  392. public bool IsReadOnly
  393. {
  394. get { return false; }
  395. }
  396. public FasterList()
  397. {
  398. _count = 0;
  399. _buffer = new T[MIN_SIZE];
  400. }
  401. public FasterList(int initialSize)
  402. {
  403. _count = 0;
  404. _buffer = new T[initialSize];
  405. }
  406. public FasterList(ICollection<T> collection)
  407. {
  408. _buffer = new T[collection.Count];
  409. collection.CopyTo(_buffer, 0);
  410. _count = _buffer.Length;
  411. }
  412. public FasterList(FasterList<T> listCopy)
  413. {
  414. _buffer = new T[listCopy.Count];
  415. listCopy.CopyTo(_buffer, 0);
  416. _count = listCopy.Count;
  417. }
  418. public T this[int i]
  419. {
  420. get { DesignByContract.Check.Require(i < _count, "out of bound index"); return _buffer[i]; }
  421. set { DesignByContract.Check.Require(i < _count, "out of bound index"); _buffer[i] = value; }
  422. }
  423. public void Add(T item)
  424. {
  425. if (_count == _buffer.Length)
  426. AllocateMore();
  427. _buffer[_count++] = item;
  428. }
  429. /// <summary>
  430. /// this is a dirtish trick to be able to use the index operastor
  431. /// before adding the elements through the Add functions
  432. /// </summary>
  433. /// <typeparam name="U"></typeparam>
  434. /// <param name="initialSize"></param>
  435. /// <returns></returns>
  436. public static FasterList<T> PreFill<U>(int initialSize) where U:T, new()
  437. {
  438. var list = new FasterList<T>(initialSize);
  439. for (int i = 0; i < initialSize; i++)
  440. list.Add(new U());
  441. list.Clear();
  442. return list;
  443. }
  444. public void AddRange(IEnumerable<T> items, int count)
  445. {
  446. AddRange(items.GetEnumerator(), count);
  447. }
  448. public void AddRange(IEnumerator<T> items, int count)
  449. {
  450. if (_count + count >= _buffer.Length)
  451. AllocateMore(_count + count);
  452. while (items.MoveNext())
  453. _buffer[_count++] = items.Current;
  454. }
  455. public void AddRange(ICollection<T> items)
  456. {
  457. AddRange(items.GetEnumerator(), items.Count);
  458. }
  459. public void AddRange(FasterList<T> items)
  460. {
  461. AddRange(items.ToArrayFast(), items.Count);
  462. }
  463. public void AddRange(T[] items, int count)
  464. {
  465. if (count == 0) return;
  466. if (_count + count >= _buffer.Length)
  467. AllocateMore(_count + count);
  468. Array.Copy(items, 0, _buffer, _count, count);
  469. _count += count;
  470. }
  471. public void AddRange(T[] items)
  472. {
  473. AddRange(items, items.Length);
  474. }
  475. public FasterReadOnlyList<T> AsReadOnly()
  476. {
  477. return new FasterReadOnlyList<T>(this);
  478. }
  479. /// <summary>
  480. /// Careful, you could keep on holding references you don't want to hold to anymore
  481. /// Use DeepClear in case.
  482. /// </summary>
  483. public void Clear()
  484. {
  485. _count = 0;
  486. }
  487. public void DeepClear()
  488. {
  489. Array.Clear(_buffer, 0, _buffer.Length);
  490. _count = 0;
  491. }
  492. public bool Contains(T item)
  493. {
  494. var index = IndexOf(item);
  495. return index != -1;
  496. }
  497. public void CopyTo(T[] array, int arrayIndex)
  498. {
  499. Array.Copy(_buffer, 0, array, arrayIndex, Count);
  500. }
  501. public FasterListEnumerator<T> GetEnumerator()
  502. {
  503. return new FasterListEnumerator<T>(_buffer, Count);
  504. }
  505. public int IndexOf(T item)
  506. {
  507. var comp = EqualityComparer<T>.Default;
  508. for (var index = _count - 1; index >= 0; --index)
  509. if (comp.Equals(_buffer[index], item))
  510. return index;
  511. return -1;
  512. }
  513. public void Insert(int index, T item)
  514. {
  515. DesignByContract.Check.Require(index < _count, "out of bound index");
  516. if (_count == _buffer.Length) AllocateMore();
  517. Array.Copy(_buffer, index, _buffer, index + 1, _count - index);
  518. _buffer[index] = item;
  519. ++_count;
  520. }
  521. public void Release()
  522. {
  523. _count = 0;
  524. _buffer = null;
  525. }
  526. public bool Remove(T item)
  527. {
  528. var index = IndexOf(item);
  529. if (index == -1)
  530. return false;
  531. RemoveAt(index);
  532. return true;
  533. }
  534. public void RemoveAt(int index)
  535. {
  536. DesignByContract.Check.Require(index < _count, "out of bound index");
  537. if (index == --_count)
  538. return;
  539. Array.Copy(_buffer, index + 1, _buffer, index, _count - index);
  540. _buffer[_count] = default(T);
  541. }
  542. public void Resize(int newSize)
  543. {
  544. if (newSize < MIN_SIZE)
  545. newSize = MIN_SIZE;
  546. Array.Resize(ref _buffer, newSize);
  547. _count = newSize;
  548. }
  549. public void SetAt(int index, T value)
  550. {
  551. if (index >= _buffer.Length)
  552. AllocateMore(index + 1);
  553. if (_count <= index)
  554. _count = index + 1;
  555. this[index] = value;
  556. }
  557. public void Sort(IComparer<T> comparer)
  558. {
  559. Array.Sort(_buffer, 0, _count, comparer);
  560. }
  561. public T[] ToArray()
  562. {
  563. T[] destinationArray = new T[_count];
  564. Array.Copy(_buffer, 0, destinationArray, 0, _count);
  565. return destinationArray;
  566. }
  567. /// <summary>
  568. /// This function exists to allow fast iterations. The size of the array returned cannot be
  569. /// used. The list count must be used instead.
  570. /// </summary>
  571. /// <returns></returns>
  572. public T[] ToArrayFast()
  573. {
  574. return _buffer;
  575. }
  576. public bool UnorderedRemove(T item)
  577. {
  578. var index = IndexOf(item);
  579. if (index == -1)
  580. return false;
  581. UnorderedRemoveAt(index);
  582. return true;
  583. }
  584. public bool UnorderedRemoveAt(int index)
  585. {
  586. DesignByContract.Check.Require(index < _count && _count > 0, "out of bound index");
  587. if (index == --_count)
  588. return false;
  589. T swap = _buffer[index];
  590. _buffer[index] = _buffer[_count];
  591. _buffer[_count] = swap;
  592. return true;
  593. }
  594. IEnumerator IEnumerable.GetEnumerator()
  595. {
  596. return GetEnumerator();
  597. }
  598. IEnumerator<T> IEnumerable<T>.GetEnumerator()
  599. {
  600. return GetEnumerator();
  601. }
  602. void AllocateMore()
  603. {
  604. var newList = new T[Mathf.Max(_buffer.Length << 1, MIN_SIZE)];
  605. if (_count > 0) _buffer.CopyTo(newList, 0);
  606. _buffer = newList;
  607. }
  608. void AllocateMore(int newSize)
  609. {
  610. var oldLength = Mathf.Max(_buffer.Length, MIN_SIZE);
  611. while (oldLength < newSize)
  612. oldLength <<= 1;
  613. var newList = new T[oldLength];
  614. if (_count > 0) Array.Copy(_buffer, newList, _count);
  615. _buffer = newList;
  616. }
  617. public void Trim()
  618. {
  619. if (_count < _buffer.Length)
  620. Resize(_count);
  621. }
  622. public bool Reuse<U>(int index, out U result)
  623. where U:class, T
  624. {
  625. result = default(U);
  626. if (index >= _buffer.Length)
  627. return false;
  628. result = (U)_buffer[index];
  629. return result != null;
  630. }
  631. T[] _buffer;
  632. int _count;
  633. }
  634. }