lockstep alternative for StoppingPolicy.longest

Ali Çehreli acehreli at yahoo.com
Mon Sep 17 16:02:41 PDT 2012


On 09/17/2012 03:51 PM, Andrej Mitrovic wrote:
> On 9/18/12, Ali Çehreli<acehreli at yahoo.com>  wrote:
>> I think you actually want .shortest, no?
>
> No I want to continue iterating as long as one of the ranges is still
> not empty. I'm not doing just comparisons, once there's only one range
> that's not empty I have to do some special checks on its elements. In
> simple terms:
>
> void main()
> {
>      enum sentinel = -1;
>
>      // imagine 3rd element missing and lockstep replaces it with -1
>      int[] arr1 = [2, 4, -1];
>      int[] arr2 = [2, 4, 3];
>
>      bool state;
>      foreach (aa, bb; lockstep(arr1, arr2))
>      {
>          if (aa == sentinel)
>          {
>              if (aa % 2 == 0)
>              {
>                  state = true;
>                  break;
>              }
>          }
>          else
>          if (bb == sentinel)
>          {
>              if (bb % 2 == 0)
>              {
>                  state = true;
>                  break;
>              }
>          }
>          else
>          {
>              if (aa == bb)
>              {
>                  state = true;
>                  break;
>              }
>          }
>
>      }
> }
>
> That's not the algorithm I'm using and I'm not dealing with integers
> but that's just an example. If either range is empty I want to
> continue doing some special work on the range that isn't empty,
> otherwise I have to do work on both.

Then .longest with zip seems to be the way to go. zip() produces the 
.init value for the short range. Luckily, you are not dealing with 
int.init so perhaps your type's sentinel is distinguishable:

     foreach (aa, bb; zip(StoppingPolicy.longest, a, b))
     {
         if (aa == aa.init) {
         }
         // etc.

         writefln("%s and %s", aa, bb);
     }

Ali


More information about the Digitalmars-d-learn mailing list