- Joined
- Dec 12, 2008
- Messages
- 7,385
You have no idea how many times I have to repeat writing this snippet of code:
It's a light stack object.
Iteration is done with the
You'd iterate through the stack as if you were iterating through any other indexed array.
Feel free to comment.
JASS:
/***********************************************
*
* Stack
* v1.0.0.0
* By Magtheridon96
*
* - Light stack object.
* - Similar to indexed array.
*
* API:
* ----
*
* - struct Stack extends array
*
* - static method create takes nothing returns thistype
* - Creates a new stack.
* - method destroy takes nothing returns nothing
* - Destroys a stack.
* - method push takes integer value returns nothing
* - Adds a value to the top of a stack.
* - method pop takes nothing returns integer
* - Removes the top of a stack and returns the value.
* - method get takes integer index returns integer
* - Gets a value from the stack given the index (Zero being the first).
* - method set takes integer index, integer value returns nothing
* - Sets a value in the stack given the index.
* - method has takes integer value returns boolean
* - Determines if a value is present in the stack.
* - method operator top takes nothing returns integer
* - Returns the value at the top of a stack.
* - method operator first takes nothing returns integer
* - Returns the value at the bottom of a stack.
* - method operator size takes nothing returns integer
* - Returns the size of the stack.
*
***********************************************/
library Stack requires Table
struct Stack extends array
private static integer ic = 0
private static integer ir = 0
private static integer array rn
private static integer array count
private static Table array list
private static Table array bool
method push takes integer i returns nothing
set list[this][count[this]] = i
set bool[this][i] = bool[this][i] + 1
set count[this] = count[this] + 1
endmethod
method pop takes nothing returns integer
local integer i = list[this][count[this]-1]
set bool[this][i] = bool[this][i] - 1
set count[this] = count[this] - 1
return i
endmethod
method operator size takes nothing returns integer
return count[this]
endmethod
method operator first takes nothing returns integer
return list[this][0]
endmethod
method operator top takes nothing returns integer
return list[this][count[this]-1]
endmethod
method get takes integer index returns integer
return list[this][index]
endmethod
method set takes integer index, integer value returns nothing
set bool[this][list[this][index]] = bool[this][list[this][index]] - 1
set list[this][index] = value
set bool[this][value] = bool[this][value] + 1
endmethod
method has takes integer value returns boolean
return bool[this][value] > 0
endmethod
method destroy takes nothing returns nothing
call list[this].flush()
call bool[this].flush()
set count[this] = 0
set rn[this] = ir
set ir = this
endmethod
static method create takes nothing returns thistype
local thistype this = ir
if this == 0 then
set ic = ic + 1
set this = ic
else
set ir = rn[this]
endif
if list[this] == 0 then
set list[this] = Table.create()
set bool[this] = Table.create()
endif
return this
endmethod
endstruct
endlibrary
It's a light stack object.
Iteration is done with the
method get
.You'd iterate through the stack as if you were iterating through any other indexed array.
Feel free to comment.
Last edited: