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 Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top