- Joined
- Dec 19, 2012
- Messages
- 411
JASS:
library MultiDimension initializer Init /*
*/requires Table, /* [url]http://www.hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/[/url]
*/optional ErrorMessage/* [url]https://github.com/nestharus/JASS/tree/master/jass/Systems/ErrorMessage[/url]
MultiDimension made by DD_LegionTN
A dimension library, made for simplify the complexity of an array
Supports minimum 2D, maximum 4D
Syntax inspired by New_Table
API :
struct Dimension
readonly integer size
static method create takes integer i returns nothing
i : input dimension size, minimum 2, maximum 4
method destroy takes nothing returns nothing
method operator [] takes integer i returns thistype
i : input value, as long as no less than 0 or greater than ARRAY_MAX_SIZE
For 2D : [a][b] : a value will be stored first and then b value
For 3D : [a][b][c] : a first, b second, c last
For 4D : [a][b][c][d] : a first, b second, c third, d last
*/
globals
//Debug purpose, an addition debug for retrieving stored/load key value.
private constant boolean INTERNAL_DEBUG = true
//Below globals variables are non-configurable
//constant array which able to determine the max size of a dimension
private integer array ARRAY_MAX_SIZE
//constant array used for formula
private integer array ARRAY_MULTIPLY
//Debug purpose, minimum dimension size
debug integer MIN_SIZE = 2
//Debug purpose, maximum dimension size
debug integer MAX_SIZE = 4
endglobals
private function Init takes nothing returns nothing
//multiplication of array must not exceed 2147483647 (2^31-1)
//Formula for checking 2D maximum storage size:
//ARRAY_MAX_SIZE[8]*ARRAY_MULTIPLY[8] + ARRAY_MAX_SIZE[9]
//2D constant
//[array8][array9]
set ARRAY_MAX_SIZE[8] = 99
set ARRAY_MAX_SIZE[9] = 99
//Formula for checking 3D maximum storage size:
//ARRAY_MAX_SIZE[12]*ARRAY_MULTIPLY[12] + ARRAY_MAX_SIZE[13]*ARRAY_MULTIPLY[13] + ARRAY_MAX_SIZE[14]
//3D constant
//[array12][array13][array14]
set ARRAY_MAX_SIZE[12] = 99
set ARRAY_MAX_SIZE[13] = 99
set ARRAY_MAX_SIZE[14] = 99
//Formula for checking 4D maximum storage size:
//ARRAY_MAX_SIZE[16]*ARRAY_MULTIPLY[16] + ARRAY_MAX_SIZE[17]*ARRAY_MULTIPLY[17] + ARRAY_MAX_SIZE[18]*ARRAY_MULTIPLY[18] + ARRAY_MAX_SIZE[19]
//4D constant
//[array16][array17][array18][array19]
set ARRAY_MAX_SIZE[16] = 99
set ARRAY_MAX_SIZE[17] = 99
set ARRAY_MAX_SIZE[18] = 99
set ARRAY_MAX_SIZE[19] = 99
/*
End of configuration
*/
//Non-configurable
set ARRAY_MULTIPLY[8] = (ARRAY_MAX_SIZE[8]+1)*(ARRAY_MAX_SIZE[9]+1)
set ARRAY_MULTIPLY[13] = (ARRAY_MAX_SIZE[13]+1)*(ARRAY_MAX_SIZE[14]+1)
set ARRAY_MULTIPLY[12] = (ARRAY_MAX_SIZE[12]+1)*ARRAY_MULTIPLY[13]
set ARRAY_MULTIPLY[18] = (ARRAY_MAX_SIZE[18]+1)*(ARRAY_MAX_SIZE[19]+1)
set ARRAY_MULTIPLY[17] = (ARRAY_MAX_SIZE[17]+1)*ARRAY_MULTIPLY[18]
set ARRAY_MULTIPLY[16] = (ARRAY_MAX_SIZE[16]+1)*ARRAY_MULTIPLY[17]
endfunction
//! textmacro ADD_TYPE takes TYPE, EMPTY, TYPE2
private module $TYPE$a
method operator $TYPE$ takes nothing returns $TYPE$
local integer i = thistype.retrieveStoredInt()
static if DEBUG_MODE then
if inputSize != 0 then
static if LIBRARY_ErrorMessage then
call ThrowError(true, "Multidimension", "operator []", "size", this, "Input dimension size greater/lesser than " + I2S(size))
else
call BJDebugMsg("Multidimension - operator []: Input dimension size greater/lesser than " + I2S(size) + ", this : " + I2S(this))
endif
elseif storedInt[1] == -1 then
static if LIBRARY_ErrorMessage then
call ThrowError(true, "Multidimension", "operator []", "size", this, "0 input dimension size!")
else
call BJDebugMsg("Multidimension - operator []: 0 input dimension size! this : " + I2S(this))
endif
endif
endif
call thistype.clear()
static if INTERNAL_DEBUG and DEBUG_MODE then
call BJDebugMsg("this.tb.$TYPE$[" + I2S(i) + "] loaded")
endif
return this.tb.$TYPE$[i]
endmethod
method operator $TYPE$= takes $TYPE$ b returns nothing
static if DEBUG_MODE then
if inputSize != 0 then
static if LIBRARY_ErrorMessage then
call ThrowError(true, "Multidimension", "operator []", "size", this, "Input dimension size greater/lesser than " + I2S(size))
else
call BJDebugMsg("Multidimension - operator []: Input dimension size greater/lesser than " + I2S(size) + ", this : " + I2S(this))
endif
elseif storedInt[1] == -1 then
static if LIBRARY_ErrorMessage then
call ThrowError(true, "Multidimension", "operator []", "size", this, "0 input dimension size!")
else
call BJDebugMsg("Multidimension - operator []: 0 input dimension size! this : " + I2S(this))
endif
endif
endif
set storedIntLoaded = storedIntLoaded + 1
set load = load + 1
if b == $EMPTY$ then
call this.tb.$TYPE2$.remove(storedInt[load])
static if INTERNAL_DEBUG and DEBUG_MODE then
call BJDebugMsg("this.tb.$TYPE$[" + I2S(storedInt[load]) + "] removed")
endif
else
set this.tb.$TYPE$[storedInt[load]] = b
static if INTERNAL_DEBUG and DEBUG_MODE then
call BJDebugMsg("this.tb.$TYPE$[" + I2S(storedInt[load]) + "] stored")
endif
endif
call thistype.clear()
endmethod
endmodule
//! endtextmacro
//! runtextmacro ADD_TYPE("integer", "0", "integer")
//! runtextmacro ADD_TYPE("real", "0.", "real")
//! runtextmacro ADD_TYPE("boolean", "false", "boolean")
//! runtextmacro ADD_TYPE("string", "\"\"", "string")
//! runtextmacro ADD_TYPE("player", "null", "handle")
//! runtextmacro ADD_TYPE("destructable", "null", "handle")
//! runtextmacro ADD_TYPE("item", "null", "handle")
//! runtextmacro ADD_TYPE("unit", "null", "handle")
//! runtextmacro ADD_TYPE("ability", "null", "handle")
//! runtextmacro ADD_TYPE("timer", "null", "handle")
//! runtextmacro ADD_TYPE("trigger", "null", "handle")
//! runtextmacro ADD_TYPE("triggercondition", "null", "handle")
//! runtextmacro ADD_TYPE("rect", "null", "handle")
//! runtextmacro ADD_TYPE("effect", "null", "handle")
//! runtextmacro ADD_TYPE("widget", "null", "handle")
//! runtextmacro ADD_TYPE("triggeraction", "null", "handle")
//! runtextmacro ADD_TYPE("event", "null", "handle")
//! runtextmacro ADD_TYPE("force", "null", "handle")
//! runtextmacro ADD_TYPE("group", "null", "handle")
//! runtextmacro ADD_TYPE("location", "null", "handle")
//! runtextmacro ADD_TYPE("boolexpr", "null", "handle")
//! runtextmacro ADD_TYPE("sound", "null", "handle")
//! runtextmacro ADD_TYPE("unitpool", "null", "handle")
//! runtextmacro ADD_TYPE("itempool", "null", "handle")
//! runtextmacro ADD_TYPE("quest", "null", "handle")
//! runtextmacro ADD_TYPE("questitem", "null", "handle")
//! runtextmacro ADD_TYPE("defeatcondition", "null", "handle")
//! runtextmacro ADD_TYPE("timerdialog", "null", "handle")
//! runtextmacro ADD_TYPE("leaderboard", "null", "handle")
//! runtextmacro ADD_TYPE("multiboard", "null", "handle")
//! runtextmacro ADD_TYPE("multiboarditem", "null", "handle")
//! runtextmacro ADD_TYPE("trackable", "null", "handle")
//! runtextmacro ADD_TYPE("dialog", "null", "handle")
//! runtextmacro ADD_TYPE("button", "null", "handle")
//! runtextmacro ADD_TYPE("texttag", "null", "handle")
//! runtextmacro ADD_TYPE("lightning", "null", "handle")
//! runtextmacro ADD_TYPE("image", "null", "handle")
//! runtextmacro ADD_TYPE("ubersplat", "null", "handle")
//! runtextmacro ADD_TYPE("region", "null", "handle")
//! runtextmacro ADD_TYPE("fogstate", "null", "handle")
//! runtextmacro ADD_TYPE("fogmodifier", "null", "handle")
//! runtextmacro ADD_TYPE("hashtable", "null", "handle")
struct Dimension extends array
implement integera
implement reala
implement booleana
implement stringa
implement playera
implement destructablea
implement itema
implement unita
implement abilitya
implement timera
implement triggera
implement triggerconditiona
implement eventa
implement effecta
implement widgeta
implement triggeractiona
implement forcea
implement groupa
implement locationa
implement boolexpra
implement sounda
implement unitpoola
implement itempoola
implement questa
implement questitema
implement defeatconditiona
implement timerdialoga
implement leaderboarda
implement multiboarda
implement multiboarditema
implement trackablea
implement dialoga
implement buttona
implement texttaga
implement lightninga
implement imagea
implement ubersplata
implement regiona
implement fogstatea
implement fogmodifiera
implement hashtablea
private static integer instanceCount = 0
private static thistype recycle = 0
private thistype recycleNext
readonly integer size
private static integer storedIntCount = 0
private static integer storedIntLoaded = 0
private static integer load = 0
private static integer inputSize = 0
private static integer array storedInt
private Table tb
static method create takes integer i returns thistype
local thistype this
if (recycle == 0) then
set instanceCount = instanceCount + 1
set this = instanceCount
else
set this = recycle
set recycle = recycle.recycleNext
endif
static if DEBUG_MODE then
if i < MIN_SIZE or i > MAX_SIZE then
static if LIBRARY_ErrorMessage then
call ThrowError(true, "Multidimension", "create", "i", 0, "assigned dimension size lesser/greater than " + I2S(i))
else
call BJDebugMsg("Multidimension - create() : assigned dimension size lesser/greater than " + I2S(i))
return 1/0
endif
endif
endif
set size = i
set tb = Table.create()
return this
endmethod
method destroy takes nothing returns nothing
set recycleNext = recycle
set recycle = this
call tb.destroy()
endmethod
private static method clear takes nothing returns nothing
if storedIntLoaded == storedIntCount then
set storedIntLoaded = 0
set storedIntCount = 0
set load = 0
debug set storedInt[1] = -1
endif
endmethod
private static method retrieveStoredInt takes nothing returns integer
set storedIntLoaded = storedIntLoaded + 1
return storedInt[storedIntCount]
endmethod
method operator [] takes integer i returns thistype
set inputSize = inputSize + 1
static if DEBUG_MODE then
if i > ARRAY_MAX_SIZE[size*4+(inputSize-1)] then
static if LIBRARY_ErrorMessage then
call ThrowError(true, "Multidimension", "operator []", "none", this, "input value : " + I2S(i) + " greater than ARRAY_MAX_SIZE[" + I2S(size*4+(inputSize-1)) + "]!")
else
call BJDebugMsg("Multidimension - operator [] : input value " + I2S(i)+ " greater than ARRAY_MAX_SIZE[" + I2S(size*4+(inputSize-1)) + "]! this : " + I2S(this))
return 1/0
endif
elseif i < 0 then
static if LIBRARY_ErrorMessage then
call ThrowError(true, "Multidimension", "operator []", "none", this, "input value : " + I2S(i) + " cannot be lesser than 0!")
else
call BJDebugMsg("Multidimension - operator [] : input value " + I2S(i)+ " cannot be lesser than 0! this : " + I2S(this))
return 1/0
endif
endif
endif
if inputSize == size then
set storedInt[storedIntCount] = storedInt[storedIntCount] + i
set inputSize = 0
else
if inputSize == 1 then
set storedIntCount = storedIntCount + 1
set storedInt[storedIntCount] = 0
endif
set storedInt[storedIntCount] = storedInt[storedIntCount] + i*ARRAY_MULTIPLY[size*4+(inputSize-1)]
endif
return this
endmethod
static if DEBUG_MODE then
private static method onInit takes nothing returns nothing
//Setting -1 to indicate no value assigned
set storedInt[1] = -1
endmethod
endif
endstruct
endlibrary
Demo Code :
JASS:
function Trig_Untitled_Trigger_001_Actions takes nothing returns nothing
local Dimension d = Dimension.create(2)
local Dimension d2 = Dimension.create(3)
local Dimension d3 = Dimension.create(4)
set d[0][0].integer = 5 //store integer
set d[0][1].real = 6.4 //store real
set d[7][2].player = Player(0) //store player
//load player handle
set d2[1][2][88].unit = CreateUnit(d[7][2].player, 'hfoo', 0, 0, 0) // store unit
set d3[0][3][1][42].multiboard = CreateMultiboard() //create a multiboard
call d.destroy() //destroy it
endfunction
//===========================================================================
function InitTrig_Untitled_Trigger_001 takes nothing returns nothing
set gg_trg_Untitled_Trigger_001 = CreateTrigger( )
call TriggerRegisterTimerEventSingle( gg_trg_Untitled_Trigger_001, 0.00 )
call TriggerAddAction( gg_trg_Untitled_Trigger_001, function Trig_Untitled_Trigger_001_Actions )
endfunction
Since there is no simple library that could create dimension array, therefore i made 1.
Someone would think 2D array is sufficient and there is no point for creating more dimension. But whatever, more resource is still good, let the others choose what they want
EDIT : Found a small mistake made by myself, changed :
JASS:
if b != $EMPTY$ then
call this.tb.$TYPE2$.remove(storedInt[load])
endif
static if INTERNAL_DEBUG and DEBUG_MODE then
call BJDebugMsg("this.tb.$TYPE$[" + I2S(storedInt[load]) + "] stored")
endif
set this.tb.$TYPE$[storedInt[load]] = b
JASS:
if b == $EMPTY$ then
call this.tb.$TYPE2$.remove(storedInt[load])
static if INTERNAL_DEBUG and DEBUG_MODE then
call BJDebugMsg("this.tb.$TYPE$[" + I2S(storedInt[load]) + "] removed")
endif
else
set this.tb.$TYPE$[storedInt[load]] = b
static if INTERNAL_DEBUG and DEBUG_MODE then
call BJDebugMsg("this.tb.$TYPE$[" + I2S(storedInt[load]) + "] stored")
endif
endif
Last edited: