• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JASS] Is this loop good, or can it be done better?

Status
Not open for further replies.
Level 31
Joined
Jul 10, 2007
Messages
6,306
It's a deep first post order loop for a tree. The tree does not point to root nodes, hence my crazy arrays =P.

JASS:
local boolean array d_1     //was one side looked inside of?
local $STRUCT$ array r_1  //root node of current node
local $STRUCT$ c_1         //current total depth
local $STRUCT$ n_1         //current node

set n_1 = $THIS$ //first initialize current node to this
set c_1 = 0         //and set depth to 0

if (n_1.in != 0) //if the tree isn't empty, loop
loop
    //go to lowest position
    loop
        //if can't go down anymore, exit
        exitwhen n_1.next == 0 and n_1.in == 0
        //go down because can't go right
        if (n_1.in == 0) then
            //if already went down, exit
            if (d_1[c_1]) then
                exitwhen true
            else
                //set went down to true
                set d_1[c_1] = true
                set c_1 = c_1 + 1    //increase depth
                set r_1[c_1] = n_1   //store current node as root of down node
                set n_1 = n_1.next   //go to down node
            endif
        //go right
        elseif (not d_1[c_1]) then
            set d_1[c_1] = true      //set went right to true
            set c_1 = c_1 + 1        //increase depth
            set r_1[c_1] = n_1       //store current node as root of right node
            set n_1 = n_1.in          //go to right node
        else
            set d_1[c_1] = false    //already went right, so go down
            set c_1 = c_1 + 1       //increase depth by 1
            set r_1[c_1] = n_1.next  //store the down node of current node as root of current node
            exitwhen true
        endif
    endloop
    
    //if the current node is actually a node and not a pointer, use it
    if (not n_1.pointer) then
        set $THIS$ = n_1
    endif
    
    //go up 1
    set d_1[c_1] = false    //first unflag
    set n_1 = r_1[c_1]      //go to root node
    set c_1 = c_1 - 1       //decrease depth by 1
    
    exitwhen n_1 == 0      //exit when the current node is null
endloop
endif

Tree
Code:
1	6
	7
	8
	9
	10

2	11
	12
	13
	14
	15

3	21
	22
	23
	24
	25

4	26
	27
	28
	29
	30

Output
Code:
10
9
8
7
6	1

15
14
13
12
11	2

20
19
18
17
16	3

25
24
23
22
21	4

30
29
28
27
26	5
 
Level 22
Joined
Sep 24, 2005
Messages
4,821
How come the output for the child nodes goes from bottom-up? Sorry just passing by, I have no knowledge about these things.
 
Level 18
Joined
Jan 21, 2006
Messages
2,552
I would say no, because JASS sucks. In addition I would support Bribe's comment as it is impossible to read your code accurately with names like d_1 and n_1. I would also like to ask what $STRUCT$ is and $THIS$ - is this a text-macro? Why not just include the definition...
 
Level 18
Joined
Jan 21, 2006
Messages
2,552
I wasn't under the impression you were posting pseudo-code. Your "pseudo-names" are more specific and explanatory than your "actual" variables. I think the confusion is fairly justified.
 
Status
Not open for further replies.
Top