c++cperformanceintegersymmetry

How to detect symmetries in 4 integer variables efficiently?


I want to find symmetries in 4 integer variables i,j,k and l . The symmetries are:

  1. all four numbers are equal: XXXX,
  2. three numbers are equal: XXXY,XXYX,XYXX,YXXX
  3. two pairs of equal numbers: XXYY,XYXY,XYYX,...
  4. one pair of equal numbers and two different numbers: XXYZ,XYXZ,XYZX,...
  5. all numbers are different.

All variables run within a certain non continuous range. I use nested if else statements. The first if checks for inequality of all variables. If not, then I have case 1. The next if checks if there are any equal pairs. If not, then case 5. The next if checks for three equal numbers. If true, then case 2. Otherwise, the last if checks for two pairs of equal numbers. If true, then case 3, otherwise case 4.

  if(!(i==j && j==k && k==l)){
    if(i==j || i==k || i==l || j==k || j==l || k==l){
     if((i==j && j==k) || (i==j && j==l) || (i==k && k==l) || (j==k && k==l)){            ...//do something
     }else{
    if((i==j && k==l) || (i==k && j==l) || (i==l && j==k)){ 
...//do something
    }else{
     ...//do something
    }           
  }
     }else{
     ...//do something  
     } 
 }else{
  ...//do something
 }  

Is there better way do do this? I mean better in the sense of better performance, because I have to do this test millions of times.


Solution

  • Similar idea than samgak, but without the need of external table. Just count the sum of all matches

    int count = (i==j) + (i==k) + (i==l) + (j==k) + (j==l) + (k==l);
    

    and do switch with following choices

    switch (count){
    case 0: //All differenct
    case 1: //One same
    case 2: //Two different pairs
    case 3: //Three same
    case 6: //All are same
    }
    

    Again, as already mentioned, your current code might be faster in some cases. Especially if the most common case is the one where all the elements are equal.