Skip to content
Permalink
Browse files
Interim 14
  • Loading branch information
leerho committed Apr 8, 2022
1 parent 1d79763 commit 1b54257e288c049058c6af495f0284b5400d37e6
Showing 19 changed files with 163 additions and 138 deletions.
@@ -40,7 +40,7 @@
import static org.apache.datasketches.kll.KllPreambleUtil.setMemoryNumLevels;
import static org.apache.datasketches.kll.KllPreambleUtil.setMemoryPreInts;
import static org.apache.datasketches.kll.KllPreambleUtil.setMemorySerVer;
import static org.apache.datasketches.kll.KllSketch.Error.TGT_IS_IMMUTABLE;
import static org.apache.datasketches.kll.KllSketch.Error.TGT_IS_READ_ONLY;
import static org.apache.datasketches.kll.KllSketch.Error.kllSketchThrow;

import org.apache.datasketches.Family;
@@ -117,7 +117,7 @@ public long getN() {

@Override
public void reset() {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
final int k = getK();
setN(0);
setMinK(k);
@@ -202,14 +202,14 @@ int getNumLevels() {

@Override
void incN() {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
long n = getMemoryN(wmem);
setMemoryN(wmem, ++n);
}

@Override
void incNumLevels() {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
int numLevels = getMemoryNumLevels(wmem);
setMemoryNumLevels(wmem, ++numLevels);
}
@@ -221,95 +221,95 @@ boolean isLevelZeroSorted() {

@Override
void setDoubleItemsArray(final double[] doubleItems) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
itemsArrUpdatable.putDoubleArray(0, doubleItems, 0, doubleItems.length);
}

@Override
void setDoubleItemsArrayAt(final int index, final double value) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
itemsArrUpdatable.putDouble((long)index * Double.BYTES, value);
}

@Override
void setItemsArrayUpdatable(final WritableMemory itemsMem) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
itemsArrUpdatable = itemsMem;
}

@Override
void setLevelsArray(final int[] levelsArr) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
levelsArrUpdatable.putIntArray(0, levelsArr, 0, levelsArr.length);
}

@Override
void setLevelsArrayAt(final int index, final int value) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
levelsArrUpdatable.putInt((long)index * Integer.BYTES, value);
}

@Override
void setLevelsArrayAtMinusEq(final int index, final int minusEq) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
final int offset = index * Integer.BYTES;
final int curV = levelsArrUpdatable.getInt(offset);
levelsArrUpdatable.putInt(offset, curV - minusEq);
}

@Override
void setLevelsArrayAtPlusEq(final int index, final int plusEq) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
final int offset = index * Integer.BYTES;
final int curV = levelsArrUpdatable.getInt(offset);
levelsArrUpdatable.putInt(offset, curV + plusEq);
}

@Override
void setLevelsArrayUpdatable(final WritableMemory levelsMem) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
levelsArrUpdatable = levelsMem;
}

@Override
void setLevelZeroSorted(final boolean sorted) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
setMemoryLevelZeroSortedFlag(wmem, sorted);
}

@Override
void setMaxDoubleValue(final double value) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
minMaxArrUpdatable.putDouble(Double.BYTES, value);
}

@Override
void setMinDoubleValue(final double value) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
minMaxArrUpdatable.putDouble(0, value);
}

@Override
void setMinK(final int minK) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
setMemoryMinK(wmem, minK);
}

@Override
void setMinMaxArrayUpdatable(final WritableMemory minMaxMem) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
minMaxArrUpdatable = minMaxMem;
}

@Override
void setN(final long n) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
setMemoryN(wmem, n);
}

@Override
void setNumLevels(final int numLevels) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
setMemoryNumLevels(wmem, numLevels);
}

@@ -39,7 +39,7 @@
import static org.apache.datasketches.kll.KllPreambleUtil.setMemoryNumLevels;
import static org.apache.datasketches.kll.KllPreambleUtil.setMemoryPreInts;
import static org.apache.datasketches.kll.KllPreambleUtil.setMemorySerVer;
import static org.apache.datasketches.kll.KllSketch.Error.TGT_IS_IMMUTABLE;
import static org.apache.datasketches.kll.KllSketch.Error.TGT_IS_READ_ONLY;
import static org.apache.datasketches.kll.KllSketch.Error.kllSketchThrow;

import org.apache.datasketches.Family;
@@ -117,7 +117,7 @@ public long getN() {

@Override
public void reset() {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
final int k = getK();
setN(0);
setMinK(k);
@@ -202,14 +202,14 @@ int getNumLevels() {

@Override
void incN() {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
long n = getMemoryN(wmem);
setMemoryN(wmem, ++n);
}

@Override
void incNumLevels() {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
int numLevels = getMemoryNumLevels(wmem);
setMemoryNumLevels(wmem, ++numLevels);
}
@@ -221,95 +221,95 @@ boolean isLevelZeroSorted() {

@Override
void setFloatItemsArray(final float[] floatItems) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
itemsArrUpdatable.putFloatArray(0, floatItems, 0, floatItems.length);
}

@Override
void setFloatItemsArrayAt(final int index, final float value) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
itemsArrUpdatable.putFloat((long)index * Float.BYTES, value);
}

@Override
void setItemsArrayUpdatable(final WritableMemory itemsMem) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
itemsArrUpdatable = itemsMem;
}

@Override
void setLevelsArray(final int[] levelsArr) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
levelsArrUpdatable.putIntArray(0, levelsArr, 0, levelsArr.length);
}

@Override
void setLevelsArrayAt(final int index, final int value) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
levelsArrUpdatable.putInt((long)index * Integer.BYTES, value);
}

@Override
void setLevelsArrayAtMinusEq(final int index, final int minusEq) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
final int offset = index * Integer.BYTES;
final int curV = levelsArrUpdatable.getInt(offset);
levelsArrUpdatable.putInt(offset, curV - minusEq);
}

@Override
void setLevelsArrayAtPlusEq(final int index, final int plusEq) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
final int offset = index * Integer.BYTES;
final int curV = levelsArrUpdatable.getInt(offset);
levelsArrUpdatable.putInt(offset, curV + plusEq);
}

@Override
void setLevelsArrayUpdatable(final WritableMemory levelsMem) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
levelsArrUpdatable = levelsMem;
}

@Override
void setLevelZeroSorted(final boolean sorted) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
setMemoryLevelZeroSortedFlag(wmem, sorted);
}

@Override
void setMaxFloatValue(final float value) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
minMaxArrUpdatable.putFloat(Float.BYTES, value);
}

@Override
void setMinFloatValue(final float value) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
minMaxArrUpdatable.putFloat(0, value);
}

@Override
void setMinK(final int minK) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
setMemoryMinK(wmem, minK);
}

@Override
void setMinMaxArrayUpdatable(final WritableMemory minMaxMem) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
minMaxArrUpdatable = minMaxMem;
}

@Override
void setN(final long n) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
setMemoryN(wmem, n);
}

@Override
void setNumLevels(final int numLevels) {
if (!updatableMemory) { kllSketchThrow(TGT_IS_IMMUTABLE); }
if (!updatableMemory) { kllSketchThrow(TGT_IS_READ_ONLY); }
setMemoryNumLevels(wmem, numLevels);
}

@@ -129,18 +129,19 @@ static double[] getDoublesQuantiles(final KllSketch mine, final double[] fractio
static void mergeDoubleImpl(final KllSketch mine, final KllSketch other) {
if (other.isEmpty()) { return; }
final long finalN = mine.getN() + other.getN();
//update this sketch with level0 items from the other sketch
final double[] otherDoubleItemsArr = other.getDoubleItemsArray();
final int otherNumLevels = other.getNumLevels();
final int[] otherLevelsArr = other.getLevelsArray();
for (int i = otherLevelsArr[0]; i < otherLevelsArr[1]; i++) {
KllDoublesHelper.updateDouble(mine, otherDoubleItemsArr[i]);
}
// after the level 0 update, we capture the key mutable variables
//capture my min & max, minK
final double myMin = mine.getMinDoubleValue();
final double myMax = mine.getMaxDoubleValue();
final int myMinK = mine.getMinK();

//update this sketch with level0 items from the other sketch
for (int i = otherLevelsArr[0]; i < otherLevelsArr[1]; i++) {
KllDoublesHelper.updateDouble(mine, otherDoubleItemsArr[i]);
}
// after the level 0 update, we capture the state of levels and items arrays
final int myCurNumLevels = mine.getNumLevels();
final int[] myCurLevelsArr = mine.getLevelsArray();
final double[] myCurDoubleItemsArr = mine.getDoubleItemsArray();
@@ -260,7 +261,7 @@ static void mergeSortedDoubleArrays(
* @param start data start
* @param length items length
* @param random instance of Random
*/
*/ //NOTE Validation Method: Need to modify.
static void randomlyHalveDownDoubles(final double[] buf, final int start, final int length, final Random random) {
assert isEven(length);
final int half_length = length / 2;
@@ -279,7 +280,7 @@ static void randomlyHalveDownDoubles(final double[] buf, final int start, final
* @param start data start
* @param length items length
* @param random instance of Random
*/
*/ //NOTE Validation Method: Need to modify.
static void randomlyHalveUpDoubles(final double[] buf, final int start, final int length, final Random random) {
assert isEven(length);
final int half_length = length / 2;
@@ -543,14 +544,14 @@ private static void validateDoubleValues(final double[] values) {
* Validation Method.
* The following must be enabled for use with the KllDoublesValidationTest,
* which is only enabled for manual testing. In addition, two Validation Methods
* above need to be modified as commented.
*/
// static int nextOffset = 0;
* above need to be modified.
*/ //NOTE Validation Method: Need to uncomment
// static int nextOffset = 0;
//
// private static int deterministicOffset() {
// final int result = nextOffset;
// nextOffset = 1 - nextOffset;
// return result;
// }
// private static int deterministicOffset() {
// final int result = nextOffset;
// nextOffset = 1 - nextOffset;
// return result;
// }

}

0 comments on commit 1b54257

Please sign in to comment.