- Joined
- Dec 12, 2008
- Messages
- 7,385
This snippet just makes my life easier. I won't have to keep saving data into hashtables,
I can simply use arrays and store data based on some index below 8190.
It may seem stupid to you, but it makes things seem a bit more readable for me.
Feel free to comment..
I can simply use arrays and store data based on some index below 8190.
It may seem stupid to you, but it makes things seem a bit more readable for me.
JASS:
/*******************************************
*
* RawCodeIndexer
* v2.0.0.0
* By Magtheridon96
*
* - Generates a very small index for very large integers. (For array storage)
* - Basically a wrapper.
*
* Optional Requirements:
* ----------------------
*
* - Table by Bribe
* - hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/
*
* API:
* ----
*
* - function RawCode2Id takes integer rawCode returns integer
* - Converts a RawCode to an Id. GetRawCodeId will be usable at this point. (It's much faster and inlines)
* - function Id2RawCode takes integer id returns integer
* - Converts an Id to a raw code. (inlines)
* - function GetRawCodeId takes integer rawCode returns integer
* - Retrieves the Id for a given raw code (0 if not converted with RawCode2Id at least once) (inlines)
*
*******************************************/
library RawCodeIndexer requires optional Table
globals
private integer array codes
private integer count = 0
endglobals
static if LIBRARY_Table then
private module Init
private static method onInit takes nothing returns nothing
set tb = Table.create()
endmethod
endmodule
endif
private struct Hash extends array
static if LIBRARY_Table then
static Table tb
implement Init
else
static hashtable ht = InitHashtable()
endif
endstruct
// Just to make RawCode2Id look shorter
private function SetRawCodeId takes integer raw, integer id returns nothing
static if LIBRARY_Table then
set Hash.tb[raw] = id
else
call SaveInteger(Hash.ht, raw, 0, id)
endif
endfunction
function GetRawCodeId takes integer raw returns integer
static if LIBRARY_Table then
return Hash.tb[raw]
else
return LoadInteger(Hash.ht, raw, 0)
endif
endfunction
function RawCode2Id takes integer i returns integer
local integer x = GetRawCodeId(i)
if x == 0 then
set x = count + 1
set count = x
call SetRawCodeId(i, x)
set codes[x] = i
endif
return x
endfunction
function Id2RawCode takes integer id returns integer
return codes[id]
endfunction
endlibrary
Feel free to comment..
Last edited: