Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,15 @@ private static void compressWhileUpdatingSketch(final KllDoublesSketch dblSk) {
}

//assumes readOnly = false and UPDATABLE, called from KllDoublesSketch::merge
static void mergeDoubleImpl(final KllDoublesSketch mySketch,
final KllDoublesSketch otherDblSk) {
static void mergeDoubleImpl(final KllDoublesSketch mySketch, final KllDoublesSketch otherDblSk) {
if (otherDblSk.isEmpty()) { return; }

//capture my key mutable fields before doing any merging
final boolean myEmpty = mySketch.isEmpty();
final double myMin = myEmpty ? Double.NaN : mySketch.getMinItem();
final double myMax = myEmpty ? Double.NaN : mySketch.getMaxItem();
final int myMinK = mySketch.getMinK();
final long finalN = mySketch.getN() + otherDblSk.getN();
final long finalN = Math.addExact(mySketch.getN(), otherDblSk.getN());

//buffers that are referenced multiple times
final int otherNumLevels = otherDblSk.getNumLevels();
Expand Down Expand Up @@ -398,7 +397,7 @@ private static int[] generalDoublesCompress(
curLevel++; // start out at level 0

// If we are at the current top level, add an empty level above it for convenience,
// but do not increment numLevels until later
// but do not actually increment numLevels until later
if (curLevel == (numLevels - 1)) {
inLevels[curLevel + 2] = inLevels[curLevel + 1];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ public QuantilesDoublesSketchIterator iterator() {
@Override
public final void merge(final KllSketch other) {
if (readOnly || sketchStructure != UPDATABLE) { throw new SketchesArgumentException(TGT_IS_READ_ONLY_MSG); }
final KllDoublesSketch othDblSk = (KllDoublesSketch)other; //check cast first
if (this == other) { throw new SketchesArgumentException(SELF_MERGE_MSG); }
final KllDoublesSketch othDblSk = (KllDoublesSketch)other;
if (othDblSk.isEmpty()) { return; } //then check empty
KllDoublesHelper.mergeDoubleImpl(this, othDblSk);
kllDoublesSV = null;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/apache/datasketches/kll/KllHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,9 @@ static int getNumRetainedAboveLevelZero(final int numLevels, final int[] levels)
* @return the capacity of a specific level
*/
static int levelCapacity(final int k, final int numLevels, final int level, final int m) {
assert (k <= (1 << 29));
assert (numLevels >= 1) && (numLevels <= 61);
assert (level >= 0) && (level < numLevels);
assert (k <= (1 << 29)) : "The given k is > 2^29.";
assert (numLevels >= 1) && (numLevels <= 61) : "The given numLevels is < 1 or > 61";
assert (level >= 0) && (level < numLevels) : "The given level is < 0 or >= numLevels.";
final int depth = numLevels - level - 1; //depth is # levels from the top level (= 0)
return (int) Math.max(m, intCapAux(k, depth));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ public interface QuantilesAPI {
static String NOT_SINGLE_ITEM_MSG = "Sketch does not have just one item. ";
static String MEM_REQ_SVR_NULL_MSG = "MemoryRequestServer must not be null. ";
static String TGT_IS_READ_ONLY_MSG = "Target sketch is Read Only, cannot write. ";
static String SELF_MERGE_MSG = "A sketch cannot merge with itself. ";

/**
* Gets the user configured parameter k, which controls the accuracy of the sketch
Expand Down
81 changes: 73 additions & 8 deletions src/test/java/org/apache/datasketches/common/Shuffle.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,19 @@ public final class Shuffle {
private static final Random rand = new Random();

/**
* Shuffle the given input float array
* @param array input array
* Shuffle the given input array using a default source of randomness.
* @param array the array to be shuffled.
*/
public static void shuffle(final float[] array) {
shuffle(array, rand);
}

/**
* Shuffle the given input array using the given source of randomness.
* @param array the array to be shuffled.
* @param rand the source of randomness used to shuffle the list.
*/
public static void shuffle(final float[] array, final Random rand) {
final int arrLen = array.length;
for (int i = 0; i < arrLen; i++) {
final int r = rand.nextInt(i + 1);
Expand All @@ -46,10 +55,19 @@ private static void swap(final float[] array, final int i1, final int i2) {
}

/**
* Shuffle the given input double array
* @param array input array
* Shuffle the given input array using a default source of randomness.
* @param array the array to be shuffled.
*/
public static void shuffle(final double[] array) {
shuffle(array, rand);
}

/**
* Shuffle the given input array using the given source of randomness.
* @param array the array to be shuffled.
* @param rand the source of randomness used to shuffle the list.
*/
public static void shuffle(final double[] array, final Random rand) {
final int arrLen = array.length;
for (int i = 0; i < arrLen; i++) {
final int r = rand.nextInt(i + 1);
Expand All @@ -64,10 +82,19 @@ private static void swap(final double[] array, final int i1, final int i2) {
}

/**
* Shuffle the given input long array
* @param array input array
* Shuffle the given input array using a default source of randomness.
* @param array the array to be shuffled.
*/
public static void shuffle(final long[] array) {
shuffle(array, rand);
}

/**
* Shuffle the given input array using the given source of randomness.
* @param array the array to be shuffled.
* @param rand the source of randomness used to shuffle the list.
*/
public static void shuffle(final long[] array, final Random rand) {
final int arrLen = array.length;
for (int i = 0; i < arrLen; i++) {
final int r = rand.nextInt(i + 1);
Expand All @@ -82,10 +109,19 @@ private static void swap(final long[] array, final int i1, final int i2) {
}

/**
* Shuffle the given input int array
* @param array input array
* Shuffle the given input array using a default source of randomness.
* @param array the array to be shuffled.
*/
public static void shuffle(final int[] array) {
shuffle(array, rand);
}

/**
* Shuffle the given input array using the given source of randomness.
* @param array the array to be shuffled.
* @param rand the source of randomness used to shuffle the list.
*/
public static void shuffle(final int[] array, final Random rand) {
final int arrLen = array.length;
for (int i = 0; i < arrLen; i++) {
final int r = rand.nextInt(i + 1);
Expand All @@ -99,4 +135,33 @@ private static void swap(final int[] array, final int i1, final int i2) {
array[i2] = value;
}

/**
* Shuffle the given input array using a default source of randomness.
* @param array the array to be shuffled.
* @param <T> the component type of the given array.
*/
public static <T> void shuffle(final T[] array) {
shuffle(array, rand);
}

/**
* Shuffle the given input array using the given source of randomness.
* @param array the array to be shuffled.
* @param rand the source of randomness used to shuffle the list.
* @param <T> the component type of the given array.
*/
public static <T> void shuffle(final T[] array, final Random rand) {
final int arrLen = array.length;
for (int i = 0; i < arrLen; i++) {
final int r = rand.nextInt(i + 1);
swap(array, i, r);
}
}

private static<T> void swap(final T[] array, final int i1, final int i2) {
final T value = array[i1];
array[i1] = array[i2];
array[i2] = value;
}

}