FastStack.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #if !USE_DYNAMIC_STACKS
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace MoonSharp.Interpreter.DataStructs
  7. {
  8. public class FastStack<T> : IList<T>
  9. {
  10. T[] m_Storage;
  11. int m_HeadIdx = 0;
  12. public FastStack(int maxCapacity)
  13. {
  14. m_Storage = new T[maxCapacity];
  15. }
  16. public T this[int index]
  17. {
  18. get { return m_Storage[index]; }
  19. set { m_Storage[index] = value; }
  20. }
  21. public T Push(T item)
  22. {
  23. m_Storage[m_HeadIdx++] = item;
  24. return item;
  25. }
  26. public void Expand(int size)
  27. {
  28. m_HeadIdx += size;
  29. }
  30. private void Zero(int from, int to)
  31. {
  32. Array.Clear(m_Storage, from, to - from + 1);
  33. }
  34. private void Zero(int index)
  35. {
  36. m_Storage[index] = default(T);
  37. }
  38. public T Peek(int idxofs = 0)
  39. {
  40. T item = m_Storage[m_HeadIdx - 1 - idxofs];
  41. return item;
  42. }
  43. public void CropAtCount(int p)
  44. {
  45. RemoveLast(Count - p);
  46. }
  47. public void RemoveLast( int cnt = 1)
  48. {
  49. if (cnt == 1)
  50. {
  51. --m_HeadIdx;
  52. m_Storage[m_HeadIdx] = default(T);
  53. }
  54. else
  55. {
  56. int oldhead = m_HeadIdx;
  57. m_HeadIdx -= cnt;
  58. Zero(m_HeadIdx, oldhead);
  59. }
  60. }
  61. public T Pop()
  62. {
  63. --m_HeadIdx;
  64. T retval = m_Storage[m_HeadIdx];
  65. m_Storage[m_HeadIdx] = default(T);
  66. return retval;
  67. }
  68. public void Clear()
  69. {
  70. Array.Clear(m_Storage, 0, m_Storage.Length);
  71. m_HeadIdx = 0;
  72. }
  73. public int Count
  74. {
  75. get { return m_HeadIdx; }
  76. }
  77. #region IList<T> Impl.
  78. int IList<T>.IndexOf(T item)
  79. {
  80. throw new NotImplementedException();
  81. }
  82. void IList<T>.Insert(int index, T item)
  83. {
  84. throw new NotImplementedException();
  85. }
  86. void IList<T>.RemoveAt(int index)
  87. {
  88. throw new NotImplementedException();
  89. }
  90. T IList<T>.this[int index]
  91. {
  92. get
  93. {
  94. return this[index];
  95. }
  96. set
  97. {
  98. this[index] = value;
  99. }
  100. }
  101. void ICollection<T>.Add(T item)
  102. {
  103. Push(item);
  104. }
  105. void ICollection<T>.Clear()
  106. {
  107. Clear();
  108. }
  109. bool ICollection<T>.Contains(T item)
  110. {
  111. throw new NotImplementedException();
  112. }
  113. void ICollection<T>.CopyTo(T[] array, int arrayIndex)
  114. {
  115. throw new NotImplementedException();
  116. }
  117. int ICollection<T>.Count
  118. {
  119. get { return this.Count; }
  120. }
  121. bool ICollection<T>.IsReadOnly
  122. {
  123. get { return false; }
  124. }
  125. bool ICollection<T>.Remove(T item)
  126. {
  127. throw new NotImplementedException();
  128. }
  129. IEnumerator<T> IEnumerable<T>.GetEnumerator()
  130. {
  131. throw new NotImplementedException();
  132. }
  133. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  134. {
  135. throw new NotImplementedException();
  136. }
  137. #endregion
  138. }
  139. }
  140. #endif