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.
  • Monovertex Monovertex:
    How are you all? :D
    +1
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though

      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