Guest User

Untitled

a guest
Oct 20th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. public class FastList<T>
  2. {
  3. private readonly List<T> _internalList = new List<T>();
  4. public int Count => _count;
  5. private int _count;
  6. private int _freeIndex;
  7.  
  8. public T this[int index] => _internalList[index];
  9.  
  10. public int Add(T input)
  11. {
  12. while (true)
  13. {
  14. if (_freeIndex == _count)
  15. {
  16. _internalList.Add(input);
  17. return _count++;
  18. }
  19. else
  20. {
  21. if (_internalList[_freeIndex] == null || _internalList.Equals(default(T)))
  22. {
  23. _internalList[_freeIndex] = input;
  24. return _freeIndex++;
  25. }
  26. else
  27. {
  28. _freeIndex++;
  29. }
  30. }
  31. }
  32. }
  33.  
  34. public void Remove(int index)
  35. {
  36. _internalList[index] = default(T);
  37. Interlocked.Exchange(ref _freeIndex, index);
  38. }
  39.  
  40. public bool Exists(int index)
  41. {
  42. if (_count < index || _internalList[index] == null || _internalList[index].Equals(default(T)))
  43. return false;
  44. return true;
  45. }
  46.  
  47. public bool TryGetValue(int key, out T value)
  48. {
  49. if (!Exists(key))
  50. {
  51. value = default(T);
  52. return false;
  53. }
  54. else
  55. {
  56. value = _internalList[key];
  57. return true;
  58. }
  59. }
  60.  
  61. public bool TryPickValue(int key, out T value)
  62. {
  63. if (!Exists(key))
  64. {
  65. value = default(T);
  66. return false;
  67. }
  68. else
  69. {
  70. value = _internalList[key];
  71. Remove(key);
  72. return true;
  73. }
  74. }
  75. }
Add Comment
Please, Sign In to add comment