HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Able to get the struct the method runs in?

04-07-2008, 08:10 PM#1
Gwypaas
I tried creating a simple linked list but I got a problem. I'm using this code:
Collapse JASS:
library LinkedList
struct NODE
    private NODE previous
    private NODE next
    private integer data
    
    method Next takes nothing returns NODE
        set .next.previous = ThisStruct //Problem
        return this.next
    endmethod
    
    method Previous takes nothing returns NODE
        set .previous.next = ThisStruct //Problem
        return .previous
    endmethod
endstruct
endlibrary

But I have a problem is there any way I could get the ID of the struct I am currently are using the methods in?
04-07-2008, 08:26 PM#2
Fireeye
Structs are acutally just integers, so you can get with integer(<your struct>) the integer value [id] for each struct.
so you can use this.
Collapse JASS:
method GetId takes nothing returns integer
    return integer(this)
endmethod
or this
Collapse JASS:
    local integer i = integer(this)
04-07-2008, 08:32 PM#3
Gwypaas
I knew that structs are integers that's why the data part is a intger because it would contain a struct. And I'm actully asking for a way to get the ID and the ID is a integer anyways :P

So......

If I have this piece of code:
Collapse JASS:
library LinkedList
struct NODE
    private NODE previous
    private NODE next
    private integer data
    
    method Next takes nothing returns NODE
        set .next.previous = ThisStruct
        return this.next
    endmethod
    
    method Previous takes nothing returns NODE
        set .previous.next = ThisStruct
        return .previous
    endmethod
endstruct
endlibrary

Then I would want to get the ID of the highlighted part at the top and then take that ID and use in the other 2 highlighted parts further down.


Edit: Oh you edited your post while I was writing mine xD. Thanks now I understand what the "this" keyword does atleast. I think I've been reading about something similar in my C++ book anyways :P And I think it works but I don't have enough time to try it out, atleast it compiled correctly. :)
04-07-2008, 08:40 PM#4
Fireeye
let me try to figure out if i got it correct.
Some Pseudo example:
[1] <=> [3] <=> [2] <=> [5] ...
When you use Next on the hypotitcal Struct [1] you want the id of it and use it, to set the value of previous in Struct [3] to the value Struct [1], right?
Then you just use 'set .next.previous = this', or did i miss something?
04-07-2008, 10:11 PM#5
Ammorth
.something can replace this.something

you could should just be
Collapse JASS:
method next takes nothing returns NODE
    return .next
endmethod
method prev takes nothing returns NODE
    return .prev
endmethod

the .next.prev is redundant as you should already have those values allocated when you create each node.

Collapse JASS:
method createEX takes NODE n returns NODE
    local NODE o = NODE.allocate()
    set o.prev = n
    set n.next = o
    return o
endmethod

so to create a list of 20 nodes, you would do:
Collapse JASS:
function CreateList takes nothing returns nothing
    integer i = 0
    NODE n = NODE.create() // generic node
    loop
        exitwhen i > 19
        set n = NODE.createEX(n)
        set i = i + 1
    endloop
endfunction
04-08-2008, 11:21 AM#6
Strilanc
This was already mentioned in one of the posts, but you seemed to still be confused.

The 'current' structure is called "this". If you look at the generated code, "this" is an integer argument passed to all the non-static methods.
04-10-2008, 01:58 AM#7
PandaMine
This just refers to the current instance of a struct, a struct is just an integer index that points to a bunch of arrays, a method is just a function that only alters a data for one of the instances of that struct

Collapse JASS:
method next takes nothing returns NODE
    return .next
endmethod
method prev takes nothing returns NODE
    return .prev
endmethod

In my opinion would confuse/obfuscate code more then help anyone or anything. If you just want a method to return its own struct, you already have the struct that is calling the method, wtf is the point. You have to call a method on a struct, so you already have the struct
04-10-2008, 02:24 AM#8
Strilanc
Quote:
In my opinion would confuse/obfuscate code more then help anyone or anything. If you just want a method to return its own struct, you already have the struct that is calling the method, wtf is the point. You have to call a method on a struct, so you already have the struct

Some people like to hide everything behind functions so they can change the implementation without changing the interface.

But, seriously, this is a LINKED LIST. The implementation is NOT going to change! Listen to PandaMine.
04-11-2008, 02:56 AM#9
PandaMine
You can just make your own struct stack and use a loop if you want to go through all instances of a struct