/** * Obtains a new Direct instance of a DoublesSketch, which may be off-heap. * * @param k Parameter that controls space usage of sketch and accuracy of estimates. * Must be greater than 1 and less than 65536 and a power of 2. * @param dstMem the destination Memory that will be initialized to hold the data for this sketch. * It must initially be at least (16 * MIN_K + 32) bytes, where MIN_K defaults to 2. As it grows * it will request more memory using the MemoryRequest callback. * @return a DirectUpdateDoublesSketch */ static DirectUpdateDoublesSketch newInstance(final int k, final WritableMemory dstMem) { // must be able to hold at least an empty sketch final long memCap = dstMem.getCapacity(); checkDirectMemCapacity(k, 0, memCap); //initialize dstMem dstMem.putLong(0, 0L); //clear pre0 insertPreLongs(dstMem, 2); insertSerVer(dstMem, DoublesSketch.DOUBLES_SER_VER); insertFamilyID(dstMem, Family.QUANTILES.getID()); insertFlags(dstMem, EMPTY_FLAG_MASK); insertK(dstMem, k); if (memCap >= COMBINED_BUFFER) { insertN(dstMem, 0L); insertMinDouble(dstMem, Double.NaN); insertMaxDouble(dstMem, Double.NaN); } final DirectUpdateDoublesSketch dds = new DirectUpdateDoublesSketch(k); dds.mem_ = dstMem; return dds; }
/** * Wrap this sketch around the given non-compact Memory image of a DoublesSketch. * * @param srcMem the given non-compact Memory image of a DoublesSketch that may have data * @return a sketch that wraps the given srcMem */ static DirectUpdateDoublesSketch wrapInstance(final WritableMemory srcMem) { final long memCap = srcMem.getCapacity(); final int preLongs = extractPreLongs(srcMem); final int serVer = extractSerVer(srcMem); final int familyID = extractFamilyID(srcMem); final int flags = extractFlags(srcMem); final int k = extractK(srcMem); final boolean empty = (flags & EMPTY_FLAG_MASK) > 0; //Preamble flags empty state final long n = empty ? 0 : extractN(srcMem); //VALIDITY CHECKS checkPreLongs(preLongs); Util.checkFamilyID(familyID); DoublesUtil.checkDoublesSerVer(serVer, MIN_DIRECT_DOUBLES_SER_VER); checkDirectFlags(flags); //Cannot be compact Util.checkK(k); checkCompact(serVer, flags); checkDirectMemCapacity(k, n, memCap); checkEmptyAndN(empty, n); final DirectUpdateDoublesSketch dds = new DirectUpdateDoublesSketch(k); dds.mem_ = srcMem; return dds; }
/** * Obtains a new Direct instance of a DoublesSketch, which may be off-heap. * * @param k Parameter that controls space usage of sketch and accuracy of estimates. * Must be greater than 1 and less than 65536 and a power of 2. * @param dstMem the destination Memory that will be initialized to hold the data for this sketch. * It must initially be at least (16 * MIN_K + 32) bytes, where MIN_K defaults to 2. As it grows * it will request more memory using the MemoryRequest callback. * @return a DirectUpdateDoublesSketch */ static DirectUpdateDoublesSketch newInstance(final int k, final WritableMemory dstMem) { // must be able to hold at least an empty sketch final long memCap = dstMem.getCapacity(); checkDirectMemCapacity(k, 0, memCap); //initialize dstMem dstMem.putLong(0, 0L); //clear pre0 insertPreLongs(dstMem, 2); insertSerVer(dstMem, DoublesSketch.DOUBLES_SER_VER); insertFamilyID(dstMem, Family.QUANTILES.getID()); insertFlags(dstMem, EMPTY_FLAG_MASK); insertK(dstMem, k); if (memCap >= COMBINED_BUFFER) { insertN(dstMem, 0L); insertMinDouble(dstMem, Double.NaN); insertMaxDouble(dstMem, Double.NaN); } final DirectUpdateDoublesSketch dds = new DirectUpdateDoublesSketch(k); dds.mem_ = dstMem; return dds; }
/** * Wrap this sketch around the given non-compact Memory image of a DoublesSketch. * * @param srcMem the given non-compact Memory image of a DoublesSketch that may have data * @return a sketch that wraps the given srcMem */ static DirectUpdateDoublesSketch wrapInstance(final WritableMemory srcMem) { final long memCap = srcMem.getCapacity(); final int preLongs = extractPreLongs(srcMem); final int serVer = extractSerVer(srcMem); final int familyID = extractFamilyID(srcMem); final int flags = extractFlags(srcMem); final int k = extractK(srcMem); final boolean empty = (flags & EMPTY_FLAG_MASK) > 0; //Preamble flags empty state final long n = empty ? 0 : extractN(srcMem); //VALIDITY CHECKS checkPreLongs(preLongs); Util.checkFamilyID(familyID); DoublesUtil.checkDoublesSerVer(serVer, MIN_DIRECT_DOUBLES_SER_VER); checkDirectFlags(flags); //Cannot be compact Util.checkK(k); checkCompact(serVer, flags); checkDirectMemCapacity(k, n, memCap); checkEmptyAndN(empty, n); final DirectUpdateDoublesSketch dds = new DirectUpdateDoublesSketch(k); dds.mem_ = srcMem; return dds; }