Snippet BoolArray

Jesus4Lyf

Good Idea™
Reaction score
397
I thought this was too cool not to share.

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&#039;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(&quot;1 correct&quot;)
        endif
        if not xb[2] then
            call BJDebugMsg(&quot;2 correct&quot;)
        endif
        if xb[3] then
            call BJDebugMsg(&quot;3 correct&quot;)
        endif
        if not xb[20] then
            call BJDebugMsg(&quot;20 correct&quot;)
        endif
        if xb[21] then
            call BJDebugMsg(&quot;21 correct&quot;)
        endif
        set xb[30]=true
        if xb[30] then
            call BJDebugMsg(&quot;Last correct&quot;)
        endif
    endfunction
endscope

(PS. I'm outa here for ~3 days after today, catchya all then.)
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
I really don't understand the usefulness of this.
We can already have a boolean array and an extended boolean array.
 

Azlier

Old World Ghost
Reaction score
461
Well, let's assume you need to store a bunch of booleans in a struct (for some odd reason).

JASS:
struct Wtflol
    boolean array Data[30]
endstruct


That would hurt your instance limit quite a bit. Unless you extend the array, which then makes it slower.

JASS:
struct Wtflol
    boolarray Data = boolarray.create()
endstruct
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
Oh well.
Still seems not useful for me but i've understood the point of this thing, thx.
 

Azlier

Old World Ghost
Reaction score
461
Why use GC when you can replace it with something cooler?
 

Azlier

Old World Ghost
Reaction score
461
30 arrays? No, infinite arrays with 30 booleans in each.
 

SerraAvenger

Cuz I can
Reaction score
234
JASS:
scope TestXBool initializer DoTest
    private function DoTest takes nothing returns nothing
        local boolarray xb=boolarray.create()
        local boolarray yb=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(&quot;1 correct&quot;)
        endif
        if not xb[2] then
            call BJDebugMsg(&quot;2 correct&quot;)
        endif
        if yb[3] then
            call BJDebugMsg(&quot;3 correct&quot;)
        endif
        if not xb[20] then
            call BJDebugMsg(&quot;20 correct&quot;)
        endif
        if yb[21] then
            call BJDebugMsg(&quot;21 correct&quot;)
        endif
        set xb[30]=true
        if yb[30] then
            call BJDebugMsg(&quot;Last correct&quot;)
        endif
    endfunction
endscope


glancing at your code, this came to my mind. Just away testing it.

hummm...
how do you change "this" in place?
 

Jesus4Lyf

Good Idea™
Reaction score
397
I don't know what you mean, please explain in more detail.

But I will take a random guess.
The xb/yb are integers, which change when you use set xb=.
The []= operator actually returns a value, which is unusual but vJass actually allows it for this kind of purpose. :)

You can make operators return values, which actually writes to the variable used.
 

Azlier

Old World Ghost
Reaction score
461
The Jasshelper manual. He told me.
 

Jesus4Lyf

Good Idea™
Reaction score
397
Yep. Saw it in there one day...

I used it in CodeVar too. It's not just the []= operator, you can have things like value= operators, which means when you say set structVar.value=something, it can change the value of structVar.
In CodeVar I had a code= operator. So you can write things that look like fields (or "members") which are actually methods. And these can return new values for the variable they're called on (or at least the "=" operators can, I doubt the others can too). :)
 

Romek

Super Moderator
Reaction score
964
Why would someone need a giant boolean array in a struct? :rolleyes:
 

SerraAvenger

Cuz I can
Reaction score
234
Hint: With operators like .fieldname= and []= it is possible to have a return value in the method, however this return value would almost always be impossible to get from outside the function, there is an exception, and it is when these methods return a value of the struct's type, then it will get translated to an assignment. For example, instead of call var_set(object,45), the result would be set object=var_set(object,45)

Should I really have read the manual before programming :D?

Quite clever to make it that way.
I would've used one additional integer... per instance :D (instead of using this)
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top