www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Can't incremen int inside funciton call -- bug?

reply Aldarris <aldarri_s yahoo.com> writes:
Hello.

I have a nested function that iterates through a tree recursively.
here is it code:

void recursiveXMLTreeIteration(XMLNode currentNode, int treeLevel)
{
	writefln(treeLevel);
	tabOffset.length = 0;
	for(int i = 0; i < treeLevel; i++)
		{tabOffset ~= "\t";}
.......			
	foreach(XMLNode childNode; currentNode.getChildrenList())
		{recursiveXMLTreeIteration(childNode, treeLevel++);}
}

recursiveXMLTreeIteration(this, 0);

Does not work as expected, in a tree of a root node plus two child nodes it
prints 0,0,1.

It treeLevel++; is moved outside, works as expected:
.......			
        treeLevel++;
	foreach(XMLNode childNode; currentNode.getChildrenList())
		{recursiveXMLTreeIteration(childNode, treeLevel)
}


Prints 0,1,1,
May 29 2007
next sibling parent reply Ary Manzana <ary esperanto.org.ar> writes:
Hi.

Shouldn't that be ++treeLevel?

In general var++ is an expression that returns var and leaves var with 
the value var + 1. ++var is an expression that returns var + 1 and 
leaves var with var + 1.

int x = 1;
int y = x++; // leaves y = 1, x = 2

int x = 1;
int y = ++x; // leaves y = 2, x = 2

Further, your two codes are differentes. In the first one you increment 
treeLevel in each foreach iteration, while on the second you increment 
it once before the foreach loop.

Best regards,
Ary

Aldarris escribió:
 Hello.
 
 I have a nested function that iterates through a tree recursively.
 here is it code:
 
 void recursiveXMLTreeIteration(XMLNode currentNode, int treeLevel)
 {
 	writefln(treeLevel);
 	tabOffset.length = 0;
 	for(int i = 0; i < treeLevel; i++)
 		{tabOffset ~= "\t";}
 .......			
 	foreach(XMLNode childNode; currentNode.getChildrenList())
 		{recursiveXMLTreeIteration(childNode, treeLevel++);}
 }
 
 recursiveXMLTreeIteration(this, 0);
 
 Does not work as expected, in a tree of a root node plus two child nodes it
prints 0,0,1.
 
 It treeLevel++; is moved outside, works as expected:
 .......			
         treeLevel++;
 	foreach(XMLNode childNode; currentNode.getChildrenList())
 		{recursiveXMLTreeIteration(childNode, treeLevel)
 }
 
 
 Prints 0,1,1,

May 29 2007
parent reply Aldarris <aldarri_s yahoo.com> writes:
Ary Manzana Wrote:

 Hi.
 
 Shouldn't that be ++treeLevel?
 
 In general var++ is an expression that returns var and leaves var with 
 the value var + 1. ++var is an expression that returns var + 1 and 
 leaves var with var + 1.

Thanks. Should have studied theory better.
 
 int x = 1;
 int y = x++; // leaves y = 1, x = 2
 
 int x = 1;
 int y = ++x; // leaves y = 2, x = 2
 
 Further, your two codes are differentes. In the first one you increment 
 treeLevel in each foreach iteration, while on the second you increment 
 it once before the foreach loop.

Yes, that was the idea -- to send to the function not treeLevel, but a number higher than treeLevel by one and thus leaving treeLevel unchanged to call the function again from the same node. Incrementing treeLevel elsewhere gives the desired result. I have just found that the following: {recursiveXMLTreeIteration(childNode, (treeLevel + 1));} gives the desired result.
 Best regards,
 Ary
 
 Aldarris escribió:
 Hello.
 
 I have a nested function that iterates through a tree recursively.
 here is it code:
 
 void recursiveXMLTreeIteration(XMLNode currentNode, int treeLevel)
 {
 	writefln(treeLevel);
 	tabOffset.length = 0;
 	for(int i = 0; i < treeLevel; i++)
 		{tabOffset ~= "\t";}
 .......			
 	foreach(XMLNode childNode; currentNode.getChildrenList())
 		{recursiveXMLTreeIteration(childNode, treeLevel++);}
 }
 
 recursiveXMLTreeIteration(this, 0);
 
 Does not work as expected, in a tree of a root node plus two child nodes it
prints 0,0,1.
 
 It treeLevel++; is moved outside, works as expected:
 .......			
         treeLevel++;
 	foreach(XMLNode childNode; currentNode.getChildrenList())
 		{recursiveXMLTreeIteration(childNode, treeLevel)
 }
 
 
 Prints 0,1,1,


May 29 2007
parent Regan Heath <regan netmail.co.nz> writes:
Aldarris Wrote:
 I have just found that the following:
 {recursiveXMLTreeIteration(childNode, (treeLevel + 1));}
 gives the desired result.

The () around treeLevel + 1 are unnecessary - unless you like the look or think it clearer to read. The compiler will always completely evaluate the argument before passing it to the function. The only time you need braces is when using operators of differing precedence where you want the lower (higher? not sure which term is used) precedence operator to apply first, eg. {recursiveXMLTreeIteration(childNode, treeLevel + 1 * 2);} the * is evaluated first in the above, whereas with: {recursiveXMLTreeIteration(childNode, (treeLevel + 1) * 2);} the + is evaluated first. You probably know all that already but someone else might be interested :) Regan
May 29 2007
prev sibling parent Daniel Keep <daniel.keep.lists gmail.com> writes:
Aldarris wrote:
 Hello.
 
 I have a nested function that iterates through a tree recursively.
 here is it code:
 
 void recursiveXMLTreeIteration(XMLNode currentNode, int treeLevel)
 {
 	writefln(treeLevel);
 	tabOffset.length = 0;
 	for(int i = 0; i < treeLevel; i++)
 		{tabOffset ~= "\t";}
 ........			
 	foreach(XMLNode childNode; currentNode.getChildrenList())
 		{recursiveXMLTreeIteration(childNode, treeLevel++);}
 }
 
 recursiveXMLTreeIteration(this, 0);
 
 Does not work as expected, in a tree of a root node plus two child nodes it
prints 0,0,1.
 
 It treeLevel++; is moved outside, works as expected:
 ........			
         treeLevel++;
 	foreach(XMLNode childNode; currentNode.getChildrenList())
 		{recursiveXMLTreeIteration(childNode, treeLevel)
 }
 
 
 Prints 0,1,1,

It's a bug alright: in your code. treeLevel++ is post increment. It evaluates "treeLevel" and THEN increments it. You want pre increment: "++treeLevel". -- Daniel -- int getRandomNumber() { return 4; // chosen by fair dice roll. // guaranteed to be random. } http://xkcd.com/ v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
May 29 2007