docs: private static byte[] convertToByteArray(final DoublesSketch sketch, final int flags,
                                             final boolean ordered, final boolean compact) {
      final int preLongs = 2;
      final int extra = 2; // extra space for min and max values
      final int prePlusExtraBytes = (preLongs + extra) << 3;
      final int k = sketch.getK();
      final long n = sketch.getN();
  
      // If not-compact, have accessor always report full levels. Then use level size to determine
      // whether to copy data out.
      final DoublesSketchAccessor dsa = DoublesSketchAccessor.wrap(sketch, !compact);
  
      final int outBytes = (compact ? sketch.getCompactStorageBytes() : sketch.getUpdatableStorageBytes());
  
      final byte[] outByteArr = new byte[outBytes];
      final WritableMemory memOut = WritableMemory.wrap(outByteArr);
  
      //insert preamble-0, N, min, max
      insertPre0(memOut, preLongs, flags, k);
      if (sketch.isEmpty()) { return outByteArr; }
  
      insertN(memOut, n);
      insertMinDouble(memOut, sketch.getMinValue());
      insertMaxDouble(memOut, sketch.getMaxValue());
  
      long memOffsetBytes = prePlusExtraBytes;
  
      // might need to sort base buffer but don't want to change input sketch
      final int bbCnt = Util.computeBaseBufferItems(k, n);
      if (bbCnt > 0) { //Base buffer items only
        final double[] bbItemsArr = dsa.getArray(0, bbCnt);
        if (ordered) { Arrays.sort(bbItemsArr); }
        memOut.putDoubleArray(memOffsetBytes, bbItemsArr, 0, bbCnt);
      }
      // If n < 2k, totalLevels == 0 so ok to overshoot the offset update
      memOffsetBytes += (compact ? bbCnt : 2 * k) << 3;
  
      // If serializing from a compact sketch to a non-compact form, we may end up copying data for a
      // higher level one or more times into an unused level. A bit wasteful, but not incorrect.
      final int totalLevels = Util.computeTotalLevels(sketch.getBitPattern());
      for (int lvl = 0; lvl < totalLevels; ++lvl) {
        dsa.setLevel(lvl);
        if (dsa.numItems() > 0) {
          assert dsa.numItems() == k;
          memOut.putDoubleArray(memOffsetBytes, dsa.getArray(0, k), 0, k);
          memOffsetBytes += (k << 3);
        }
      }
  
      return outByteArr;
    }