arrays elements in strange order

fsk's picture

im trying to finish my custom patch but i stumbled on another problem. this time its a bit tougher (at least for me) because i have no clue where the problem comes from. here is the code from the execute:withTime:withArguments method:

   NSUInteger i;
   NSUInteger j;
   NSUInteger connectedCount;
   NSUInteger end=(NSUInteger)self.numOfInputs;
   NSMutableArray *inputs=[NSMutableArray arrayWithCapacity:self.numOfInputs];
   NSUInteger maxCount=0;
   k=0;
   for(i=0;i<end;i++)
   {
      //generate the key string and copy the array to avoid accesing the inputs more than once
      NSString* index=[[NSNumber numberWithUnsignedInteger:i+1] stringValue];
      NSString* name=@"Structure ";
      name=[name stringByAppendingString:index];

      NSArray *array=[NSArray arrayWithArray: [self valueForInputKey:name]];
      if([array count]>0)
      {
         [inputs addObject: array];
         connectedCount++;
      }
      maxCount=fmax(maxCount,[array count]);
   }
   if(maxCount>0)
   {
      NSMutableArray *output=[NSMutableArray arrayWithCapacity: maxCount];

      for(j=0;j<maxCount;j++)
      {
         NSMutableArray *column=[NSMutableArray arrayWithCapacity:connectedCount];
           for(i=0;i<connectedCount;i++)
         {
            [column addObject: [ [inputs objectAtIndex:i] objectAtIndex: j % [ [inputs objectAtIndex:i] count] ] ];
         }
         [output addObject: column];
      }

      self.outputArray = output;
   }
   return YES;

its supposed to merge two or more arrays to one two dimensional array. it seems to do what its supposed to but the order of elements in the output array in QC is messed up. if i give it two structures:

a1 a2 a3

b1 b2 b3

i would expect it to give me:

a1 a2 a3
b1 b2 b3

but it gives me:

a3 a1 a2   
b3 b1 b2

when i look at my code i don't see anything that would cause this. because it preserves the order in the y axis i can presume that i either get the structures mixed up at the start or that the arrayWithArray does it. any ideas?

cwright's picture
structure mix

That's QC's Structure type mixing things up (you can also see this with structures generated in JS) -- They're stored as hash tables (dictionaries), not linear arrays, so data isn't necessarily kept in-order.

Not sure how to deal with that in the official API... I fight with it even in the unofficial one. I'm sure there's some way to work with it though.

fsk's picture
thanx for clearing that up.

thanx for clearing that up. at least i know its not my fault:). does this happen on input or output or at both ends?

fsk's picture
before doing a version with

before doing a version with dynamic inputs i made one with a fixed number of inputs. i just noticed that that version doesn't mix things up. the only important difference i can spot is this part in the header:

@property(assign) NSArray* inputArray1; @property(assign) NSArray* inputArray2; @property(assign) NSArray* inputArray3;

where i define the properties as NSArrays instead of dictionaries. i have no idea how to do this with dynamic inputs because the method for creating an input has only one type for structure inputs and that seems to always make dictionaries :/.

cwright's picture
just output (?)

I think this is just on output -- on input, they're pulled out in dictionary order (mixed), but the order-mix happens when objects are inserted.