RefStack.cs 4.8 KB

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