Jesus4Lyf
Good Idea™
- Reaction score
- 397
I thought this was too cool not to share.
What's this? It's a boolean array type. You've heard of vJassified gamecache. Now meet vJassified integers! All this does is look at an integer like an array of binary bits, and plucks out the bit requested (from least significant value to most).
HUH? WHAT? Shh. It's an array of booleans. Indexes go from 0-30. There is no need to destroy these as they don't leak. Furthermore, you can have unlimited of these in a map (yes, more than 8191). In fact, these don't even really exist. All they are is integers.
The latest in the "Black Magic" series.
Since I can't upload new maps still, here is a test script showing that this actually works, and is really quite neat.
(PS. I'm outa here for ~3 days after today, catchya all then.)
JASS:
library BoolArray initializer InitBoolArrayData
// Written by Jesus4Lyf.
// No need to destroy boolarrays. They don't leak.
// Use: local boolarray b=boolarray.create()
// set b[0]=true
// set b[30]=not b[0]
// etc.
// Indexes span from 0 to 30 (inclusive).
// All indexes are set to "false" by default on creation.
// These are value based objects, not reference based.
// So treat them like a native type, ie string or integer.
// If you pass it into a function and change it there, the
// change will not be visible in the caller. <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite1" alt=":)" title="Smile :)" loading="lazy" data-shortname=":)" />
globals
private integer array ValueForBinaryIndex
private integer tempindexval
endglobals
struct boolarray
static method create takes nothing returns boolarray
return 0
endmethod
method operator [] takes integer index returns boolean
// The below line: How may times can 2^index be subtracted from this?
set index=this/ValueForBinaryIndex[index]
// Return: Is the number odd? (Hence a 1 in the index column.)
return index-(index/2)*2==1
// The above line was originally replaced by:
// return index-(index/2)*2
// return false
// But I thought i'd avoid the typecast.
endmethod
method operator []= takes integer index, boolean value returns boolarray
// Following similar logic...
set tempindexval=ValueForBinaryIndex[index]
set index=this/tempindexval
if (index/2)*2==index then // If bool at index is currently false...
if value then
return this+tempindexval // Set it to true.
endif
return this // Leave it.
endif // If bool at index is currently true...
if value then
return this // Leave it.
endif
return this-tempindexval // Set it to false.
endmethod
endstruct
private function InitBoolArrayData takes nothing returns nothing
local integer i=0
local integer value=1
loop
set ValueForBinaryIndex<i>=value
exitwhen i==30 // Max index for xbools is 30.
set i=i+1
set value=value+value // set value = value * 2
endloop
endfunction
endlibrary
</i>
What's this? It's a boolean array type. You've heard of vJassified gamecache. Now meet vJassified integers! All this does is look at an integer like an array of binary bits, and plucks out the bit requested (from least significant value to most).
HUH? WHAT? Shh. It's an array of booleans. Indexes go from 0-30. There is no need to destroy these as they don't leak. Furthermore, you can have unlimited of these in a map (yes, more than 8191). In fact, these don't even really exist. All they are is integers.
The latest in the "Black Magic" series.
Since I can't upload new maps still, here is a test script showing that this actually works, and is really quite neat.
JASS:
scope TestXBool initializer DoTest
private function DoTest takes nothing returns nothing
local boolarray xb=boolarray.create()
set xb[1]=true
set xb[2]=false
set xb[3]=true
set xb[20]=false
set xb[21]=true
if xb[1] then
call BJDebugMsg("1 correct")
endif
if not xb[2] then
call BJDebugMsg("2 correct")
endif
if xb[3] then
call BJDebugMsg("3 correct")
endif
if not xb[20] then
call BJDebugMsg("20 correct")
endif
if xb[21] then
call BJDebugMsg("21 correct")
endif
set xb[30]=true
if xb[30] then
call BJDebugMsg("Last correct")
endif
endfunction
endscope
(PS. I'm outa here for ~3 days after today, catchya all then.)