code: static HeapUpdateDoublesSketch copyToHeap(final DoublesSketch sketch) {
      final HeapUpdateDoublesSketch qsCopy;
      qsCopy = HeapUpdateDoublesSketch.newInstance(sketch.getK());
      qsCopy.putN(sketch.getN());
      qsCopy.putMinValue(sketch.getMinValue());
      qsCopy.putMaxValue(sketch.getMaxValue());
      qsCopy.putBaseBufferCount(sketch.getBaseBufferCount());
      qsCopy.putBitPattern(sketch.getBitPattern());
  
      if (sketch.isCompact()) {
        final int combBufItems = Util.computeCombinedBufferItemCapacity(sketch.getK(), sketch.getN());
        final double[] combBuf = new double[combBufItems];
        qsCopy.putCombinedBuffer(combBuf);
        final DoublesSketchAccessor sketchAccessor = DoublesSketchAccessor.wrap(sketch);
        final DoublesSketchAccessor copyAccessor = DoublesSketchAccessor.wrap(qsCopy);
        // start with BB
        copyAccessor.putArray(sketchAccessor.getArray(0, sketchAccessor.numItems()),
                0, 0, sketchAccessor.numItems());
  
        long bitPattern = sketch.getBitPattern();
        for (int lvl = 0; bitPattern != 0L; ++lvl, bitPattern >>>= 1) {
          if ((bitPattern & 1L) > 0L) {
            sketchAccessor.setLevel(lvl);
            copyAccessor.setLevel(lvl);
            copyAccessor.putArray(sketchAccessor.getArray(0, sketchAccessor.numItems()),
                    0, 0, sketchAccessor.numItems());
          }
        }
      } else {
        final double[] combBuf = sketch.getCombinedBuffer();
        qsCopy.putCombinedBuffer(Arrays.copyOf(combBuf, combBuf.length));
      }
      return qsCopy;
    }
docs: Returns an on-heap copy of the given sketch
  @param sketch the given sketch
  @return a copy of the given sketch