private UnicodeSet retain(int[] other, int otherLen, int polarity) {
        ensureBufferCapacity(len + otherLen);
        int i = 0, j = 0, k = 0;
        int a = list[i++];
        int b = other[j++];
        // change from xor is that we have to check overlapping pairs
        // polarity bit 1 means a is second, bit 2 means b is.
        main:
            while (true) {
                switch (polarity) {
                case 0: // both first; drop the smaller
                    if (a < b) { // drop a
                        a = list[i++]; polarity ^= 1;
                    } else if (b < a) { // drop b
                        b = other[j++]; polarity ^= 2;
                    } else { // a == b, take one, drop other
                        if (a == HIGH) break main;
                        buffer[k++] = a; a = list[i++]; polarity ^= 1;
                        b = other[j++]; polarity ^= 2;
                    }
                    break;
                case 3: // both second; take lower if unequal
                    if (a < b) { // take a
                        buffer[k++] = a; a = list[i++]; polarity ^= 1;
                    } else if (b < a) { // take b
                        buffer[k++] = b; b = other[j++]; polarity ^= 2;
                    } else { // a == b, take one, drop other
                        if (a == HIGH) break main;
                        buffer[k++] = a; a = list[i++]; polarity ^= 1;
                        b = other[j++]; polarity ^= 2;
                    }
                    break;
                case 1: // a second, b first;
                    if (a < b) { // NO OVERLAP, drop a
                        a = list[i++]; polarity ^= 1;
                    } else if (b < a) { // OVERLAP, take b
                        buffer[k++] = b; b = other[j++]; polarity ^= 2;
                    } else { // a == b, drop both!
                        if (a == HIGH) break main;
                        a = list[i++]; polarity ^= 1;
                        b = other[j++]; polarity ^= 2;
                    }
                    break;
                case 2: // a first, b second; if a < b, overlap
                    if (b < a) { // no overlap, drop b
                        b = other[j++]; polarity ^= 2;
                    } else  if (a < b) { // OVERLAP, take a
                        buffer[k++] = a; a = list[i++]; polarity ^= 1;
                    } else { // a == b, drop both!
                        if (a == HIGH) break main;
                        a = list[i++]; polarity ^= 1;
                        b = other[j++]; polarity ^= 2;
                    }
                    break;
                }
            }
        buffer[k++] = HIGH;    // terminate
        len = k;
        // swap list and buffer
        int[] temp = list;
        list = buffer;
        buffer = temp;
        pat = null;
        return this;
    }
private UnicodeSet retain ( int [ ] other , int otherLen , int polarity ) { ensureBufferCapacity ( len + otherLen ) ; int i = 0 , j = 0 , k = 0 ; int a = list [ i ++ ] ; int b = other [ j ++ ] ; main : while ( true ) { switch ( polarity ) { case 0 : if ( a < b ) { a = list [ i ++ ] ; polarity ^= 1 ; } else if ( b < a ) { b = other [ j ++ ] ; polarity ^= 2 ; } else { if ( a == HIGH ) break main ; buffer [ k ++ ] = a ; a = list [ i ++ ] ; polarity ^= 1 ; b = other [ j ++ ] ; polarity ^= 2 ; } break ; case 3 : if ( a < b ) { buffer [ k ++ ] = a ; a = list [ i ++ ] ; polarity ^= 1 ; } else if ( b < a ) { buffer [ k ++ ] = b ; b = other [ j ++ ] ; polarity ^= 2 ; } else { if ( a == HIGH ) break main ; buffer [ k ++ ] = a ; a = list [ i ++ ] ; polarity ^= 1 ; b = other [ j ++ ] ; polarity ^= 2 ; } break ; case 1 : if ( a < b ) { a = list [ i ++ ] ; polarity ^= 1 ; } else if ( b < a ) { buffer [ k ++ ] = b ; b = other [ j ++ ] ; polarity ^= 2 ; } else { if ( a == HIGH ) break main ; a = list [ i ++ ] ; polarity ^= 1 ; b = other [ j ++ ] ; polarity ^= 2 ; } break ; case 2 : if ( b < a ) { b = other [ j ++ ] ; polarity ^= 2 ; } else if ( a < b ) { buffer [ k ++ ] = a ; a = list [ i ++ ] ; polarity ^= 1 ; } else { if ( a == HIGH ) break main ; a = list [ i ++ ] ; polarity ^= 1 ; b = other [ j ++ ] ; polarity ^= 2 ; } break ; } } buffer [ k ++ ] = HIGH ; len = k ; int [ ] temp = list ; list = buffer ; buffer = temp ; pat = null ; return this ; }
polarity = 3: ~x intersect ~y