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 17KB

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
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  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 UnorderredRemoveAt(int index)
  305. {
  306. _lockQ.EnterWriteLock();
  307. try
  308. {
  309. _list.UnorderredRemoveAt(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 class FasterList<T> : IList<T>
  383. {
  384. public static FasterList<T> DefaultList = new FasterList<T>();
  385. const int MIN_SIZE = 4;
  386. public int Count
  387. {
  388. get { return _count; }
  389. }
  390. public bool IsReadOnly
  391. {
  392. get { return false; }
  393. }
  394. public FasterList()
  395. {
  396. _count = 0;
  397. _buffer = new T[MIN_SIZE];
  398. }
  399. public FasterList(int initialSize)
  400. {
  401. _count = 0;
  402. _buffer = new T[initialSize];
  403. }
  404. public FasterList(ICollection<T> collection)
  405. {
  406. _buffer = new T[collection.Count];
  407. collection.CopyTo(_buffer, 0);
  408. _count = _buffer.Length;
  409. }
  410. public FasterList(FasterList<T> listCopy)
  411. {
  412. _buffer = new T[listCopy.Count];
  413. listCopy.CopyTo(_buffer, 0);
  414. _count = listCopy.Count;
  415. }
  416. public T this[int i]
  417. {
  418. get { DesignByContract.Check.Require(i < _count, "out of bound index"); return _buffer[i]; }
  419. set { DesignByContract.Check.Require(i < _count, "out of bound index"); _buffer[i] = value; }
  420. }
  421. public void Add(T item)
  422. {
  423. if (_count == _buffer.Length)
  424. AllocateMore();
  425. _buffer[_count++] = item;
  426. }
  427. /// <summary>
  428. /// this is a dirtish trick to be able to use the index operastor
  429. /// before adding the elements through the Add functions
  430. /// </summary>
  431. /// <typeparam name="U"></typeparam>
  432. /// <param name="initialSize"></param>
  433. /// <returns></returns>
  434. public static FasterList<T> PreFill<U>(int initialSize) where U:T, new()
  435. {
  436. var list = new FasterList<T>(initialSize);
  437. for (int i = 0; i < initialSize; i++)
  438. list.Add(new U());
  439. list.Clear();
  440. return list;
  441. }
  442. public void AddRange(IEnumerable<T> items, int count)
  443. {
  444. AddRange(items.GetEnumerator(), count);
  445. }
  446. public void AddRange(IEnumerator<T> items, int count)
  447. {
  448. if (_count + count >= _buffer.Length)
  449. AllocateMore(_count + count);
  450. while (items.MoveNext())
  451. _buffer[_count++] = items.Current;
  452. }
  453. public void AddRange(ICollection<T> items)
  454. {
  455. AddRange(items.GetEnumerator(), items.Count);
  456. }
  457. public void AddRange(FasterList<T> items)
  458. {
  459. AddRange(items.ToArrayFast(), items.Count);
  460. }
  461. public void AddRange(T[] items, int count)
  462. {
  463. if (count == 0) return;
  464. if (_count + count >= _buffer.Length)
  465. AllocateMore(_count + count);
  466. Array.Copy(items, 0, _buffer, _count, count);
  467. _count += count;
  468. }
  469. public FasterReadOnlyList<T> AsReadOnly()
  470. {
  471. return new FasterReadOnlyList<T>(this);
  472. }
  473. /// <summary>
  474. /// Careful, you could keep on holding references you don't want to hold to anymore
  475. /// Use DeepClear in case.
  476. /// </summary>
  477. public void Clear()
  478. {
  479. _count = 0;
  480. }
  481. public void DeepClear()
  482. {
  483. Array.Clear(_buffer, 0, _buffer.Length);
  484. _count = 0;
  485. }
  486. public bool Contains(T item)
  487. {
  488. var index = IndexOf(item);
  489. return index != -1;
  490. }
  491. public void CopyTo(T[] array, int arrayIndex)
  492. {
  493. Array.Copy(_buffer, 0, array, arrayIndex, Count);
  494. }
  495. public FasterListEnumerator<T> GetEnumerator()
  496. {
  497. return new FasterListEnumerator<T>(_buffer, Count);
  498. }
  499. public int IndexOf(T item)
  500. {
  501. var comp = EqualityComparer<T>.Default;
  502. for (var index = _count - 1; index >= 0; --index)
  503. if (comp.Equals(_buffer[index], item))
  504. return index;
  505. return -1;
  506. }
  507. public void Insert(int index, T item)
  508. {
  509. DesignByContract.Check.Require(index < _count, "out of bound index");
  510. if (_count == _buffer.Length) AllocateMore();
  511. Array.Copy(_buffer, index, _buffer, index + 1, _count - index);
  512. _buffer[index] = item;
  513. ++_count;
  514. }
  515. public void Release()
  516. {
  517. _count = 0;
  518. _buffer = null;
  519. }
  520. public bool Remove(T item)
  521. {
  522. var index = IndexOf(item);
  523. if (index == -1)
  524. return false;
  525. RemoveAt(index);
  526. return true;
  527. }
  528. public void RemoveAt(int index)
  529. {
  530. DesignByContract.Check.Require(index < _count, "out of bound index");
  531. if (index == --_count)
  532. return;
  533. Array.Copy(_buffer, index + 1, _buffer, index, _count - index);
  534. _buffer[_count] = default(T);
  535. }
  536. public void Resize(int newSize)
  537. {
  538. if (newSize < MIN_SIZE)
  539. newSize = MIN_SIZE;
  540. Array.Resize(ref _buffer, newSize);
  541. _count = newSize;
  542. }
  543. public void SetAt(int index, T value)
  544. {
  545. if (index >= _buffer.Length)
  546. AllocateMore(index + 1);
  547. if (_count <= index)
  548. _count = index + 1;
  549. this[index] = value;
  550. }
  551. public void Sort(IComparer<T> comparer)
  552. {
  553. Array.Sort(_buffer, 0, _count, comparer);
  554. }
  555. public T[] ToArray()
  556. {
  557. T[] destinationArray = new T[_count];
  558. Array.Copy(_buffer, 0, destinationArray, 0, _count);
  559. return destinationArray;
  560. }
  561. /// <summary>
  562. /// This function exists to allow fast iterations. The size of the array returned cannot be
  563. /// used. The list count must be used instead.
  564. /// </summary>
  565. /// <returns></returns>
  566. public T[] ToArrayFast()
  567. {
  568. return _buffer;
  569. }
  570. public bool UnorderredRemove(T item)
  571. {
  572. var index = IndexOf(item);
  573. if (index == -1)
  574. return false;
  575. UnorderredRemoveAt(index);
  576. return true;
  577. }
  578. public T UnorderredRemoveAt(int index)
  579. {
  580. DesignByContract.Check.Require(index < _count && _count > 0, "out of bound index");
  581. T item = _buffer[index];
  582. if (index == --_count)
  583. return item;
  584. T swap = _buffer[index];
  585. _buffer[index] = _buffer[_count];
  586. _buffer[_count] = swap;
  587. return item;
  588. }
  589. IEnumerator IEnumerable.GetEnumerator()
  590. {
  591. return GetEnumerator();
  592. }
  593. IEnumerator<T> IEnumerable<T>.GetEnumerator()
  594. {
  595. return GetEnumerator();
  596. }
  597. void AllocateMore()
  598. {
  599. var newList = new T[Mathf.Max(_buffer.Length << 1, MIN_SIZE)];
  600. if (_count > 0) _buffer.CopyTo(newList, 0);
  601. _buffer = newList;
  602. }
  603. void AllocateMore(int newSize)
  604. {
  605. var oldLength = Mathf.Max(_buffer.Length, MIN_SIZE);
  606. while (oldLength < newSize)
  607. oldLength <<= 1;
  608. var newList = new T[oldLength];
  609. if (_count > 0) Array.Copy(_buffer, newList, _count);
  610. _buffer = newList;
  611. }
  612. public void Trim()
  613. {
  614. if (_count < _buffer.Length)
  615. Resize(_count);
  616. }
  617. public bool Reuse<U>(int index, out U result)
  618. where U:class, T
  619. {
  620. result = default(U);
  621. if (index >= _buffer.Length)
  622. return false;
  623. result = (U)_buffer[index];
  624. return result != null;
  625. }
  626. T[] _buffer;
  627. int _count;
  628. }
  629. }