Questions about structs

NoobImbaPro

You can change this now in User CP.
Reaction score
60
Lets say I have a empty struct A for beggining
I call A.create() and do some actions inside (using methods)
1)What actually .create() method does? Does it create an empty ram space? Or does it use an already created space for that struct ?
2)I saw in some system using thistype(0) for some reason, what is that, the first ram space for that struct?
3)Let's say I use the created A stuct in "this" variable and then i set this = 1, what I've done ?
4)I saw systems using this.next/prev struct types. What is their purpose?
 

Sevion

The DIY Ninja
Reaction score
413
1. It simply returns the first unused struct instance number.
2. thistype(0) is equivalent to myArray[0]
3. You've effectively set this = thistype(1)
4. To have a doubly linked list.
 

Dirac

22710180
Reaction score
147
This works if none of the structs have been indexed yet
JASS:
method fun takes fun returns fun
set this=thistype.allocate()
//this is now equal to 1
set this=thistype.allocate()
//this is now equal to 2
set this=anothertype.allocate()
//this is now equal to 1
endmethod
 

NoobImbaPro

You can change this now in User CP.
Reaction score
60
"4. To have a doubly linked list."
This answer is too general :D

I mean what do they accomplish with that?

Also let's say I have this

JASS:
struct x[300]
endstruct


I initialize 300 array positions in a row like from thistype(0) to thistype(300)? or is it now extended to array?

OFF TOPIC: Sevion when I rep you and for what??
 

tooltiperror

Super Moderator
Reaction score
231
Imagine you have this.

610px-Doubly-linked-list.svg.png

The arrow pointing forward is a nodes "this.next" and the arrow pointing backwards is a nodes "this.prev". If you start from the left most node and move to the right using [ljass]this.next[/ljass], you can go through every node. This saves you some math from using an array, simplifies code, and is usually faster.
 

NoobImbaPro

You can change this now in User CP.
Reaction score
60
Yes I understand that from the names already. But what I should care when I use them ?
Should I set this.next.prev = this and this.prev.next = this, if my next struct is a null one, how can I set its previous to the one I have now?
And if I've got the first array how should I know that there no previous than this?
On destroy what should I take in mind? What does destroy actually do?
 

Dirac

22710180
Reaction score
147
I think you're confusing something, prev and next are NOT incorporated in vJass, they're set manually according to what you write.
JASS:
struct chain
thistype next
thistype prev
endstruct

this.prev.next= sets an instance as the next instance of the previous instance in the chain
this.next.prev= sets an instance as the previous instance of the next instance in the chain
this.next= sets an instance as the next instance of instance "this" in the chain
this.prev= sets an instance as the previous instance of instance "this" in the chain
 

Sevion

The DIY Ninja
Reaction score
413
"4. To have a doubly linked list."
This answer is too general :D

I mean what do they accomplish with that?

Also let's say I have this

JASS:
struct x[300]
endstruct


I initialize 300 array positions in a row like from thistype(0) to thistype(300)? or is it now extended to array?

OFF TOPIC: Sevion when I rep you and for what??

The question is too general >_> You asked what the members do. They allow a doubly linked list.

Array structs aren't declared like that FYI. They're defined as [ljass]struct myStruct extends array[/ljass] and then you declare them as [ljass]myStruct foo[/ljass] and use them like [ljass]foo[300][/ljass].

I don't know. There's no such listing in my control panel.

Yes I understand that from the names already. But what I should care when I use them ?
Should I set this.next.prev = this and this.prev.next = this, if my next struct is a null one, how can I set its previous to the one I have now?
And if I've got the first array how should I know that there no previous than this?
On destroy what should I take in mind? What does destroy actually do?

-->

The arrow pointing forward is a nodes "this.next" and the arrow pointing backwards is a nodes "this.prev". If you start from the left most node and move to the right using this.next, you can go through every node. This saves you some math from using an array, simplifies code, and is usually faster.
 

NoobImbaPro

You can change this now in User CP.
Reaction score
60
JassHelper Manual:
Code:
For some reason, the 8190 instances limit is not enough for us, we need 10000 instances ! so:

struct X[10000]
    integer a
    integer b
endstruct

Q)In debug mode I got sometimes messages like "Double free of type: A_Interface" what is not ok now?
Is one member of the interface's I extended to mus struct got prob?

"They allow a doubly linked list." I didn't understand what does this mean at the first time :p

"this.prev.next= sets a struct as the next struct of the previous struct in the chain
this.next.prev= sets a struct as the previous struct of the next struct in the chain"

I got what does it mean, I said if I use this.next.prev = this and this.next is non-allocated struct will it take effect?
 

Sevion

The DIY Ninja
Reaction score
413
I've never seen that method of declaration before. I've never needed more than 8190 instances.

It's because you're destroying some variable of type A_Interface twice.

If you [ljass]set this.next.prev = this[/ljass] when this.next is not allocated, it'll do nothing. It may crash the thread, methinks.
 

Ayanami

칼리
Reaction score
288
Just a bit more information about a doubly linked list.

Generally, a doubly linked list can be imagine as a circle, everything always comes back to the main node (it's called a head i believe). This "head" is [ljass]thistype(0)[/ljass]. When you create a struct instance, you assign an index to that struct instance. This instance starts from 1 and all the way to 8190 instances. [ljass]thistype(0)[/ljass] basically means the instance which has the index of 0, which isn't used. Thus, it can be used as the "head". So let's say you have an "empty" struct.

JASS:
struct MyStruct
    thistype next
    thistype prev

    private static method create takes nothing returns nothing
        local thistype this = thistype.allocate() // creates a new struct instance (index 1)
        set thistype(0).next.prev = this // this doesn't really do anything for now since your struct was initially "empty"
        set this.next = thistype(0).next = // this basically sets this instance's (1) next as the instance 0
        set thistype(0).next = this // sets the 0 instance's next to this instance (1)
        set this.prev = thistype(0) // sets this instance's (1) prev as instance 0

        // currently, your link list would look like:
        // 0 -> 1 -> 0
    endmethod
endstruct


Now let's say you create another instance.

JASS:
struct MyStruct
    thistype next
    thistype prev

    private static method create takes nothing returns nothing
        local thistype this = thistype.allocate() // creates a new struct instance (now index 2)
        set thistype(0).next.prev = this // thistype(0).next refers to instance 1, thus this is setting instance 1's prev to this instance (2)
        set this.next = thistype(0).next = // sets this instance's (2) next as instance 0's next (which is instance 1)
        set thistype(0).next = this // sets the 0 instance's next to this instance (2)
        set this.prev = thistype(0) // sets this instance's (2) prev as instance 0

        // now, your link list would look like:
        // 0 -> 2 -> 1 -> 0
    endmethod
endstruct


That's what it basically does. Then you also need to delink these instances when you destroy them.

JASS:
struct MyStruct
    thistype next
    thistype prev

    private method destroy takes nothing returns nothing
        // imagine you're destroying instance 2
        set this.next.prev = this.prev // sets this.next's (which is instance 1) prev as this.prev (which is instance 0)
        // this would link back instance 1's prev to instance 0
        set this.prev.next = this.next // sets this.prev's (which is instance 0) next as this.next (which is instance 1)
        // this would link back instance 0's next to instance 1

        // then your link will be back to:
        // 0 -> 1 -> 0
    endmethod
endstruct


That's basically how a doubly linked list work. You can use a doubly linked list to iterate through all instances:

JASS:
struct MyStruct
    thistype next
    thistype prev

    private static method iterate takes nothing returns nothing
        local thistype this = thistype(0)

        loop
            set this = this.next
            exitwhen this == 0
  
            // actions
        endloop
    endmethod
endstruct
 
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