Struct Issues

NoobImbaPro

You can change this now in User CP.
Reaction score
60
I have a struct like this

JASS:
struct MyData
    integer id
endstruct


and then I execute this function

JASS:
function OnInit takes nothing returns nothing
    local integer i = 0
    loop
        set i = i + 1
        set MyData.create().id = GetRandomInt(1, 12)
    exitwhen i == 12
    endloop
endfunction


and then I want to see for example what instance has id of 5 or how many nodes have the id 12
How do I a line search to all created nodes without creating an array variable of struct?
EDIT:
And how do I detect and instance which is not allocated?
 

luorax

Invasion in Duskwood
Reaction score
67
JASS:
struct MyData
    integer id
    static integer instanceCount
    static method searchForValue takes integer value returns integer
        local integer r=0
        local integer i=1
        loop
            exitwhen i>thistype.instanceCount
            if thistype(i).id==value then
                set r=r+1
            endif
            set i=i+1
        endloop
        return r
    endmethod
    static method create takes nothing returns thistype
        set thistype.instanceCount=thistype.instanceCount+1
        return thistype.allocate()
    endmethod
    static method onInit takes nothing returns nothing
        local integer i=0
        loop
            set i=i+1
            set thistype.create().id=GetRandomInt(1,12)
            exitwhen i==12
        endloop
    endmethod
endstruct
 

tooltiperror

Super Moderator
Reaction score
231
Use a linked list.

JASS:
struct Data
    thistype next
    thistype prev
    integer id

    method destroy takes nothing returns nothing
        set this.next.prev = this.prev
        set this.prev.next = this.next
        call this.deallocate()
    endmethod

    static method GetInstanceById takes integer search returns thistype
        local thistype this = thistype(0)

        loop
            exitwhen this.next == null
            set this = this.next

            if (this.id == search) then
                return this
            endif
        endloop

        return thistype(0)
    endmethod

    static method create takes integer int returns thistype
        local thistype this = thistype.allocate()

        set this.id = int
        set this.next = thistype(0).next
        set this.next.prev = this
        set thistype(0).next = this
        set this.prev = thistype(0)
 
        return this
    endmethod
endstruct

function InitTrig_init takes nothing returns nothing
    local Data array data[10]

    set data[0] = Data.create(GetRandomInt(0, 10))  // 4
    set data[1] = Data.create(GetRandomInt(0, 10))  // 8
    set data[2] = Data.create(GetRandomInt(0, 10))  // 5
    set data[3] = Data.create(GetRandomInt(0, 10))  // 9

    set data[4] = Data.GetInstanceById(5)
    call BJDebugMsg(R2S(Data.id))                   // "5"

    set data[4] = Data.GetInstanceById(10)
    call BJDebugMsg(R2S(Data.id))                   // "0"

    call data[1].destroy()
    set data[4] = Data.GetInstanceById(8)
    call BJDebugMsg(R2S(Data.id))                   // "0"
endfunction
 

NoobImbaPro

You can change this now in User CP.
Reaction score
60
@tooltiperror
yours is faster than luorax's?

EDIT: Oh, a non-allocated struct has the value null?
so I don't need an integer counter for instances based on luorax's snippet
 

tooltiperror

Super Moderator
Reaction score
231
For Christ's sake, why is always about speed? My map was #2 on battle.net and coded in GUI (with a few lines of Jass glue) and it wasn't noticable at all. Jass should be used for convenience (and some speed improvements) but unless you're doing something over 20 times a second, or making a large system that processes a lot, speed doesn't matter.

And mine may be faster, I don't know, they should be about the same. The advantage of mine is that you can delete and add elements with O(1) complexity. You can also cache with mine, while that is much more complicated on luorax's. His also fails with a script like this I believe:
JASS:
local thistype poop
call thistype.create()        // instance: 1, count: 1
call thistype.create()        // instance: 2, count: 2
set poop = thistype.create()  // instance: 3, count: 3
call thistype.create()        // instance: 4, count: 4
call poop.destroy()           // count: 3

[del]Now it's not gonna search all the way to #4[/del] I stand corrected, it will just keep increasing the count.
 

NoobImbaPro

You can change this now in User CP.
Reaction score
60
I don't think so. The destroy function will send instance 4 to instance 3, and also counter will go this way.

Thank you tooltiperror and luorax for your time. I got what I wanted to know.
 

luorax

Invasion in Duskwood
Reaction score
67
His also fails with a script like this I believe:
Oh, that's true. I most likely use that maxCount thing for structs whose instances never get destroyed (e.g. hero data, items, recipes, similar things). Using a linked list is clearly a better solution if you also destroy your struct instances.
 

tooltiperror

Super Moderator
Reaction score
231
Oh, that's true. I most likely use that maxCount thing for structs whose instances never get destroyed (e.g. hero data, items, recipes, similar things). Using a linked list is clearly a better solution if you also destroy your struct instances.
Yeah, but reading an array is supposedly faster than adding anyways.
 

Dirac

22710180
Reaction score
147
Oh, a non-allocated struct has the value null?
No, deallocating a struct just marks it for recycling, it dosn't clear any vars inside it, i think you still have some problems on understanding how a struct works. Structs are like array systems, it saves values inside of them using proper integers which are later recycled.
Read this library >Alloc<. It shows how the deallocate and allocate methods works.

Also if you only wish to count how many nodes have X number from a random seed do this.
JASS:
library Tester
    globals
        private integer array c
    endglobals
    function Go takes nothing returns nothing
        local integer i=0
        local integer n
        loop
            exitwhen i==12
            set n=GetRandomInt(1,12)
            set c[n]=c[n]+1
            set i=i+1
        endloop
    endfunction
endlibrary
You don't even need a struct

EDIT: forgot to mention something important
Variables inside structs are actually arrays, which index is set to their prefix ([ljass]thistype(3).value==value[3][/ljass]) and different from non-array members their initial value is always 0, they don't have to be initialized.
 

luorax

Invasion in Duskwood
Reaction score
67
Nice, stupid variable and function names again. I'm glad to see that the Nestharus-virus is spreading.
 

Dirac

22710180
Reaction score
147
That's only 4 lines of code, i'm sure you're able to read it. That little script is nothing compared to some of nestharu's codes
I do am sorry because i didn't explain how it works:

The array c keeps track of how many random numbers where taken position, Ex:

c[4] returns 1, becuase it was only set once, but c[7] returns 3, becuase there are 3 number 7 on that list.
 

Sgqvur

FullOfUltimateTruthsAndEt ernalPrinciples, i.e shi
Reaction score
62
tooltiperror: My map was #2 on battle.net

Which map was that? (name)
 

NoobImbaPro

You can change this now in User CP.
Reaction score
60
All I wanted to know is how to search inside nodes, I just told a simple example to see an applied code rather than a theoretical one.

It's because I am updating my system "AUMS" and I have already double linked list. So I tried to make the nodes stack to be for a unit only.
Like this.prev returns the previous struct created chronologically of the same unit.
So I needed a line search, for the beginning, to find if the unit has already an instance applied to it, so to put this node as the next of the already created one.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • 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
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      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