Moar questions about 'this'

SineCosine

I'm still looking for my Tangent
Reaction score
77
I'm back, went to catch some Zs >.>
Mmm..
So..
.typeid and GetType().. return that constant integer..
But what does constant integer of an interface mean? o.0

What's an interface? =x

Internally the [normal] members are arrays in which the index is the struct integer.

So..
[lJASS]unit Caster[/lJASS] <-- That would be [lJASS]unit array Caster[/lJASS] <--- ??
 

Bribe

vJass errors are legion
Reaction score
67
I talked about this matter in another thread of yours.

Me said:
static members are prefixed to the struct name, so unless you want to protect that member in the event someone extends your struct, there is absolutely no reason to privatize it.

Static means that the unit will not be an array (unless you specify "static unit array" or something). Dynamic members (ones that just say "unit someUnit") instantly become arrays and are initialized in the hidden "allocate" function to whatever value you have it set to.

JASS:
struct s
    static unit blah = null //is not an array
    unit dude = null //is an array that the instance-specified slot is set to &quot;null&quot; on each allocation
endstruct
 

SineCosine

I'm still looking for my Tangent
Reaction score
77
So..
If I do use static unit..
It will be like a global for use between all structs? o.0
 

tooltiperror

Super Moderator
Reaction score
231
You could think of it like this.

JASS:

//! External Comment
 library Example initializer onInit
  struct Human
     static integer year
     integer age
  endstruct
  private function onInit takes nothing returns nothing
      local Human Bill=Human.create()
      local Human Jeff=Human.create()
      // I forget, is it Human.year or Bill.year?
      set Bill.year=2010
      call BJDebugMsg(I2S(Bill.year))
      call BJDebugMsg(I2S(Jeff.year))
      // Both display 2010.
 endlibrary
 

tooltiperror

Super Moderator
Reaction score
231
Pretty much. You could also use it to keep a count, I suppose.

JASS:

//! External Comment
 library Example initializer onInit
  struct Human
     static integer year
     integer population
     private static method create takes nothing returns thistype
         local thistype this=thistype.create()
         set this.population=this.population+1
         return this
     endmethod
     private static method onDestroy takes nothing returns nothing
         set this.population=this.population-1
     endethod
  endstruct
 endlibrary
 

SineCosine

I'm still looking for my Tangent
Reaction score
77
I see..
So..

How do linked lists work and what are its general applications? =x
(Yeap, the whole point of the thread was for me to inch closer to this XD)

Why do I see .next and .prev so often? >.>
 

tooltiperror

Super Moderator
Reaction score
231
Not sure, I've got no idea what linked lists are like in JASS.
 

PurgeandFire

zxcvmkgdfg
Reaction score
509
Linked lists are basically lists of structs in which you'll know the current "this" instance, "this.prev" which is the previous instance of "this" and "this.next", which is the next instance. For each instance, you'll know its previous and next instance.

Take this list of integers:
3, 6, 8, 9
Now you want to insert a "7" between 6 and 8.
3, 6, 7, 8, 9
What you must do, is make 6 think its next instance is "7" and 8 think its previous instance is "7".
When you want to remove 7, you'll have to do the opposite, kind of. You have to make 6 think its next instance is "8" (aka: 7's next instance) and make 8 think its previous instance is "6" (aka: 7's previous instance) That removes it from the chain.
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
Linked lists are basically lists of structs in which you'll know the current "this" instance, "this.prev" which is the previous instance of "this" and "this.next", which is the next instance. For each instance, you'll know its previous and next instance.
No, you can use it to do a list of units, triggers, items and many stuffs, not limited to structs only. :)

For Linked List, you won't have to use it until you reach certain situation like flexible array(Just like the classical spell, Meat Hook), but it's speed is not as fast as array read because it needs some O(n) searching, not like O(1) in array.
You must take extra care of Linked List when you use it.
You will be doomed if you linked those head and node wrongly.
 

tooltiperror

Super Moderator
Reaction score
231
Oh, so, that is how one achieves O(n)? Or is O(1) the faster one? Which one doesn't loop?
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
Oh, so, that is how one achieves O(n)? Or is O(1) the faster one? Which one doesn't loop?

O(n) complexity means that the number of iterations something takes to complete is directly dependent on the size of the data. O(1) means that the number of iterations something takes to complete is always going to be constant, no matter what value is used.

Linked Lists is one type of Data Structure. Removing any specific item from the list is O(1). Searching is O(n). Inserting is O(1). Appending is O(n), However, a drawback is that to get a value by index, it is O(n). They are useful as a Queue, where you can simply get the 'head' of the list and remove it.

Arrays are another type of Data structure. Removing any specific item (while maintaining the order of the list) is O(n), as each node would have to be moved left one. Searching is O(n). Inserting is O(n). Appending is O(1). However, getting a value by index is O(1). They are useful as a stack, as you can append and remove from the end with O(1) complexity.
 

SineCosine

I'm still looking for my Tangent
Reaction score
77
Thanks, guys =D
But..
How will I make the struct 'know' which came before and which will come after? o.0

Or is it impossible and the only way is to just define this, next and prev ourselves ._.
We could figure out which should come next if we can access that variable struct__A_F and struct__A_I.. right?
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
So we have previous and next.
350px-Circularly-linked-list.svg.png

12's next -> 99
99's next -> 37
37's next -> 12

12's prev -> 37
37's prev -> 99
99's prev -> 12

There are many types of linked lists, from simplest Linear to harder like Singly, Doubly, Multiply even mad 2d-doubly (Jesus4Lyf did it long time ago, probably at last year.)
 

SineCosine

I'm still looking for my Tangent
Reaction score
77
Okay..

So..
If I create one struct only..
I wouldn't be able to link it, because there isn't a 2nd or 3rd struct.. right?

So..
I create the 1st struct..
Then I create a 2nd struct..
How would--

Wait--

Do we store next and prev and statics? o.0
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
An example would help.
JASS:
        private static method PeriodicLoop takes nothing returns boolean
            local thistype this=thistype(0).next
            loop
                exitwhen this==0
                call this.periodic()
                set this=this.next
            endloop
            return false
        endmethod

        method startPeriodic takes nothing returns nothing
            set thistype(0).next.prev=this
            set this.next=thistype(0).next
            set thistype(0).next=this
            set this.prev=thistype(0)
        endmethod
        
        method stopPeriodic takes nothing returns nothing
            set this.prev.next=this.next
            set this.next.prev=this.prev
        endmethod
// 0 is the head of the linked list.
 

SineCosine

I'm still looking for my Tangent
Reaction score
77
You've lost me ._.
We call PeriodicLoop first, right?
Then..

What's thistype(0)?
Is it struct instance 0?

What's the initial value of .next and .prev ._.
 

PurgeandFire

zxcvmkgdfg
Reaction score
509
[ljass]thistype(0)[/ljass] is the 0 struct, so yeah it is the 0 instance. Basically, the null struct that was mentioned before.

JASS:
        method startPeriodic takes nothing returns nothing
            set thistype(0).next.prev=this
            set this.next=thistype(0).next
            set thistype(0).next=this
            set this.prev=thistype(0)
        endmethod


This is starting it off. What we want to do is insert the current instance into the chain. Since this is a "normal" method [non-static], it is associated with an instance, "this". That means that we can refer to the current instance using "this".
  • set thistype(0).next.prev=this - Okay, this will simply set the next instance's previous to the current instance. So we have this:
    <PREV> <NEXT>
    Now we want to insert "this" instance. So first we make next think its previous is the current instance.
    <PREV> <NEXTPREV: this><NEXT>
    However, the current instance doesn't know its next instance is "<NEXT>". Let's fix that.
  • set this.next=thistype(0).next - Okay, now we set the current instance's next to the next. Okay, now we've officially made it so that the current instance considers its next as "<NEXT>" and the <NEXT> considers its previous instance as the current "this". Now we need to make the standard "next" instance as the current instance.
  • set thistype(0).next=this - Now we have "this.next" as the next instance. Now if we want to insert anything else, we'll insert it right before "this". So now we can make "this" the next instance. That creates a sort of "chain". As they keep getting pushed over, the end of the chain will be this.next.next.next.next... etc... Until something is removed from the chain. However, now we need to let the current instance know what its previous instance is.
  • set this.prev=thistype(0) - Now it sets this' current instance to the head of the list. So basically:
    <HEAD> <this> <NEXT>
    That is what it looks like now. "this" is now the head's next. This' next is now <NEXT>. Next's previous is <this>. <Head> is where it starts, so it only has a next, no previous. Now think of a standard chain. As we keep inserting, each instance will be pushed to the right.
    <HEAD> <this> <NEXT> <NEXT> <NEXT>
    It'll be a chain shifting. Now that we can insert it, we must utilize it.

JASS:
private static method PeriodicLoop takes nothing returns boolean
    local thistype this=thistype(0).next
    loop
        exitwhen this==0
        call this.periodic()
        set this=this.next
    endloop
    return false
endmethod


Okay, now we will loop through the chain to execute the functions. So now, we declare "this" as thistype(0).next. Consider this chain:
<HEAD> <this> <NEXT> <NEXT> <NEXT>
So [ljass]thistype(0).next[/ljass] will be the first instance, marked as "this". Now we loop. We will loop through each instance, and [ljass]exitwhen this==0[/ljass], or when it is generally a struct instance that isn't in the chain. Ignore the [ljass]call this.periodic()[/ljass], that is part of T32. It is a module so it will just be implemented and call that periodic function. Okay, so now we will move on to get the next instance.
[ljass]set this=this.next[/ljass]
This will move on to the next instance in the chain. Now we will execute the function using that instance. Hopefully, you can see that we are looping through the chain and executing the function for each instance. Now remember, each [normal] method is associated with an instance. So when we are calling it like this:
JASS:
call this.periodic()

We are associating "this" with that method. So now we can use "this" freely inside the method:
JASS:
private method periodic takes nothing returns nothing
    //note that this is a non-static method//
    set this.x = 5 //you can freely use this now//
endmethod


Okay, so now we know how to insert and execute. But what if we don't want an instance to be used anymore?? Well, now we need to remove that link from the chain.

Consider this chain:
<HEAD> <NEXT1> <this> <NEXT2>
Say we want to remove "this" from the link.
  • set this.prev.next=this.next - This is where it gets fun. The easiest way to think of it is as it is read. Okay, so we have "this". It's previous is <NEXT1>. It's next is <NEXT2>. We are going to modify "this.prev.next". First, consider what the current value is.
    This's previous is <NEXT1>. However, the ".next" is added. That means This's previous is <NEXT1> and <NEXT1>'s next instance is "this". What we want to do to remove "this" from the chain is to make <NEXT1> think its next instance is <NEXT2>, and <NEXT2> think its previous instance is <NEXT1>.

    set this.prev.next=this.next. What this does is it makes this' previous think its next instance is this' next. Essentially:
    <NEXT1 "Hey, my next is NEXT2."> <this> <NEXT2 "Hey, me previous is still 'this'.">
  • set this.next.prev=this.prev - Now NEXT2 is confused and is contemplating what is going on. Let's help him, rather than make him go to therapy. Now, this' next is <NEXT2>. Its previous is "this". By setting the next instance's previous to "this'" previous, it essentially completes the removal.
    <NEXT1> <NEXT2>
    You just saved NEXT2 from going to therapy.

Hopefully that'll clear some things up a bit. That code kingking posted is an excerpt from T32. Essentially, it is doing an insert, loop to execute for that instance, and then a removal of the instance. Nice and easy. :D
 

SineCosine

I'm still looking for my Tangent
Reaction score
77
Umm..
Yea..

I got stuck at the very first bit.
So..

01) set thistype(0).next.prev=this
Instance 0's next instance's previous instance is this instance.
So.. we're making instance 0's next instance 'think' it's previous instance is this.

<INSTANCE 0> <NEXT PREV = THIS> <NEXT>

02) set this.next=thistype(0).next
This instance's next instance is instance 0's next instance.
So.. we're making this instance think it's next instance is instance 0's next instance

This:<INSTANCE 0> <INSTANCE 0's NEXT = NEXT> <NEXT PREV = THIS> <NEXT>
Becomes This:<INSTANCE 0> <NEXT PREV = THIS> <INSTANCE 0's NEXT = NEXT>

03) set thistype(0).next=this
Instance 0's next instance is this instance...
Uhh..
So.. We're making instance 0's next instance 'think' it is the current instance? o.0

This: <INSTANCE 0> <NEXT PREV = THIS> <INSTANCE 0's NEXT = NEXT = THIS>
Becomes This: <INSTANCE 0> <NEXT PREV = THIS> <INSTANCE 0's NEXT = NEXT = (NEW)THIS>

04) set this.prev=thistype(0)
So..
We now have 2 'this'.. Don't we?
NEXT PREV is still 'this'..
And NEXT thinks it is this..

So.. Uhh..
I'm lost =x
I must have screwed up some where..
 
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