Skip to content
Permalink
Browse files
Fixed the LGTM issues detected on the last push.
  • Loading branch information
leerho committed Mar 26, 2022
1 parent 5e3dd40 commit 7407207060210d9f046b74a91eaef25e16b8d384
Showing 15 changed files with 381 additions and 352 deletions.
@@ -60,10 +60,10 @@ public KllDirectDoublesSketch(final WritableMemory wmem, final MemoryRequestServ
* the maximum value.
* It is not necessary to include either the min or max values in these split points.
*
* @return an array of m+1 double values, which are a consecutive approximation to the CDF
* of the input stream given the splitPoints. The value at array position j of the returned
* CDF array is the sum of the returned values in positions 0 through j of the returned PMF
* array.
* @return an array of m+1 double values on the interval [0.0, 1.0) exclusive,
* which are a consecutive approximation to the CDF of the input stream given the splitPoints.
* The value at array position j of the returned CDF array is the sum of the returned values
* in positions 0 through j of the returned PMF array.
*/
public double[] getCDF(final double[] splitPoints) {
return getDoublesPmfOrCdf(splitPoints, true);
@@ -105,8 +105,9 @@ public double getMinValue() {
* the maximum value.
* It is not necessary to include either the min or max values in these split points.
*
* @return an array of m+1 doubles each of which is an approximation
* to the fraction of the input stream values (the mass) that fall into one of those intervals.
* @return an array of m+1 doubles on the interval [0.0, 1.0) exclusive,
* each of which is an approximation to the fraction of the total input stream values
* (the mass) that fall into one of those intervals.
* The definition of an "interval" is inclusive of the left splitPoint and exclusive of the right
* splitPoint, with the exception that the last interval will include maximum value.
*/
@@ -242,6 +243,11 @@ public String toString(final boolean withLevels, final boolean withData) {
return toStringImpl(withLevels, withData);
}

/**
* Updates this sketch with the given data item.
*
* @param value an item from a stream of items. NaNs are ignored.
*/
public void update(final double value) {
updateDouble(value);
}
@@ -60,10 +60,10 @@ public KllDirectFloatsSketch(final WritableMemory wmem, final MemoryRequestServe
* the maximum value.
* It is not necessary to include either the min or max values in these split points.
*
* @return an array of m+1 double values, which are a consecutive approximation to the CDF
* of the input stream given the splitPoints. The value at array position j of the returned
* CDF array is the sum of the returned values in positions 0 through j of the returned PMF
* array.
* @return an array of m+1 double values on the interval [0.0, 1.0) exclusive,
* which are a consecutive approximation to the CDF of the input stream given the splitPoints.
* The value at array position j of the returned CDF array is the sum of the returned values
* in positions 0 through j of the returned PMF array.
*/
public double[] getCDF(final float[] splitPoints) {
return getFloatsPmfOrCdf(splitPoints, true);
@@ -105,8 +105,9 @@ public float getMinValue() {
* the maximum value.
* It is not necessary to include either the min or max values in these split points.
*
* @return an array of m+1 doubles each of which is an approximation
* to the fraction of the input stream values (the mass) that fall into one of those intervals.
* @return an array of m+1 doubles on the interval [0.0, 1.0) exclusive,
* each of which is an approximation to the fraction of the total input stream values
* (the mass) that fall into one of those intervals.
* The definition of an "interval" is inclusive of the left splitPoint and exclusive of the right
* splitPoint, with the exception that the last interval will include maximum value.
*/
@@ -242,6 +243,11 @@ public String toString(final boolean withLevels, final boolean withData) {
return toStringImpl(withLevels, withData);
}

/**
* Updates this sketch with the given data item.
*
* @param value an item from a stream of items. NaNs are ignored.
*/
public void update(final float value) {
updateFloat(value);
}
@@ -35,10 +35,13 @@
import org.apache.datasketches.memory.MemoryRequestServer;
import org.apache.datasketches.memory.WritableMemory;


/**
* This class implements all the methods for the Direct (off-heap) sketches that are independent
* of the sketch type (float or double).
*/
abstract class KllDirectSketch extends KllSketch {
//All these members are constant for the life of this object. If the WritableMemory changes, it will require
//rebuilding this class
//All these members are constant for the life of this object. If the WritableMemory changes,
// it may require rebuilding this class
final Layout layout;
final boolean updatable;
WritableMemory levelsArrUpdatable;
@@ -69,28 +72,16 @@ public int getK() {
}

@Override
double getMaxDoubleValue() {
return minMaxArrUpdatable.getDouble(Double.BYTES);
}

@Override
float getMaxFloatValue() {
return minMaxArrUpdatable.getFloat(Float.BYTES);
}

@Override
double getMinDoubleValue() {
return minMaxArrUpdatable.getDouble(0);
}

@Override
float getMinFloatValue() {
return minMaxArrUpdatable.getFloat(0);
public long getN() {
return extractN(wmem);
}

@Override
public long getN() {
return extractN(wmem);
public byte[] toUpdatableByteArray() {
final int bytes = (int) wmem.getCapacity();
final byte[] byteArr = new byte[bytes];
wmem.getByteArray(0, byteArr, 0, bytes);
return byteArr;
}

@Override
@@ -105,7 +96,7 @@ public long getN() {
@Override
double getDoubleItemsArrayAt(final int index) {
if (sketchType == FLOATS_SKETCH) { return Double.NaN; }
return itemsArrUpdatable.getDouble(index * Double.BYTES);
return itemsArrUpdatable.getDouble((long)index * Double.BYTES);
}

@Override
@@ -125,7 +116,7 @@ float[] getFloatItemsArray() {
@Override
float getFloatItemsArrayAt(final int index) {
if (sketchType == DOUBLES_SKETCH) { return Float.NaN; }
return itemsArrUpdatable.getFloat(index * Float.BYTES);
return itemsArrUpdatable.getFloat((long)index * Float.BYTES);
}

int getItemsArrLengthItems() {
@@ -145,7 +136,27 @@ int[] getLevelsArray() {

@Override
int getLevelsArrayAt(final int index) {
return levelsArrUpdatable.getInt(index * Integer.BYTES);
return levelsArrUpdatable.getInt((long)index * Integer.BYTES);
}

@Override
double getMaxDoubleValue() {
return minMaxArrUpdatable.getDouble(Double.BYTES);
}

@Override
float getMaxFloatValue() {
return minMaxArrUpdatable.getFloat(Float.BYTES);
}

@Override
double getMinDoubleValue() {
return minMaxArrUpdatable.getDouble(0);
}

@Override
float getMinFloatValue() {
return minMaxArrUpdatable.getFloat(0);
}

@Override
@@ -180,7 +191,7 @@ void setDoubleItemsArray(final double[] doubleItems) {

@Override
void setDoubleItemsArrayAt(final int index, final double value) {
itemsArrUpdatable.putDouble(index * Double.BYTES, value);
itemsArrUpdatable.putDouble((long)index * Double.BYTES, value);
}

@Override
@@ -197,7 +208,7 @@ void setFloatItemsArray(final float[] floatItems) {

@Override
void setFloatItemsArrayAt(final int index, final float value) {
itemsArrUpdatable.putFloat(index * Float.BYTES, value);
itemsArrUpdatable.putFloat((long)index * Float.BYTES, value);
}

@Override
@@ -213,7 +224,7 @@ void setLevelsArray(final int[] levelsArr) {

@Override
void setLevelsArrayAt(final int index, final int value) {
levelsArrUpdatable.putInt(index * Integer.BYTES, value);
levelsArrUpdatable.putInt((long)index * Integer.BYTES, value);
}

@Override
@@ -276,19 +287,10 @@ void setN(final long n) {
insertN(wmem, n);
}


@Override
void setNumLevels(final int numLevels) {
if (!updatable) { kllSketchThrow(30); }
insertNumLevels(wmem, numLevels);
}

@Override
public byte[] toUpdatableByteArray() {
final int bytes = (int) wmem.getCapacity();
final byte[] byteArr = new byte[bytes];
wmem.getByteArray(0, byteArr, 0, bytes);
return byteArr;
}

}
@@ -34,54 +34,6 @@
*/
class KllDoublesHelper {

/**
* Checks the sequential validity of the given array of double values.
* They must be unique, monotonically increasing and not NaN.
* @param values the given array of values
*/
static void validateDoubleValues(final double[] values) {
for (int i = 0; i < values.length; i++) {
if (!Double.isFinite(values[i])) {
throw new SketchesArgumentException("Values must be finite");
}
if (i < values.length - 1 && values[i] >= values[i + 1]) {
throw new SketchesArgumentException(
"Values must be unique and monotonically increasing");
}
}
}

static void mergeSortedDoubleArrays(
final double[] bufA, final int startA, final int lenA,
final double[] bufB, final int startB, final int lenB,
final double[] bufC, final int startC) {
final int lenC = lenA + lenB;
final int limA = startA + lenA;
final int limB = startB + lenB;
final int limC = startC + lenC;

int a = startA;
int b = startB;

for (int c = startC; c < limC; c++) {
if (a == limA) {
bufC[c] = bufB[b];
b++;
} else if (b == limB) {
bufC[c] = bufA[a];
a++;
} else if (bufA[a] < bufB[b]) {
bufC[c] = bufA[a];
a++;
} else {
bufC[c] = bufB[b];
b++;
}
}
assert a == limA;
assert b == limB;
}

/**
* Compression algorithm used to merge higher levels.
* <p>Here is what we do for each level:</p>
@@ -193,20 +145,47 @@ static int[] generalDoublesCompress(
numLevels++;
targetItemCount += KllHelper.levelCapacity(k, numLevels, 0, m);
}

} // end of code for compacting a level

// determine whether we have processed all levels yet (including any new levels that we created)

if (curLevel == (numLevels - 1)) { doneYet = true; }

} // end of loop over levels

assert (outLevels[numLevels] - outLevels[0]) == currentItemCount;

return new int[] {numLevels, targetItemCount, currentItemCount};
}

static void mergeSortedDoubleArrays(
final double[] bufA, final int startA, final int lenA,
final double[] bufB, final int startB, final int lenB,
final double[] bufC, final int startC) {
final int lenC = lenA + lenB;
final int limA = startA + lenA;
final int limB = startB + lenB;
final int limC = startC + lenC;

int a = startA;
int b = startB;

for (int c = startC; c < limC; c++) {
if (a == limA) {
bufC[c] = bufB[b];
b++;
} else if (b == limB) {
bufC[c] = bufA[a];
a++;
} else if (bufA[a] < bufB[b]) {
bufC[c] = bufA[a];
a++;
} else {
bufC[c] = bufB[b];
b++;
}
}
assert a == limA;
assert b == limB;
}

//This must be modified for validation
static void randomlyHalveDownDoubles(final double[] buf, final int start, final int length, final Random random) {
assert isEven(length);
@@ -233,9 +212,26 @@ static void randomlyHalveUpDoubles(final double[] buf, final int start, final in
}
}

/**
* Checks the sequential validity of the given array of double values.
* They must be unique, monotonically increasing and not NaN.
* @param values the given array of values
*/
static void validateDoubleValues(final double[] values) {
for (int i = 0; i < values.length; i++) {
if (!Double.isFinite(values[i])) {
throw new SketchesArgumentException("Values must be finite");
}
if (i < values.length - 1 && values[i] >= values[i + 1]) {
throw new SketchesArgumentException(
"Values must be unique and monotonically increasing");
}
}
}

/*
* The following must be enabled for use with the KllDoublesValidationTest,
* which is only enabled for manual testing. In addition, the two methods
* which is only enabled for manual testing. In addition, two methods
* above need to be modified as commented.
*/

@@ -248,4 +244,3 @@ static void randomlyHalveUpDoubles(final double[] buf, final int start, final in
// }

}

0 comments on commit 7407207

Please sign in to comment.