RefStack.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using System.Collections;
  2. using System.Diagnostics.CodeAnalysis;
  3. using System.Runtime.CompilerServices;
  4. using Jint.Runtime;
  5. namespace Jint.Collections
  6. {
  7. /// <summary>
  8. /// Stack for struct types.
  9. /// </summary>
  10. internal sealed class RefStack<T> : IEnumerable<T> where T : struct
  11. {
  12. internal T[] _array;
  13. internal int _size;
  14. private const int DefaultCapacity = 2;
  15. public RefStack(int capacity = DefaultCapacity)
  16. {
  17. _array = new T[capacity];
  18. _size = 0;
  19. }
  20. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  21. public ref readonly T Peek()
  22. {
  23. return ref _array[_size - 1];
  24. }
  25. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  26. public ref readonly T Peek(int fromTop)
  27. {
  28. var index = _size - 1 - fromTop;
  29. return ref _array[index];
  30. }
  31. public T this[int index] => _array[index];
  32. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  33. public bool TryPeek([NotNullWhen(true)] out T item)
  34. {
  35. if (_size > 0)
  36. {
  37. item = _array[_size - 1];
  38. return true;
  39. }
  40. item = default;
  41. return false;
  42. }
  43. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  44. public ref readonly T Pop()
  45. {
  46. if (_size == 0)
  47. {
  48. ExceptionHelper.ThrowInvalidOperationException("stack is empty");
  49. }
  50. _size--;
  51. return ref _array[_size];
  52. }
  53. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  54. public void Push(in T item)
  55. {
  56. if (_size == _array.Length)
  57. {
  58. EnsureCapacity(_size + 1);
  59. }
  60. _array[_size++] = item;
  61. }
  62. private void EnsureCapacity(int min)
  63. {
  64. var array = _array;
  65. if (array.Length < min)
  66. {
  67. var newCapacity = array.Length == 0
  68. ? DefaultCapacity
  69. : array.Length * 2;
  70. if (newCapacity < min)
  71. {
  72. newCapacity = min;
  73. }
  74. Resize(newCapacity);
  75. }
  76. }
  77. private void Resize(int value)
  78. {
  79. if (value != _array.Length)
  80. {
  81. if (value > 0)
  82. {
  83. var newItems = new T[value];
  84. if (_size > 0)
  85. {
  86. Array.Copy(_array, 0, newItems, 0, _size);
  87. }
  88. _array = newItems;
  89. }
  90. else
  91. {
  92. _array = Array.Empty<T>();
  93. }
  94. }
  95. }
  96. public void Clear()
  97. {
  98. _size = 0;
  99. }
  100. public Enumerator GetEnumerator()
  101. {
  102. return new Enumerator(this);
  103. }
  104. IEnumerator<T> IEnumerable<T>.GetEnumerator()
  105. {
  106. return GetEnumerator();
  107. }
  108. IEnumerator IEnumerable.GetEnumerator()
  109. {
  110. return GetEnumerator();
  111. }
  112. internal struct Enumerator : IEnumerator<T>
  113. {
  114. private readonly RefStack<T> _stack;
  115. private int _index;
  116. private T? _currentElement;
  117. internal Enumerator(RefStack<T> stack)
  118. {
  119. _stack = stack;
  120. _index = -2;
  121. _currentElement = default;
  122. }
  123. public void Dispose()
  124. {
  125. _index = -1;
  126. }
  127. public bool MoveNext()
  128. {
  129. bool returnValue;
  130. if (_index == -2)
  131. {
  132. // First call to enumerator.
  133. _index = _stack._size - 1;
  134. returnValue = (_index >= 0);
  135. if (returnValue)
  136. {
  137. _currentElement = _stack._array[_index];
  138. }
  139. return returnValue;
  140. }
  141. if (_index == -1)
  142. {
  143. // End of enumeration.
  144. return false;
  145. }
  146. returnValue = (--_index >= 0);
  147. if (returnValue)
  148. {
  149. _currentElement = _stack._array[_index];
  150. }
  151. else
  152. {
  153. _currentElement = default;
  154. }
  155. return returnValue;
  156. }
  157. public T Current => (T) _currentElement!;
  158. object? IEnumerator.Current => Current;
  159. void IEnumerator.Reset()
  160. {
  161. _index = -2;
  162. _currentElement = default;
  163. }
  164. }
  165. }
  166. }