Stack.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. const ComputerParameterMode = require("./ComputerParameterMode");
  2. /**
  3. * The stack, or memory, for the Intcode Computer
  4. *
  5. * @author Apis Necros
  6. */
  7. module.exports = class Stack {
  8. constructor(stack) {
  9. this.pointer = 0;
  10. this._stack = stack;
  11. }
  12. /**
  13. * Move the stack's pointer to the right by 1
  14. * @returns {void}
  15. */
  16. Next() {
  17. this.pointer++;
  18. return this;
  19. }
  20. /**
  21. * Move the stack's pointer to the left by 1
  22. * @returns {void}
  23. */
  24. Prev() {
  25. this.pointer--;
  26. }
  27. /**
  28. * Get the value from the stack by the pointer's current position
  29. *
  30. * @param {number} [parameterMode=0] The Parameter Mode to use to retrieve the value
  31. * @returns {number} The value at the current pointer position on the stack
  32. */
  33. Get(parameterMode = ComputerParameterMode.POSITION_MODE) {
  34. let value = this._stack[this.pointer];
  35. if (parameterMode == ComputerParameterMode.POSITION_MODE) {
  36. value = this._stack[value];
  37. }
  38. return value;
  39. }
  40. /**
  41. * Get a value at a given index on the stack
  42. *
  43. * @param {number} index The index of the value to retrieve
  44. * @param {number} [parameterMode=0] The Parameter Mode to use to retrieve the value
  45. * @returns {number} The value at the given index, or 0 if the index hasn't been defined yet
  46. */
  47. GetAtIndex(index, parameterMode = ComputerParameterMode.POSITION_MODE) {
  48. if (index == null || Number.isNaN(index) || !Number.isInteger(index)) {
  49. throw new TypeError("index must be an integer");
  50. }
  51. try {
  52. let value = this._stack[index];
  53. if (parameterMode == ComputerParameterMode.POSITION_MODE) {
  54. value = this._stack[value];
  55. }
  56. return value;
  57. }
  58. catch (e) {
  59. return 0;
  60. }
  61. }
  62. /**
  63. * Use the value at the current pointer to get a value at another position
  64. *
  65. * This is essentially shorthand for `stack.Get(stack.Get())`
  66. *
  67. * @returns {number} The value at the index given by the pointer's current position
  68. */
  69. GetUsingStackValue() {
  70. return this.Get(this._stack[this.pointer]);
  71. }
  72. /**
  73. * Push a new value onto the end of the stack
  74. *
  75. * @param {number} value The value to add
  76. * @returns {void}
  77. */
  78. Push(value) {
  79. this._stack[++this.pointer] = value;
  80. }
  81. /**
  82. * Pop a value off the end of the stack
  83. *
  84. * @returns {number}
  85. * @returns {void}
  86. */
  87. Pop() {
  88. return this._stack.pop();
  89. }
  90. /**
  91. * Overwrite the value at a given index with a new value
  92. *
  93. * The index need not be defined
  94. *
  95. * @param {number} index The index on the stack to write a value to
  96. * @param {number} value The value to write to that position
  97. * @returns {void}
  98. */
  99. Put(index, value) {
  100. this._stack[index] = value;
  101. }
  102. /**
  103. * Return the whole stack
  104. *
  105. * @returns {number[]} The current stack
  106. */
  107. Dump() {
  108. return this._stack;
  109. }
  110. };