3

I have being seeing some weird codes of late. And i could not understand significance of their use.
a if(m==c)m=c; does not make any sense until it is seen in a library such as fastutil and jhighlight. I have googled a lot on the subject but to my surprise it seems to be Java specification which is not so documented - maybe due to deprecation or such. I will be grateful if someone would elaborate.

            int comparison;
            while ( b <= c && ( ( comparison = comp.compare( b, m ) ) <= 0 ) ) {
                if ( comparison == 0 ) {
                    if ( a == m ) m = b; // moving target; DELTA to JDK !!!
                    else if ( b == m ) m = a; // moving target; DELTA to JDK !!!
                    swapper.swap( a++, b );
                }
                b++;
            }
            while ( c >= b && ( ( comparison = comp.compare( c, m ) ) >= 0 ) ) {
                if ( comparison == 0 ) {
                    if ( c == m ) m = d; // moving target; DELTA to JDK !!!
                    else if ( d == m ) m = c; // moving target; DELTA to JDK !!!
                    swapper.swap( c, d-- );
                }
                c--;
            }
            if ( b > c ) break;
            if ( b == m ) m = d; // moving target; DELTA to JDK !!!
            else if ( c == m ) m = c; // moving target; DELTA to JDK !!!
            swapper.swap( b++, c-- );
        }
D. Sikilai
  • 467
  • 1
  • 3
  • 17
  • It is odd. Perhaps they're trying to do [type conversion](https://www.geeksforgeeks.org/type-conversion-java-examples/)? Really hard to say without more context of what you're showing such as what type these variables are. – Tim Hunter Nov 17 '20 at 21:47
  • 1
    Looks like a low level optimization, but hard to say what purpose. Perhaps the generated assembly is optimal with that sort of branching structure, with the no-op `else if`. Especially since it matches the two other `if/if else` parts. – Kayaman Nov 17 '20 at 21:52
  • 1
    @TimHunter Example on this file [jhighlight line 306](https://github.com/codelibs/jhighlight/blob/2f096bbd49fb7e2100cf35de48bdaaf22a3ee6e7/src/main/java/com/uwyn/jhighlight/fastutil/Arrays.java#L306). They are both ```int``` type. I thought the hint could be in the comments but... – D. Sikilai Nov 17 '20 at 21:53
  • @Kayaman that might be the reason. I could view the assembly code. – D. Sikilai Nov 17 '20 at 21:55
  • 2
    Ahhhh, it's the good ole quick sort algorithm. Very possible they also just left that to keep the code as logically similar as the pieces in the while loops before it for ease of understanding. – Tim Hunter Nov 17 '20 at 21:59
  • Purely by symmetry, is it possible `...if ( c == m ) m = c` should really be `...if ( c == m ) m = b`? – Jim Garrison Nov 18 '20 at 00:03

0 Answers0