/** * 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 DirectUpdateDoublesSketchR wrapInstance(final Memory 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 DirectUpdateDoublesSketchR dds = new DirectUpdateDoublesSketchR(k); dds.mem_ = (WritableMemory) srcMem; return dds; }
@Override double[] getCombinedBuffer() { final int k = getK(); if (isEmpty()) { return new double[k << 1]; } //2K final long n = getN(); final int itemCap = Util.computeCombinedBufferItemCapacity(k, n); final double[] combinedBuffer = new double[itemCap]; mem_.getDoubleArray(COMBINED_BUFFER, combinedBuffer, 0, itemCap); return combinedBuffer; }
/** * Checks the validity of the direct memory capacity assuming n, k. * @param k the given value of k * @param n the given value of n * @param memCapBytes the current memory capacity in bytes */ static void checkDirectMemCapacity(final int k, final long n, final long memCapBytes) { final int reqBufBytes = getUpdatableStorageBytes(k, n); if (memCapBytes < reqBufBytes) { throw new SketchesArgumentException("Possible corruption: Memory capacity too small: " + memCapBytes + " < " + reqBufBytes); } }
/** * Returns a read-only Union object that wraps off-heap data structure of the given memory * image of a non-compact DoublesSketch. The data structures of the Union remain off-heap. * * @param mem A memory image of a non-compact DoublesSketch to be used as the data * structure for the union and will be modified. * @return a Union object */ static DoublesUnionImplR wrapInstance(final Memory mem) { final DirectUpdateDoublesSketchR sketch = DirectUpdateDoublesSketchR.wrapInstance(mem); final int k = sketch.getK(); final DoublesUnionImplR union = new DoublesUnionImplR(k); union.maxK_ = k; union.gadget_ = sketch; return union; }
try { int flags = PreambleUtil.COMPACT_FLAG_MASK; DirectUpdateDoublesSketchR.checkCompact(2, 0); fail(); } catch (SketchesArgumentException e) {} //OK try { int flags = PreambleUtil.COMPACT_FLAG_MASK; DirectUpdateDoublesSketchR.checkCompact(3, flags); fail(); } catch (SketchesArgumentException e) {} //OK try { DirectUpdateDoublesSketchR.checkPreLongs(3); fail(); } catch (SketchesArgumentException e) {} //OK try { DirectUpdateDoublesSketchR.checkPreLongs(0); fail(); } catch (SketchesArgumentException e) {} //OK try { DirectUpdateDoublesSketchR.checkDirectFlags(PreambleUtil.COMPACT_FLAG_MASK); fail(); } catch (SketchesArgumentException e) {} //OK try { DirectUpdateDoublesSketchR.checkEmptyAndN(true, 1); fail(); } catch (SketchesArgumentException e) {} //OK
/** * Wrap this sketch around the given compact Memory image of a DoublesSketch. * * @param srcMem the given compact Memory image of a DoublesSketch that may have data, * @return a sketch that wraps the given srcMem */ static DirectCompactDoublesSketch wrapInstance(final Memory 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; final long n = empty ? 0 : extractN(srcMem); //VALIDITY CHECKS DirectUpdateDoublesSketchR.checkPreLongs(preLongs); Util.checkFamilyID(familyID); DoublesUtil.checkDoublesSerVer(serVer, MIN_DIRECT_DOUBLES_SER_VER); checkCompact(serVer, flags); Util.checkK(k); checkDirectMemCapacity(k, n, memCap); DirectUpdateDoublesSketchR.checkEmptyAndN(empty, n); final DirectCompactDoublesSketch dds = new DirectCompactDoublesSketch(k); dds.mem_ = (WritableMemory) srcMem; return dds; }
@Test public void checkCheckDirectMemCapacity() { final int k = 128; DirectUpdateDoublesSketchR.checkDirectMemCapacity(k, (2 * k) - 1, (4 + (2 * k)) * 8); DirectUpdateDoublesSketchR.checkDirectMemCapacity(k, (2 * k) + 1, (4 + (3 * k)) * 8); DirectUpdateDoublesSketchR.checkDirectMemCapacity(k, 0, 8); try { DirectUpdateDoublesSketchR.checkDirectMemCapacity(k, 10000, 64); fail(); } catch (final SketchesArgumentException e) { // expected } }
@Override public double getMinValue() { return isEmpty() ? Double.NaN : mem_.getDouble(MIN_DOUBLE); }
/** * Wrap this sketch around the given compact Memory image of a DoublesSketch. * * @param srcMem the given compact Memory image of a DoublesSketch that may have data, * @return a sketch that wraps the given srcMem */ static DirectCompactDoublesSketch wrapInstance(final Memory 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; final long n = empty ? 0 : extractN(srcMem); //VALIDITY CHECKS DirectUpdateDoublesSketchR.checkPreLongs(preLongs); Util.checkFamilyID(familyID); DoublesUtil.checkDoublesSerVer(serVer, MIN_DIRECT_DOUBLES_SER_VER); checkCompact(serVer, flags); Util.checkK(k); checkDirectMemCapacity(k, n, memCap); DirectUpdateDoublesSketchR.checkEmptyAndN(empty, n); final DirectCompactDoublesSketch dds = new DirectCompactDoublesSketch(k); dds.mem_ = (WritableMemory) srcMem; return dds; }
/** * Returns a read-only Union object that wraps off-heap data structure of the given memory * image of a non-compact DoublesSketch. The data structures of the Union remain off-heap. * * @param mem A memory image of a non-compact DoublesSketch to be used as the data * structure for the union and will be modified. * @return a Union object */ static DoublesUnionImplR wrapInstance(final Memory mem) { final DirectUpdateDoublesSketchR sketch = DirectUpdateDoublesSketchR.wrapInstance(mem); final int k = sketch.getK(); final DoublesUnionImplR union = new DoublesUnionImplR(k); union.maxK_ = k; union.gadget_ = sketch; return union; }
@Override public double getMaxValue() { return isEmpty() ? Double.NaN : mem_.getDouble(MAX_DOUBLE); }
/** * 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 DirectUpdateDoublesSketchR wrapInstance(final Memory 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 DirectUpdateDoublesSketchR dds = new DirectUpdateDoublesSketchR(k); dds.mem_ = (WritableMemory) srcMem; return dds; }
@Override double[] getCombinedBuffer() { final int k = getK(); if (isEmpty()) { return new double[k << 1]; } //2K final long n = getN(); final int itemCap = Util.computeCombinedBufferItemCapacity(k, n); final double[] combinedBuffer = new double[itemCap]; mem_.getDoubleArray(COMBINED_BUFFER, combinedBuffer, 0, itemCap); return combinedBuffer; }
@Override public double getMinValue() { return isEmpty() ? Double.NaN : mem_.getDouble(MIN_DOUBLE); }
/** * Checks the validity of the direct memory capacity assuming n, k. * @param k the given value of k * @param n the given value of n * @param memCapBytes the current memory capacity in bytes */ static void checkDirectMemCapacity(final int k, final long n, final long memCapBytes) { final int reqBufBytes = getUpdatableStorageBytes(k, n); if (memCapBytes < reqBufBytes) { throw new SketchesArgumentException("Possible corruption: Memory capacity too small: " + memCapBytes + " < " + reqBufBytes); } }
@Override public double getMaxValue() { return isEmpty() ? Double.NaN : mem_.getDouble(MAX_DOUBLE); }