FastStackDynamic.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace MoonSharp.Interpreter.DataStructs
  6. {
  7. #if USE_DYNAMIC_STACKS
  8. public class FastStack<T> : FastStackDynamic<T>
  9. #endif
  10. /// <summary>
  11. /// A non preallocated, non_fixed size stack
  12. /// </summary>
  13. /// <typeparam name="T"></typeparam>
  14. internal class FastStackDynamic<T> : List<T>
  15. {
  16. public FastStackDynamic(int startingCapacity)
  17. : base(startingCapacity)
  18. {
  19. }
  20. public T Push(T item)
  21. {
  22. this.Add(item);
  23. return item;
  24. }
  25. public void Expand(int size)
  26. {
  27. for(int i = 0; i < size; i++)
  28. this.Add(default(T));
  29. }
  30. public void Zero(int index)
  31. {
  32. this[index] = default(T);
  33. }
  34. public T Peek(int idxofs = 0)
  35. {
  36. T item = this[this.Count - 1 - idxofs];
  37. return item;
  38. }
  39. public void CropAtCount(int p)
  40. {
  41. RemoveLast(Count - p);
  42. }
  43. public void RemoveLast( int cnt = 1)
  44. {
  45. if (cnt == 1)
  46. {
  47. this.RemoveAt(this.Count - 1);
  48. }
  49. else
  50. {
  51. this.RemoveRange(this.Count - cnt, cnt);
  52. }
  53. }
  54. public T Pop()
  55. {
  56. T retval = this[this.Count - 1];
  57. this.RemoveAt(this.Count - 1);
  58. return retval;
  59. }
  60. }
  61. }