• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[JASS] Stacks

Status
Not open for further replies.
Level 4
Joined
Nov 24, 2007
Messages
55
I mean a "LIFO" stack, where I can push a data type on to it, pop a data type off of it, or peek to see what's on it.

Although, since jass is no where near supporting generics, i figure i should just make my own (since i need a stack for locations.) Let me ask you though, (assuming I make it proper), is this the type of thing to be added to the JASS resources?
 
Level 20
Joined
Apr 22, 2007
Messages
1,960
If I understood correctly, well... people don't really use systems for this. They just inline all needed code required to make a stack. It's used quite often:
JASS:
// stacking some data:
set ARRAY[COUNT] = data
set COUNT = COUNT + 1

// removing data from the stack:
set COUNT = COUNT - 1
set ARRAY[thisIndex] = ARRAY[COUNT]

But I probably misunderstood you...
 
Level 12
Joined
Apr 27, 2008
Messages
1,228
I have no idea what you are talking about(maybe I had to flunk from school a little bit less) :D
I just asked so that I would learn what stacks are(and I have learned) and if by any chance I have seen something like that, to point you.
But I have not. And I believe such a thing has not been done.
But aren't stacks like arrays, but with some "sorting".
 
Level 12
Joined
Apr 27, 2008
Messages
1,228
Yes, that is some sorting.
Another possibility would be to move all the elements after thisIndex 1 index ahead. But that would be slower.
P.s. When I posted, I did not know you have posted before me ;)
 
Level 4
Joined
Nov 24, 2007
Messages
55
I was referring to the Data Structure of a stack...basically has an array backing, but the concept is that you Push something onto the Top of the stack, and you can only access the Top of a stack, via a Peek method (just gives you that item) or a Pop call (which not only returns it but also removes it from the stack...now whether the implementation actually removes it or not, that's up to the implementor). Thus it exhibits LIFO characteristics (Last In, First Out).

and this is a stack for locations that i'm just putting in my header script
JASS:
struct locStack
    location array a[100]
    // the Top Of Stack represents our empty position
    integer tos = 0
endstruct

function pushStack takes locStack s, location loc returns nothing
    if( s.tos <= 99 ) then
        set s.a[s.tos] = loc
        set s.tos = s.tos + 1
    else
    endif
endfunction

function popStack takes locStack s returns location
    if( s.tos >= 1 ) then
        set s.tos = s.tos - 1
        return s.a[s.tos]
    else
        return null
    endif
endfunction

function peekStack takes locStack s returns location
    return s.a[s.tos]
endfunction

if you do see something wrong, please let me know. but this should cover it...sorry for the confusion lulz.

Edit: lol quick edit too. forgot to check to make sure they don't go over the 100 size...
 
Level 3
Joined
Jun 3, 2008
Messages
52
That's a neat way of making a Stack data-type. Hmm... So I guess a Queue data-type could be something very similar to this...
 
Level 12
Joined
Apr 27, 2008
Messages
1,228
Reading tutorials and manuals is a good idea:
JASS:
type stackarray extends integer array [20]

struct stack
   private stackarray V
   private integer N=0

   method Create takes nothing returns stack
    local stack s=stack.create()
       set s.V=stackarray.create()
       if (s.V==0) then
           debug call BJDebugMsg("Warning: not enough space for stack array")
           return 0
       endif
   endmethod

   method push takes integer i returns nothing
       if (this.N==stackarray.size) then
           debug call BJDebugMsg("Warning: stack is full")
       else
           set this.V[this.N]=i
           set .N = .N +1 //remember this syntax is valid as well
       endif
   endmethod

   method pop takes nothing returns nothing
       if (this.N>0) then
           set this.N=this.N-1
       else
           debug call BJDebugMsg("Warning: attempt to pop an empty stack");
       endif
   endmethod

   method top takes nothing returns integer
       return .V[.N-1]
   endmethod

   method empty takes nothing returns boolean
       return (.N==0)
   endmethod

   method onDestroy takes nothing returns nothing
       call this.V.destroy()
   endmethod
endstruct
Directly copied from JassHelper Manual
 
Level 3
Joined
Jun 3, 2008
Messages
52
You can use methods in vJASS? You can use private fields? I didn't know that. Where is this JassHelper manual?
 
Status
Not open for further replies.
Top