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