sponsored links

Collapse

Announcement

Collapse
No announcement yet.

Dynamic Typed Stack?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dynamic Typed Stack?

    I recently developed code to resemble a Stack data structure in RPG, but am curious as to how I could improve this. Currently I use a blank field ('type') that describes the type for the Stack, and use the LIKE() keyword on that type. This allows for only changing the type of the 'type' field in order to change what type the Stack supports. Is there a better way to accomplish this? Do you see any other ways that this code could be enhanced?

    Code:
    DCL-DS Stack Qualified;
          type       CHAR(10);
          array      LIKE(Stack.type) DIM(1000);
          size       ZONED(4:0) INZ(0);
    END-DS;
    
    DCL-PROC Push;
          DCL-PI *N;
            obj  LIKE(Stack.type) VALUE;
          END-PI;
    
          DCL-S i ZONED(4: 0);
    
          // Set counter to end of stack
          i = Stack.size;
    
          // Shift Stack down 1
          DOW i > 0;
            Stack.array(i+1) = Stack.array(i);
            i = i - 1;
          ENDDO;
    
          // Adds new object
          Stack.array(1) = obj;
    
          // Increments stack size
          Stack.size = Stack.size + 1;
    END-PROC;
    
    DCL-PROC Pop;
          DCL-PI *N LIKE(Stack.type);
          END-PI;
    
          DCL-S obj   LIKE(Stack.type);
          DCL-S i     ZONED(4:0) INZ(2);
    
          // Stores top object for return
          obj = Stack.array(1);
          Clear Stack.array(1);
    
          // Shift Stack up 1
          DOW i <= Stack.size;
            Stack.array(i-1) = Stack.array(i);
            i = i + 1;
          ENDDO;
    
          // Decrements stack size
          Stack.size = Stack.size - 1;
    
          // Returns top object
          RETURN obj;
    END-PROC;

  • #2
    This is not the sort of task that RPG is well-suited for -- RPG works best when the data types & sizes are known when the program is compiled.

    Having said that, it might be possible to do it by using dynamic memory, and not trying to code the data type in the array, but instead just store it's raw byte value in the array. Once you have a generic stack system set up using pointers and dynamic memory, wrap the whole thing with easier-to-use subprocedures. For example, push_char, push_int, push_packed, etc subprocedures, one for each data type so that the caller would never need to use the pointers and dynamic memory that do the work under the covers.

    Also, you could make your stacks more efficient by changing them so that they don't move all of the elements each time you push or pop. Instead, in your stack DS, have an integer that keeps track of the element number of the top of the stack. When you push, simply add one to the number, then use the new (highest indexed) array element for the top of the stack. When you pop, just subtract one from the number. Since you don't have to shift the elements around, this makes your code simpler and also makes it run a whole lot faster.

    Good luck

    Comment


    • #3
      If I was going to emulate a stack I'd create a linked list myself.

      Comment


      • JonBoy
        JonBoy commented
        Editing a comment
        And rather than reinvent the wheel I'd use one of the open source options already out there.
    Working...
    X