Ver código fonte

Add optimization to Stack.prototype.swapTwo

When executing the $ instruction, if the stack only has two elements,
then we can just reverse the stack and continue.
ApisNecros 1 ano atrás
pai
commit
4a47742d3b
1 arquivos alterados com 1 adições e 0 exclusões
  1. 1 0
      src/stack.ts

+ 1 - 0
src/stack.ts

@@ -101,6 +101,7 @@ export class Stack {
      */
     public swapTwo() {
         if(this.stack.length < 2) { throw new WrongStackSizeError(2, this.stack.length); }
+        if (this.stack.length == 2) { this.stack.reverse(); return; }
 
         const popped = this.stack.splice(this.stack.length - 2, 2);
         this.stack.push(...popped.reverse());