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