private static int findFirstDupOrDrop(int[] reorder,int newArity){
  final int BIT_LIMIT=63;
  if (newArity < BIT_LIMIT) {
    long mask=0;
    for (int i=0; i < reorder.length; i++) {
      int arg=reorder[i];
      if (arg >= newArity) {
        return reorder.length;
      }
      long bit=1L << arg;
      if ((mask & bit) != 0) {
        return i;
      }
      mask|=bit;
    }
    if (mask == (1L << newArity) - 1) {
      assert (Long.numberOfTrailingZeros(Long.lowestOneBit(~mask)) == newArity);
      return 0;
    }
    long zeroBit=Long.lowestOneBit(~mask);
    int zeroPos=Long.numberOfTrailingZeros(zeroBit);
    assert (zeroPos <= newArity);
    if (zeroPos == newArity) {
      return 0;
    }
    return ~zeroPos;
  }
 else {
    BitSet mask=new BitSet(newArity);
    for (int i=0; i < reorder.length; i++) {
      int arg=reorder[i];
      if (arg >= newArity) {
        return reorder.length;
      }
      if (mask.get(arg)) {
        return i;
      }
      mask.set(arg);
    }
    int zeroPos=mask.nextClearBit(0);
    assert (zeroPos <= newArity);
    if (zeroPos == newArity) {
      return 0;
    }
    return ~zeroPos;
  }
}

private static int findFirstDupOrDrop ( int [ ] reorder , int newArity ) { final int BIT_LIMIT = 63 ; if ( newArity < BIT_LIMIT ) { long mask = 0 ; for ( int i = 0 ; i < reorder . length ; i ++ ) { int arg = reorder [ i ] ; if ( arg >= newArity ) { return reorder . length ; } long bit = 1L << arg ; if ( ( mask & bit ) != 0 ) { return i ; } mask |= bit ; } if ( mask == ( 1L << newArity ) - 1 ) { assert ( Long . numberOfTrailingZeros ( Long . lowestOneBit ( ~ mask ) ) == newArity ) ; return 0 ; } long zeroBit = Long . lowestOneBit ( ~ mask ) ; int zeroPos = Long . numberOfTrailingZeros ( zeroBit ) ; assert ( zeroPos <= newArity ) ; if ( zeroPos == newArity ) { return 0 ; } return ~ zeroPos ; } else { BitSet mask = new BitSet ( newArity ) ; for ( int i = 0 ; i < reorder . length ; i ++ ) { int arg = reorder [ i ] ; if ( arg >= newArity ) { return reorder . length ; } if ( mask . get ( arg ) ) { return i ; } mask . set ( arg ) ; } int zeroPos = mask . nextClearBit ( 0 ) ; assert ( zeroPos <= newArity ) ; if ( zeroPos == newArity ) { return 0 ; } return ~ zeroPos ; } }
return an indication of any duplicate or omission in reorder . if the reorder contains a duplicate entry , return the index of the second occurrence . otherwise , return ~ ( n ) , for the first n in [ 0 . . newarity - 1 ] that is not present in reorder . otherwise , return zero . if an element not in [ 0 . . newarity - 1 ] is encountered , return reorder . length .
