a little `switch' use question

which is better?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
switch (m%4)
   case 0:
     ms = m-1;
     ckm(ms+1) = -m;
     break;
   case 1:
      ms = m;
      ckm(ms+1) = 1;
      break;
   case 2:
      ms = m-1;
      ckm(ms+1) = m;
      break;
   case 3:
      ms = m;
      ckm(ms+1) = -1;
      break;

or
1
2
3
4
int tmp_0[] = {m-1, m, m-1, m};
int tmp_1[] = {-m, 1, m, -1};
ms = tmp_0[m%4];
ckm[ms] = tmp_1[m%4];

or is there some other, better way, possible a little trick, to do that? what do you think?
To me its a readability issue. A switch is easier to read at the expense of complexity (code level not read level) and creates more bloated code.

However, the array method produces less bloated code, and reduced code level complexity, however is more difficult to read.

As far as efficiency, I think its a wash with modern compilers.
The latter is better. Particularly if it replaces a switch with many cases. Prefer it whenever possible.
Thank you. That is what I also thought.
But I see it as a little algorithmic puzzle, so I was actually looking for some neat trick, like for example writing the tmp_1 as a*{-1 1 1 -1} + b*{-1 -1 1 1}, with a = (m+1)/2, and b = (m-1)/2, but everything I could come up with, appears to be too complicated, at least when expressed in C++. any nice ideas?
Topic archived. No new replies allowed.