protected BytesRef dedupAndEncode(IntsRef ordinals){
  Arrays.sort(ordinals.ints,ordinals.offset,ordinals.length);
  byte[] bytes=new byte[5 * ordinals.length];
  int lastOrd=-1;
  int upto=0;
  for (int i=0; i < ordinals.length; i++) {
    int ord=ordinals.ints[ordinals.offset + i];
    if (ord > lastOrd) {
      int delta;
      if (lastOrd == -1) {
        delta=ord;
      }
 else {
        delta=ord - lastOrd;
      }
      if ((delta & ~0x7F) == 0) {
        bytes[upto]=(byte)delta;
        upto++;
      }
 else       if ((delta & ~0x3FFF) == 0) {
        bytes[upto]=(byte)(0x80 | ((delta & 0x3F80) >> 7));
        bytes[upto + 1]=(byte)(delta & 0x7F);
        upto+=2;
      }
 else       if ((delta & ~0x1FFFFF) == 0) {
        bytes[upto]=(byte)(0x80 | ((delta & 0x1FC000) >> 14));
        bytes[upto + 1]=(byte)(0x80 | ((delta & 0x3F80) >> 7));
        bytes[upto + 2]=(byte)(delta & 0x7F);
        upto+=3;
      }
 else       if ((delta & ~0xFFFFFFF) == 0) {
        bytes[upto]=(byte)(0x80 | ((delta & 0xFE00000) >> 21));
        bytes[upto + 1]=(byte)(0x80 | ((delta & 0x1FC000) >> 14));
        bytes[upto + 2]=(byte)(0x80 | ((delta & 0x3F80) >> 7));
        bytes[upto + 3]=(byte)(delta & 0x7F);
        upto+=4;
      }
 else {
        bytes[upto]=(byte)(0x80 | ((delta & 0xF0000000) >> 28));
        bytes[upto + 1]=(byte)(0x80 | ((delta & 0xFE00000) >> 21));
        bytes[upto + 2]=(byte)(0x80 | ((delta & 0x1FC000) >> 14));
        bytes[upto + 3]=(byte)(0x80 | ((delta & 0x3F80) >> 7));
        bytes[upto + 4]=(byte)(delta & 0x7F);
        upto+=5;
      }
      lastOrd=ord;
    }
  }
  return new BytesRef(bytes,0,upto);
}

protected BytesRef dedupAndEncode ( IntsRef ordinals ) { Arrays . sort ( ordinals . ints , ordinals . offset , ordinals . length ) ; byte [ ] bytes = new byte [ 5 * ordinals . length ] ; int lastOrd = - 1 ; int upto = 0 ; for ( int i = 0 ; i < ordinals . length ; i ++ ) { int ord = ordinals . ints [ ordinals . offset + i ] ; if ( ord > lastOrd ) { int delta ; if ( lastOrd == - 1 ) { delta = ord ; } else { delta = ord - lastOrd ; } if ( ( delta & ~ 0x7F ) == 0 ) { bytes [ upto ] = ( byte ) delta ; upto ++ ; } else if ( ( delta & ~ 0x3FFF ) == 0 ) { bytes [ upto ] = ( byte ) ( 0x80 | ( ( delta & 0x3F80 ) > > 7 ) ) ; bytes [ upto + 1 ] = ( byte ) ( delta & 0x7F ) ; upto += 2 ; } else if ( ( delta & ~ 0x1FFFFF ) == 0 ) { bytes [ upto ] = ( byte ) ( 0x80 | ( ( delta & 0x1FC000 ) > > 14 ) ) ; bytes [ upto + 1 ] = ( byte ) ( 0x80 | ( ( delta & 0x3F80 ) > > 7 ) ) ; bytes [ upto + 2 ] = ( byte ) ( delta & 0x7F ) ; upto += 3 ; } else if ( ( delta & ~ 0xFFFFFFF ) == 0 ) { bytes [ upto ] = ( byte ) ( 0x80 | ( ( delta & 0xFE00000 ) > > 21 ) ) ; bytes [ upto + 1 ] = ( byte ) ( 0x80 | ( ( delta & 0x1FC000 ) > > 14 ) ) ; bytes [ upto + 2 ] = ( byte ) ( 0x80 | ( ( delta & 0x3F80 ) > > 7 ) ) ; bytes [ upto + 3 ] = ( byte ) ( delta & 0x7F ) ; upto += 4 ; } else { bytes [ upto ] = ( byte ) ( 0x80 | ( ( delta & 0xF0000000 ) > > 28 ) ) ; bytes [ upto + 1 ] = ( byte ) ( 0x80 | ( ( delta & 0xFE00000 ) > > 21 ) ) ; bytes [ upto + 2 ] = ( byte ) ( 0x80 | ( ( delta & 0x1FC000 ) > > 14 ) ) ; bytes [ upto + 3 ] = ( byte ) ( 0x80 | ( ( delta & 0x3F80 ) > > 7 ) ) ; bytes [ upto + 4 ] = ( byte ) ( delta & 0x7F ) ; upto += 5 ; } lastOrd = ord ; } } return new BytesRef ( bytes , 0 , upto ) ; }
encodes ordinals into a bytesref ; expert : subclass can override this to change encoding .
