static byte[] combinedBufferToByteArray(DoublesSketch sketch,boolean ordered,boolean compact){
  final int preLongs=2;
  final int extra=2;
  int preBytes=(preLongs + extra) << 3;
  int flags=(ordered ? ORDERED_FLAG_MASK : 0) | (compact ? COMPACT_FLAG_MASK : 0);
  int k=sketch.getK();
  long n=sketch.getN();
  double[] combinedBuffer=sketch.getCombinedBuffer();
  double[] bbItemsArr=null;
  final int bbCnt=Util.computeBaseBufferItems(k,n);
  if (bbCnt > 0) {
    bbItemsArr=new double[bbCnt];
    System.arraycopy(combinedBuffer,0,bbItemsArr,0,bbCnt);
    if (ordered) {
      Arrays.sort(bbItemsArr);
    }
  }
  byte[] outByteArr=null;
  if (compact) {
    final int retainedItems=sketch.getRetainedItems();
    int outBytes=(retainedItems << 3) + preBytes;
    outByteArr=new byte[outBytes];
    Memory memOut=new NativeMemory(outByteArr);
    long cumOffset=memOut.getCumulativeOffset(0L);
    insertPre0(outByteArr,cumOffset,preLongs,flags,k);
    insertN(outByteArr,cumOffset,n);
    insertMinDouble(outByteArr,cumOffset,sketch.getMinValue());
    insertMaxDouble(outByteArr,cumOffset,sketch.getMaxValue());
    if (bbCnt > 0) {
      memOut.putDoubleArray(preBytes,bbItemsArr,0,bbCnt);
    }
    long bits=sketch.getBitPattern();
    if (bits != 0) {
      long memOffset=preBytes + (bbCnt << 3);
      int combBufOffset=2 * k;
      while (bits != 0L) {
        if ((bits & 1L) > 0L) {
          memOut.putDoubleArray(memOffset,combinedBuffer,combBufOffset,k);
          memOffset+=(k << 3);
        }
        combBufOffset+=k;
        bits>>>=1;
      }
    }
  }
 else {
    final int totLevels=Util.computeNumLevelsNeeded(k,n);
    int outBytes=(totLevels == 0) ? (bbCnt << 3) + preBytes : (((2 + totLevels) * k) << 3) + preBytes;
    outByteArr=new byte[outBytes];
    Memory memOut=new NativeMemory(outByteArr);
    long cumOffset=memOut.getCumulativeOffset(0L);
    insertPre0(outByteArr,cumOffset,preLongs,flags,k);
    insertN(outByteArr,cumOffset,n);
    insertMinDouble(outByteArr,cumOffset,sketch.getMinValue());
    insertMaxDouble(outByteArr,cumOffset,sketch.getMaxValue());
    if (bbCnt > 0) {
      memOut.putDoubleArray(preBytes,bbItemsArr,0,bbCnt);
    }
    if (totLevels > 0) {
      long memOffset=preBytes + ((2L * k) << 3);
      int combBufOffset=2 * k;
      memOut.putDoubleArray(memOffset,combinedBuffer,combBufOffset,totLevels * k);
    }
  }
  return outByteArr;
}

static byte [ ] combinedBufferToByteArray ( DoublesSketch sketch , boolean ordered , boolean compact ) { final int preLongs = 2 ; final int extra = 2 ; int preBytes = ( preLongs + extra ) << 3 ; int flags = ( ordered ? ORDERED_FLAG_MASK : 0 ) | ( compact ? COMPACT_FLAG_MASK : 0 ) ; int k = sketch . getK ( ) ; long n = sketch . getN ( ) ; double [ ] combinedBuffer = sketch . getCombinedBuffer ( ) ; double [ ] bbItemsArr = null ; final int bbCnt = Util . computeBaseBufferItems ( k , n ) ; if ( bbCnt > 0 ) { bbItemsArr = new double [ bbCnt ] ; System . arraycopy ( combinedBuffer , 0 , bbItemsArr , 0 , bbCnt ) ; if ( ordered ) { Arrays . sort ( bbItemsArr ) ; } } byte [ ] outByteArr = null ; if ( compact ) { final int retainedItems = sketch . getRetainedItems ( ) ; int outBytes = ( retainedItems << 3 ) + preBytes ; outByteArr = new byte [ outBytes ] ; Memory memOut = new NativeMemory ( outByteArr ) ; long cumOffset = memOut . getCumulativeOffset ( 0L ) ; insertPre0 ( outByteArr , cumOffset , preLongs , flags , k ) ; insertN ( outByteArr , cumOffset , n ) ; insertMinDouble ( outByteArr , cumOffset , sketch . getMinValue ( ) ) ; insertMaxDouble ( outByteArr , cumOffset , sketch . getMaxValue ( ) ) ; if ( bbCnt > 0 ) { memOut . putDoubleArray ( preBytes , bbItemsArr , 0 , bbCnt ) ; } long bits = sketch . getBitPattern ( ) ; if ( bits != 0 ) { long memOffset = preBytes + ( bbCnt << 3 ) ; int combBufOffset = 2 * k ; while ( bits != 0L ) { if ( ( bits & 1L ) > 0L ) { memOut . putDoubleArray ( memOffset , combinedBuffer , combBufOffset , k ) ; memOffset += ( k << 3 ) ; } combBufOffset += k ; bits >>>= 1 ; } } } else { final int totLevels = Util . computeNumLevelsNeeded ( k , n ) ; int outBytes = ( totLevels == 0 ) ? ( bbCnt << 3 ) + preBytes : ( ( ( 2 + totLevels ) * k ) << 3 ) + preBytes ; outByteArr = new byte [ outBytes ] ; Memory memOut = new NativeMemory ( outByteArr ) ; long cumOffset = memOut . getCumulativeOffset ( 0L ) ; insertPre0 ( outByteArr , cumOffset , preLongs , flags , k ) ; insertN ( outByteArr , cumOffset , n ) ; insertMinDouble ( outByteArr , cumOffset , sketch . getMinValue ( ) ) ; insertMaxDouble ( outByteArr , cumOffset , sketch . getMaxValue ( ) ) ; if ( bbCnt > 0 ) { memOut . putDoubleArray ( preBytes , bbItemsArr , 0 , bbCnt ) ; } if ( totLevels > 0 ) { long memOffset = preBytes + ( ( 2L * k ) << 3 ) ; int combBufOffset = 2 * k ; memOut . putDoubleArray ( memOffset , combinedBuffer , combBufOffset , totLevels * k ) ; } } return outByteArr ; }
returns a byte array , including preamble , min , max and data extracted from the combined buffer .
