6

I know that this has been already discussed, and there are multiple answers to it. See Performance of array of functions over if and switch statements for instance, however I would like to get some other ideas.

I've got a function with a large switch statement. This is 26 case and each with a "left" or "right" option. This function returns a pointer based on two given parameters (plane and direction):

double* getPointer(int plane, int direction) {
  switch (plane)
  {
  case 0:
    if (direction == 0)
      return  p_YZ_L; // Left
    else if (dir == 1)
      return p_YZ_R;  //Right
    else {
      return 0;
    }

    break;
    ...
  case 25:
    ...
  }
}

with

planes -> [0-25]
direction -> [0,1] 

I have been thinking on an array of functions, but this could also be tedious and I am not sure if it is the best option. Also it is not clear to me how to do it properly. Any ideas?

dbush
  • 205,898
  • 23
  • 218
  • 273
Manolete
  • 3,431
  • 7
  • 54
  • 92
  • 3
    Please show the code containing the`switch` statements you are talking about. – Jabberwocky Nov 04 '16 at 15:49
  • Two functions with 26 switch cases may be a good alternative, assuming that right and left sub-cases do not share much code. – Sergey Kalinichenko Nov 04 '16 at 15:50
  • 5
    How about array `double* x[2][26]` and `return x[direction][plane]`? – Kerrek SB Nov 04 '16 at 15:55
  • @MichaelWalz This shows only one large `switch` with `if else` within each `case` – Manolete Nov 04 '16 at 15:56
  • Maybe a table with 26 entries ? – Jabberwocky Nov 04 '16 at 15:59
  • @_Kerrek @MichaelWalz Could you please elaborate? – Manolete Nov 04 '16 at 16:03
  • @Manolete see my answer, Maybe you should elaborate your sample and add just the code for `case 1:` as well. – Jabberwocky Nov 04 '16 at 16:12
  • 1
    I'm a bit late on this but rather than use arrays of double, why not use an unsigned 32 bit integer with flags. Wherein bit 0 is the direction and bits 1-26 indicate the plane(s) using flags. I say planes because it would enable formation movement ~0 (all move right) and ~1 (all move left)... you could also reserve extra bits for up/down (or pitch, yaw and roll to be more appropriate). – technosaurus Nov 04 '16 at 20:14
  • @technosaurus I know it's late, but if you could elaborate your answer would be great for the community!! – Manolete Nov 07 '16 at 16:41

4 Answers4

10

You can create a lookup table like this:

double *pointers[26][2] = {
    { p_YZ_L, p_YZ_R },
    ...
};

Then your function becomes much simpler:

double* getPointer(int plane, int direction) {
    if ((plane >= 0) && (plane < 26) && (direction >= 0) && (direction < 2)) {
        return pointers[plane][direction];
    } else {
        return NULL;
    }
}
dbush
  • 205,898
  • 23
  • 218
  • 273
3

If you are just tired of typing, yu can use the preprocessor, e.g.:

#define PLZ(dir) if(!dir)return(p_YZ_L);else if(dir==1)return(p_YZ_R);else return 0;
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
2

Not quite sure, but maybe you want this:

struct
{
  double dir[2];
} directions[26] =
{
  { p_YZ_L, p_YZ_R},
  { ..., ... },           // 25 pairs of options here
  ...            
};

double* getPointer(int plane, int direction) {
  return  &directions[plane].dir[direction];
}

More tests need to be added to be sure that plane and direction are within required bounds.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
1

You can use while with an iterator as follows:

double* getPointer(int plane, int direction){
  int i=0;
  while (i<26){
    if (plane == i){
       if (direction == 0)
          return  p_YZ_L; //Left
       else if(dir==1)
          return p_YZ_R; //Right
       else 
          return 0;
    }
    i++;
  }
}

It is not optimized but it is less code with respect to your version.

ReshaD
  • 936
  • 2
  • 18
  • 30