Author |
Message
|
sandman147 |
Posted: Mon Feb 24, 2014 8:55 am Post subject: REFERENCE Instead of Index |
|
|
Apprentice
Joined: 01 Sep 2013 Posts: 42
|
Example
--------------------------------------------------------------------------
WHILE (iLoopCount<=iRecordCount) DO
SET OutputRoot.MRM.Result[iLoopCount].Data1 = InputRoot.MRM.Object[iLoopCount].Element1;
SET OutputRoot.MRM.Result[iLoopCount].Data2 = InputRoot.MRM.Object[iLoopCount].Element2;
SET OutputRoot.MRM.Result[iLoopCount].Data3 = InputRoot.MRM.Object[iLoopCount].Element3;
SET iLoopCount = iLoopCount + 1;
END WHILE;
---------------------------------------------------------------------------
How can the above be implemented with the help of REFERENCE pointers ? |
|
Back to top |
|
 |
sandman147 |
Posted: Mon Feb 24, 2014 9:22 am Post subject: |
|
|
Apprentice
Joined: 01 Sep 2013 Posts: 42
|
Does the Implementation go something like this;
--------------------------------------------------------------------------
DECLARE RefIn REFERENCE TO InputRoot.MRM.Object;
WHILE LASTMOVE(RefIn) DO
SET OutputRoot.MRM.Result[iLoopCount].Data1 = RefIn.Element1;
SET OutputRoot.MRM.Result[iLoopCount].Data2 = RefIn.Element2;
SET OutputRoot.MRM.Result[iLoopCount].Data3 = RefIn.Element3;
MOVE RefIn NEXTSIBLING NAME 'Object';
SET iLoopCount = iLoopCount + 1;
END WHILE;
---------------------------------------------------------------------------- |
|
Back to top |
|
 |
Vitor |
Posted: Mon Feb 24, 2014 9:22 am Post subject: Re: REFERENCE Instead of Index |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
sandman147 wrote: |
How can the above be implemented with the help of REFERENCE pointers ? |
CREATE LASTCHILD _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
Vitor |
Posted: Mon Feb 24, 2014 9:23 am Post subject: |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
sandman147 wrote: |
Does the Implementation go something like this;
--------------------------------------------------------------------------
DECLARE RefIn REFERENCE TO InputRoot.MRM.Object;
WHILE LASTMOVE(RefIn) DO
SET OutputRoot.MRM.Result[iLoopCount].Data1 = RefIn.Element1;
SET OutputRoot.MRM.Result[iLoopCount].Data2 = RefIn.Element2;
SET OutputRoot.MRM.Result[iLoopCount].Data3 = RefIn.Element3;
MOVE RefIn NEXTSIBLING NAME 'Object';
SET iLoopCount = iLoopCount + 1;
END WHILE;
---------------------------------------------------------------------------- |
 _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
Gralgrathor |
Posted: Mon Feb 24, 2014 10:49 am Post subject: |
|
|
Master
Joined: 23 Jul 2009 Posts: 297
|
I'd use FOR in stead of a WHILE loop, though. It seems to be somewhat faster than WHILE/MOVE. _________________ A measure of wheat for a penny, and three measures of barley for a penny; and see thou hurt not the oil and the wine. |
|
Back to top |
|
 |
Vitor |
Posted: Mon Feb 24, 2014 10:51 am Post subject: |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
Gralgrathor wrote: |
I'd use FOR in stead of a WHILE loop, though. It seems to be somewhat faster than WHILE/MOVE. |
In the specific case posted, I'd be inclined to do the same but for reasons of code clarity & simplicity. Not noticed the performance improvement you claim (which in no way reflectes on the existence or not of said performance improvement of course). _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
Gralgrathor |
Posted: Mon Feb 24, 2014 11:24 am Post subject: |
|
|
Master
Joined: 23 Jul 2009 Posts: 297
|
Vitor wrote: |
Not noticed the performance improvement you claim (which in no way reflectes on the existence or not of said performance improvement of course). |
You're right, I checked: the increase in performance in the code I rewrote likely resulted from replacing Array[Index].Field with RowRef.Field, not the loop structure per se. _________________ A measure of wheat for a penny, and three measures of barley for a penny; and see thou hurt not the oil and the wine. |
|
Back to top |
|
 |
sandman147 |
Posted: Mon Feb 24, 2014 12:24 pm Post subject: |
|
|
Apprentice
Joined: 01 Sep 2013 Posts: 42
|
How about this
---------------------------------------------------------------------------
CREATE LASTCHILD OF OutputRoot DOMAIN 'MRM';
CREATE LASTCHILD OF OutputRoot.MRM NAME 'Result';
DECLARE RefOut REFERENCE TO OutputRoot.MRM.Result;
DECLARE RefIn REFERENCE TO InputRoot.MRM.Object;
WHILE LASTMOVE(RefIn) DO
SET RefOut.Data1 = RefIn.Element1;
SET RefOut.Data2 = RefIn.Element2;
SET RefOut.Data3 = RefIn.Element3;
CREATE NEXTSIBLING OF RefOut AS RefOut REPEAT;
MOVE RefIn NEXTSIBLING NAME 'Object';
END WHILE;
------------------------------------------------------------------------------ |
|
Back to top |
|
 |
Vitor |
Posted: Mon Feb 24, 2014 1:13 pm Post subject: |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
sandman147 wrote: |
How about this
---------------------------------------------------------------------------
CREATE LASTCHILD OF OutputRoot DOMAIN 'MRM';
CREATE LASTCHILD OF OutputRoot.MRM NAME 'Result';
DECLARE RefOut REFERENCE TO OutputRoot.MRM.Result;
DECLARE RefIn REFERENCE TO InputRoot.MRM.Object;
WHILE LASTMOVE(RefIn) DO
SET RefOut.Data1 = RefIn.Element1;
SET RefOut.Data2 = RefIn.Element2;
SET RefOut.Data3 = RefIn.Element3;
CREATE NEXTSIBLING OF RefOut AS RefOut REPEAT;
MOVE RefIn NEXTSIBLING NAME 'Object';
END WHILE;
------------------------------------------------------------------------------ |
Does it work when you test it?
As I indicated above, I'd use CREATE LASTCHILD rather than CREATE NEXTSIBLING but at first glance I'd say they were functionally equivalent to your solution. _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
sandman147 |
Posted: Mon Feb 24, 2014 1:27 pm Post subject: |
|
|
Apprentice
Joined: 01 Sep 2013 Posts: 42
|
It works. I will implement these pointers which improve performance from this point forward. Thanks for all the help. |
|
Back to top |
|
 |
Vitor |
Posted: Mon Feb 24, 2014 1:33 pm Post subject: |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
Gralgrathor wrote: |
You're right, I checked: the increase in performance in the code I rewrote likely resulted from replacing Array[Index].Field with RowRef.Field, not the loop structure per se. |
Most likely. People are often surprised by how much this kind of index addressing costs. _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
mqsiuser |
Posted: Tue Feb 25, 2014 12:39 am Post subject: |
|
|
 Yatiri
Joined: 15 Apr 2008 Posts: 637 Location: Germany
|
sandman147 wrote: |
How about this
---------------------------------------------------------------------------
CREATE LASTCHILD OF OutputRoot DOMAIN 'MRM';
CREATE LASTCHILD OF OutputRoot.MRM NAME 'Result';
DECLARE RefOut REFERENCE TO OutputRoot.MRM.Result;
DECLARE RefIn REFERENCE TO InputRoot.MRM.Object;
WHILE LASTMOVE(RefIn) DO
SET RefOut.Data1 = RefIn.Element1;
SET RefOut.Data2 = RefIn.Element2;
SET RefOut.Data3 = RefIn.Element3;
CREATE NEXTSIBLING OF RefOut AS RefOut REPEAT;
MOVE RefIn NEXTSIBLING NAME 'Object';
END WHILE; |
That looks so good.
It is a bit verbose at the beginning to get your references (on the InputRoot and OutputRoot) in place (as can be seen here). At the end you move the input one and create an output one (if required ), good!
Don't hesitate to introduce more references on the part(s) that you need (again) later. And again, all this happens on the Input- and Output-Root!
And Vitor is right, the thing is... WHAT IF your resulting list is empty... you don't have to use CREATE LASTCHILD within the loop, but AT LEAST you need to add code that handles this (case). E.g. you may do it on the line where you "CREATE LASTCHILD OF OutputRoot.MRM NAME 'Result'. Put in a test on the InputRoot WHETHER a MOVE FIRSTCHILD works (yes: not empty, no: empty) and only create a FIRST OutChild, if not empty ! _________________ Just use REFERENCEs
Last edited by mqsiuser on Tue Feb 25, 2014 5:34 am; edited 3 times in total |
|
Back to top |
|
 |
Esa |
Posted: Tue Feb 25, 2014 12:50 am Post subject: |
|
|
 Grand Master
Joined: 22 May 2008 Posts: 1387 Location: Finland
|
But if you used CREATE LASTCHILD as suggested by Vitor you wouldn't need to remove the last, empty Result element afterwards... |
|
Back to top |
|
 |
mqsiuser |
Posted: Tue Feb 25, 2014 12:54 am Post subject: |
|
|
 Yatiri
Joined: 15 Apr 2008 Posts: 637 Location: Germany
|
Esa wrote: |
But if you used CREATE LASTCHILD as suggested by Vitor you wouldn't need to remove the last, empty Result element afterwards... |
yes, you need to improve on that code (especially on the creation of your out element(s)!)
Either you will see a SUPRISE (in production) OR you intensly think about your EDGE cases (no element, what is about the last element) _________________ Just use REFERENCEs |
|
Back to top |
|
 |
|