7 Replies Latest reply on Aug 19, 2009 8:45 PM by Preetham Hegde

    Efficient Looping

    flCoder74

      Is there a more efficient way to do this sort of looping within a loop? Dictionary??

       

      for (var i:int = 0; i < myAC.length; i++) {
                          if (myAC[i].cPosition > myAC[i].cCount) {
                              for (var ii:int = 0; ii < myAC.length; ii++) {
                                  if (myAC[ii].apID != myAC[i].apID && myAC[i].Group == myAC[ii].Group && myAC[ii].y > myAC[i].y) {
                                      myAC[i].cPosition = myAC[ii - 1].cPosition;
                                  }
                              }
                          }
                          for (var iii:int = 0; iii < myAC.length; iii++) {
                              if (myAC[iii].apID != myAC[i].apID && myAC[i].Group == myAC[iii].Group && myAC[i].cPosition == myAC[iii].cPosition && myAC[iii].y >= myAC[i].y && myAC[iii].y <= myAC[i].y) {
                                  myAC[iii].cPosition2 = myAC[i].cPosition + 1;
                              }   
                          }
                      }

        • 1. Re: Efficient Looping
          flCoder74 Level 1

          Any suggestions/thoughts on how to more efficiently do this type of looping within a loop?

           

          for (var i:int = 0; i < myAC.length; i++) {
               if (myAC[i].a > myAC[i].b) {
                    for (var ii:int = 0; ii < myAC.length; ii++) {
                         if (myAC[ii].c != myAC[i].c && myAC[i].d == myAC[ii].d) {
                              myAC[i].c = myAC[ii - 1].c;
                         }
                    }
               }
          }

          • 2. Re: Efficient Looping
            Preetham Hegde

            Does this work in all condition...

             

            If ii = 0 and i = 0,  and if below condition pass, it will throw exception..

             

            ..

            ...

            if (myAC[ii].c != myAC[i].c && myAC[i].d == myAC[ii].d) {
                                 myAC[i].c = myAC[ii - 1].c;
                            }

            ...

            ..

            • 3. Re: Efficient Looping
              flCoder74 Level 1

              Yes, this always works. What I'm really after is a better way to loop. Instead of a sub-loop through my ArrayCollection for every item in the ArrayCollection.

              • 4. Re: Efficient Looping
                Gregory Lafrance Level 6

                If this post answered your question or helped, please mark it as such.

                 

                If all items are unique in myAC:

                 

                <?xml version="1.0" encoding="utf-8"?>
                <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                  creationComplete="init();">
                  <mx:Script>
                    <![CDATA[
                      import mx.collections.ArrayCollection;
                     
                      [Bindable] private var myAC:ArrayCollection = new ArrayCollection([
                        {cPosition: 0, cCount: 0, apID: 1, Group: "Group One", y: 0},
                        {cPosition: 1, cCount: 1, apID: 2, Group: "Group Two", y: 10},
                        {cPosition: 2, cCount: 2, apID: 3, Group: "Group Three", y: 20},
                        {cPosition: 3, cCount: 3, apID: 4, Group: "Group Four", y: 30},
                        {cPosition: 0, cCount: 0, apID: 1, Group: "Group One", y: 0},
                        {cPosition: 1, cCount: 1, apID: 2, Group: "Group Two", y: 10},
                        {cPosition: 2, cCount: 2, apID: 3, Group: "Group Three", y: 20},
                        {cPosition: 3, cCount: 3, apID: 4, Group: "Group Four", y: 30}       
                      ]);

                      private function init():void{
                        for each(var obj1:Object in myAC) {
                          if (obj1.cPosition > obj1.cCount) {
                            for each(var obj2:Object in myAC) {
                              if (obj2.apID != obj1.apID && obj1.Group == obj2.Group && obj2.y > obj1.y) {
                                obj1.cPosition = myAC.getItemAt(myAC.getItemIndex(obj2) - 1).cPosition;
                              }
                            }
                          }
                          for each(var obj3:Object in myAC) {
                            if (obj3.apID != obj1.apID && obj1.Group == obj3.Group &&
                              obj1.cPosition == obj3.cPosition && obj3.y >= obj1.y && obj3.y <= obj1.y) {
                              obj3.cPosition2 = obj1.cPosition + 1;
                            }   
                          }
                        }
                      }     
                    ]]>
                  </mx:Script>
                </mx:Application>


                And if all items are not unique:

                 

                <?xml version="1.0" encoding="utf-8"?>
                <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                  creationComplete="init();">
                  <mx:Script>
                    <![CDATA[
                      import mx.collections.ArrayCollection;
                     
                      [Bindable] private var myAC:ArrayCollection = new ArrayCollection([
                        {cPosition: 0, cCount: 0, apID: 1, Group: "Group One", y: 0},
                        {cPosition: 1, cCount: 1, apID: 2, Group: "Group Two", y: 10},
                        {cPosition: 2, cCount: 2, apID: 3, Group: "Group Three", y: 20},
                        {cPosition: 3, cCount: 3, apID: 4, Group: "Group Four", y: 30},
                        {cPosition: 0, cCount: 0, apID: 1, Group: "Group One", y: 0},
                        {cPosition: 1, cCount: 1, apID: 2, Group: "Group Two", y: 10},
                        {cPosition: 2, cCount: 2, apID: 3, Group: "Group Three", y: 20},
                        {cPosition: 3, cCount: 3, apID: 4, Group: "Group Four", y: 30}       
                      ]);

                      private function init():void{
                        for each(var obj1:Object in myAC) {
                          if (obj1.cPosition > obj1.cCount) {
                            for (var ii:int = 0; ii < myAC.length; ii++) {
                              if (myAC[ii].apID != obj1.apID && obj1.Group == myAC[ii].Group && myAC[ii].y > obj1.y) {
                                obj1.cPosition = myAC[ii - 1].cPosition;
                              }
                            }
                          }
                          for each(var obj3:Object in myAC) {
                            if (obj3.apID != obj1.apID && obj1.Group == obj3.Group &&
                              obj1.cPosition == obj3.cPosition && obj3.y >= obj1.y && obj3.y <= obj1.y) {
                              obj3.cPosition2 = obj1.cPosition + 1;
                            }   
                          }
                        }
                      }     
                    ]]>
                  </mx:Script>
                </mx:Application>

                1 person found this helpful
                • 5. Re: Efficient Looping
                  flCoder74 Level 1

                  All items in the array are unique. Is there a performance difference between the "for each" and the "for" loop?:

                   

                          for each(var obj1:Object in myAC) {
                            if (obj1.cPosition > obj1.cCount) {
                              for each(var obj2:Object in myAC) {
                                if (obj2.apID != obj1.apID && obj1.Group == obj2.Group && obj2.y > obj1.y) {
                                  obj1.cPosition = myAC.getItemAt(myAC.getItemIndex(obj2) - 1).cPosition;
                                }

                   

                   

                  OR

                   

                  for (var i:int = 0; i < myAC.length; i++) {....

                  • 6. Re: Efficient Looping
                    Gregory Lafrance Level 6

                    Not sure, but seems like for each would be more efficient.

                    • 7. Re: Efficient Looping
                      Preetham Hegde Level 1
                      1 person found this helpful