You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datasketches.apache.org by le...@apache.org on 2020/08/29 00:02:08 UTC
[incubator-datasketches-java] branch RelativeErrorQuantiles
updated: Added binary search algo to improve getRank() and getQuantile()
searches.
This is an automated email from the ASF dual-hosted git repository.
leerho pushed a commit to branch RelativeErrorQuantiles
in repository https://gitbox.apache.org/repos/asf/incubator-datasketches-java.git
The following commit(s) were added to refs/heads/RelativeErrorQuantiles by this push:
new c6e333a Added binary search algo to improve getRank() and getQuantile() searches.
c6e333a is described below
commit c6e333ac35262862924ef890db6889e113b67c9a
Author: Lee Rhodes <le...@users.noreply.github.com>
AuthorDate: Fri Aug 28 17:01:31 2020 -0700
Added binary search algo to improve getRank() and getQuantile()
searches.
---
.../org/apache/datasketches/req/FloatBuffer.java | 26 ++--
.../org/apache/datasketches/req/ReqAuxiliary.java | 32 +++--
.../org/apache/datasketches/req/ReqCompactor.java | 152 ++++++++++++---------
.../org/apache/datasketches/req/ReqSketch.java | 32 +++--
.../apache/datasketches/req/ReqAuxiliaryTest.java | 90 ++++++++++++
.../org/apache/datasketches/req/ReqSketchTest.java | 80 ++++++-----
6 files changed, 272 insertions(+), 140 deletions(-)
diff --git a/src/main/java/org/apache/datasketches/req/FloatBuffer.java b/src/main/java/org/apache/datasketches/req/FloatBuffer.java
index 51b5aba..1e434f0 100755
--- a/src/main/java/org/apache/datasketches/req/FloatBuffer.java
+++ b/src/main/java/org/apache/datasketches/req/FloatBuffer.java
@@ -16,6 +16,7 @@ import org.apache.datasketches.SketchesArgumentException;
*/
class FloatBuffer {
static final String LS = System.getProperty("line.separator");
+ static final String TAB = "\t";
private float[] arr_;
private int count_;
private int delta_;
@@ -75,18 +76,9 @@ class FloatBuffer {
* @return count of items less-than the given value.
*/
int countLessThan(final float value) {
- int num = 0;
- if (sorted_) {
- for (int i = 0; i < count_; i++) {
- if (arr_[i] < value) { num++; }
- else { break; }
- }
- } else {
- for (int i = 0; i < count_; i++) {
- if (arr_[i] < value) { num++; }
- }
- }
- return num;
+ if (!sorted_) { sort(); }
+ final int index = ReqAuxiliary.binarySearch(arr_, 0, count_ - 1, value);
+ return (index == -1) ? 0 : index + 1;
}
/**
@@ -279,14 +271,18 @@ class FloatBuffer {
/**
* Returns a printable formatted string of the values of this buffer separated by a single space.
- * @param decimals The desired precision after the decimal point
+ * @param fmt The format for each printed item.
+ * @param width the number of items to print per line
+ * @param indent the number of spaces at the beginning of a new line
* @return a printable, formatted string of the values of this buffer.
*/
- String toHorizList(final int decimals) {
+ String toHorizList(final String fmt, final int width, final int indent) {
final StringBuilder sb = new StringBuilder();
- final String fmt = " %." + decimals + "f";
+ final char[] spaces = new char[indent];
+ Arrays.fill(spaces, ' ');
for (int i = 0; i < count_; i++) {
final String str = String.format(fmt, arr_[i]);
+ if ((i > 0) && ((i % width) == 0)) { sb.append(LS).append(spaces); }
sb.append(str);
}
return sb.toString();
diff --git a/src/main/java/org/apache/datasketches/req/ReqAuxiliary.java b/src/main/java/org/apache/datasketches/req/ReqAuxiliary.java
index 5be096a..8926658 100644
--- a/src/main/java/org/apache/datasketches/req/ReqAuxiliary.java
+++ b/src/main/java/org/apache/datasketches/req/ReqAuxiliary.java
@@ -69,7 +69,8 @@ class ReqAuxiliary {
if (!init) {
throw new SketchesStateException("Aux structure not initialized.");
}
- final int index = binarySearch(normRanks, normRank);
+ final int len = normRanks.length;
+ final int index = binarySearch(normRanks, 0, len - 1, normRank);
if (index == -1) { return Float.NaN; }
return items[index];
}
@@ -116,32 +117,33 @@ class ReqAuxiliary {
}
/**
- * Binary Search for the index of the highest value that is strictly less than the key.
- * If -1 is returned there are no values in the array that are strictly less than the key.
+ * Binary Search for the index of the highest value in the given range that is strictly less-than
+ * the key. If -1 is returned there are no values in the range that are strictly less than the key.
* If there are duplicates in the array and the key is one of those values, the index returned
* will be the index of the next lower value prior to the sequence of duplicates.
* @param arr the given array that must be sorted.
+ * @param low the index of the lowest value in the range
+ * @param high the index of the highest value in the range
* @param key the value to search for.
- * @return the index of the highest value that is strictly less than the key
+ * @return the index of the highest value in the given range that is strictly less than the key
*/
- static int binarySearch(final float[] arr, final float key) {
- int lo = 0;
- int mid = 0;
- int hi = arr.length - 1;
+ static int binarySearch(final float[] arr, final int low, final int high, final float key) {
+ int lo = low;
+ int mid = lo;
+ int hi = high;
while (lo <= hi) {
mid = lo + ((hi - lo) / 2);
if (key < arr[mid]) { hi = mid - 1; }
else if (key > arr[mid]) { lo = mid + 1; }
else {
- //handle duplicates, if any
- while ((mid > 0) && (arr[mid - 1] == arr[mid])) { mid--; }
- return mid;
+ //println("\nFound: lo: " + lo + " mid: " + mid + " hi: " + hi);
+ while ((mid > lo) && (arr[mid - 1] == arr[mid])) { --mid; }
+ return mid <= low ? -1 : --mid;
}
}
- //exact value not found
- if (mid == hi) { return mid; }
- if (lo == mid) { return hi; }
- return -1; //never actually reached
+ //println("\nNot Found: lo: " + lo + " mid: " + mid + " hi: " + hi);
+ return (hi < low) ? -1 : hi;
}
+ //static final void println(final Object o) { System.out.println(o.toString()); }
}
diff --git a/src/main/java/org/apache/datasketches/req/ReqCompactor.java b/src/main/java/org/apache/datasketches/req/ReqCompactor.java
index f5aded9..df980c8 100644
--- a/src/main/java/org/apache/datasketches/req/ReqCompactor.java
+++ b/src/main/java/org/apache/datasketches/req/ReqCompactor.java
@@ -22,6 +22,7 @@ package org.apache.datasketches.req;
import static java.lang.Math.round;
import static org.apache.datasketches.Util.numberOfTrailingOnes;
import static org.apache.datasketches.req.FloatBuffer.LS;
+import static org.apache.datasketches.req.FloatBuffer.TAB;
import static org.apache.datasketches.req.ReqSketch.INIT_NUMBER_OF_SECTIONS;
import static org.apache.datasketches.req.ReqSketch.MIN_K;
import static org.apache.datasketches.req.ReqSketch.print;
@@ -80,8 +81,8 @@ public class ReqCompactor {
private void printNewCompactor() {
println(" New Compactor: height: " + lgWeight
- + "\tsectionSize: " + sectionSize
- + "\tnumSections: " + numSections + LS);
+ + TAB + "sectionSize: " + sectionSize
+ + TAB + "numSections: " + numSections + LS);
}
/**
@@ -120,7 +121,7 @@ public class ReqCompactor {
final int count = buf.getItemCount();
if (debug) { printCompactingStart(); }
if (!buf.isSorted()) { buf.sort(); } //Footnote 1
- if (debug) { print(" "); print(toHorizontalList(0)); } //#decimals
+ if (debug) { print(" "); print(toHorizontalList("%4.0f", 24, 14)); } //#decimals
//choose a part of the buffer to compact
final int secsToCompact = numberOfTrailingOnes(state) + 1;
@@ -142,7 +143,7 @@ public class ReqCompactor {
if (numCompactions >= (1 << (numSections - 1))) {
adjustSectSizeNumSect(); //see Footnotes 3, 4 and 8
- printAdjSecSizeNumSec();
+ if (debug) { printAdjSecSizeNumSec(); }
}
if (debug) { printCompactionDone(); }
@@ -150,51 +151,6 @@ public class ReqCompactor {
return promote;
} //End Compact
- private int computeCompactionStart(final int secsToCompact) {
- int s = (getNomCapacity() / 2) + ((numSections - secsToCompact) * sectionSize);
- return (((buf.getItemCount() - s) & 1) == 1) ? ++s : s;
- }
-
- private void printAdjSecSizeNumSec() {
- final StringBuilder sb = new StringBuilder();
- sb.append(" ");
- sb.append("Adjust: SectionSize: ").append(sectionSize);
- sb.append(" NumSections: ").append(numSections);
- println(sb.toString());
- }
-
- private void printCompactingStart() {
- final StringBuilder sb = new StringBuilder();
- sb.append(" ");
- sb.append("Compacting[").append(lgWeight).append("] ");
- sb.append("NomCapacity: ").append(getNomCapacity());
- sb.append("\tSectionSize: ").append(sectionSize);
- sb.append("\tNumSections: ").append(numSections);
- sb.append("\tState(bin): ").append(Integer.toBinaryString(state));
- println(sb.toString());
- }
-
- private void printCompactionDetail(final int compactionStart, final int secsToCompact,
- final int promoteLen) { //Footnote 7
- final StringBuilder sb = new StringBuilder();
- final int count = buf.getItemCount();
- sb.append(" ");
- sb.append("SecsToCompact: ").append(secsToCompact);
- sb.append("\tNoCompact: ").append(compactionStart);
- sb.append("\tDoCompact: ").append(count - compactionStart).append(LS);
- final int delete = count - compactionStart;
- final String oddOrEven = (coin) ? "Odds" : "Evens";
- sb.append(" ");
- sb.append("Promote: ").append(promoteLen);
- sb.append("\tDelete: ").append(delete);
- sb.append("\tChoose: ").append(oddOrEven);
- println(sb.toString());
- }
-
- private void printCompactionDone() {
- println(" DONE: NumCompactions: " + numCompactions);
- }
-
/**
* Extends the given item array starting at length() by merging the items into
* the already sorted array.
@@ -255,15 +211,30 @@ public class ReqCompactor {
int getNumRetainedEntries() { return buf.getItemCount(); }
/**
+ * Returns true if this compactor is empty
+ * @return true if this compactor is empty
+ */
+ boolean isEmpty() {
+ return buf.isEmpty();
+ }
+
+ /**
+ * Returns true if this compactor is sorted
+ * @return true if this compactor is sorted
+ */
+ boolean isSorted() {
+ return buf.isSorted();
+ }
+
+ /**
* Merge the other given compactor into this one
* @param other the other given compactor
- * @param mergeSort if true, apply mergeSort algorithm instead of sort().
* @return this
*/
- ReqCompactor merge(final ReqCompactor other, final boolean mergeSort) {
+ ReqCompactor merge(final ReqCompactor other) {
state |= other.state;
numCompactions += other.numCompactions;
- if (mergeSort) { //assumes this and other is already sorted
+ if (isSorted() && other.isSorted()) {
final float[] arrIn = other.getBuffer().getArray();
buf.mergeSortIn(arrIn);
} else {
@@ -284,19 +255,6 @@ public class ReqCompactor {
}
/**
- * Returns a printable formatted string of the values of this buffer separated by a single space.
- * This string is prepended by the lgWeight and retained entries of this compactor.
- * @param decimals The desired precision after the decimal point
- * @return a printable, formatted string of the values of this buffer.
- */
- String toHorizontalList(final int decimals) {
- final int re = getNumRetainedEntries();
- final int h = getLgWeight();
- final String str = h + " [" + re + "]: " + buf.toHorizList(decimals) + LS;
- return str;
- }
-
- /**
* Gets the non-normalized rank of the given value.
* This is equal to the number of values in
* this compactor that are < the given value.
@@ -317,6 +275,23 @@ public class ReqCompactor {
}
/**
+ * Returns a printable formatted string of the values of this buffer separated by a single space.
+ * This string is prepended by the lgWeight and retained entries of this compactor.
+ * @param fmt The format for each printed item.
+ * @param width the number of items to print per line
+ * @param indent the number of spaces at the beginning of a new line
+ * @return a printable, formatted string of the values of this buffer.
+ */
+ String toHorizontalList(final String fmt, final int width, final int indent) {
+ final int re = getNumRetainedEntries();
+ final int h = getLgWeight();
+ final String prefix = String.format("%2d [%3d]: ", h, re);
+ final String str = prefix + buf.toHorizList(fmt, width, indent) + LS;
+ return str;
+ }
+
+
+ /**
* This adjusts sectionSize and numSections and guarantees that the sectionSize
* will always be even and >= minK.
*/
@@ -329,6 +304,53 @@ public class ReqCompactor {
numSections <<= 1;
}
+ private int computeCompactionStart(final int secsToCompact) {
+ int s = (getNomCapacity() / 2) + ((numSections - secsToCompact) * sectionSize);
+ return (((buf.getItemCount() - s) & 1) == 1) ? ++s : s;
+ }
+
+ //debug print functions
+
+ private void printAdjSecSizeNumSec() {
+ final StringBuilder sb = new StringBuilder();
+ sb.append(" ");
+ sb.append("Adjust: SectionSize: ").append(sectionSize);
+ sb.append(" NumSections: ").append(numSections);
+ println(sb.toString());
+ }
+
+ private void printCompactingStart() {
+ final StringBuilder sb = new StringBuilder();
+ sb.append(" ");
+ sb.append("Compacting[").append(lgWeight).append("] ");
+ sb.append("NomCapacity: ").append(getNomCapacity());
+ sb.append(TAB + "SectionSize: ").append(sectionSize);
+ sb.append(TAB + "NumSections: ").append(numSections);
+ sb.append(TAB + "State(bin): ").append(Integer.toBinaryString(state));
+ println(sb.toString());
+ }
+
+ private void printCompactionDetail(final int compactionStart, final int secsToCompact,
+ final int promoteLen) { //Footnote 7
+ final StringBuilder sb = new StringBuilder();
+ final int count = buf.getItemCount();
+ sb.append(" ");
+ sb.append("SecsToCompact: ").append(secsToCompact);
+ sb.append(TAB + "NoCompact: ").append(compactionStart);
+ sb.append(TAB + "DoCompact: ").append(count - compactionStart).append(LS);
+ final int delete = count - compactionStart;
+ final String oddOrEven = (coin) ? "Odds" : "Evens";
+ sb.append(" ");
+ sb.append("Promote: ").append(promoteLen);
+ sb.append(TAB + "Delete: ").append(delete);
+ sb.append(TAB + "Choose: ").append(oddOrEven);
+ println(sb.toString());
+ }
+
+ private void printCompactionDone() {
+ println(" DONE: NumCompactions: " + numCompactions);
+ }
+
/* Footnotes:
* 1. Sort the items in the buffer; use self.sort(reverse=True) for a better accuracy for
* higher-ranked items; TODO: test this reversed order.
diff --git a/src/main/java/org/apache/datasketches/req/ReqSketch.java b/src/main/java/org/apache/datasketches/req/ReqSketch.java
index 1c8bc6b..f5284aa 100644
--- a/src/main/java/org/apache/datasketches/req/ReqSketch.java
+++ b/src/main/java/org/apache/datasketches/req/ReqSketch.java
@@ -21,6 +21,7 @@ package org.apache.datasketches.req;
import static java.lang.Math.max;
import static org.apache.datasketches.req.FloatBuffer.LS;
+import static org.apache.datasketches.req.FloatBuffer.TAB;
import java.util.ArrayList;
import java.util.List;
@@ -181,8 +182,8 @@ public class ReqSketch {
*/
public float getQuantile(final float normRank) {
if ((normRank < 0) || (normRank > 1.0)) {
- throw new SketchesArgumentException("Normalized rank must be in the range [0, 1.0]: "
- + normRank);
+ throw new SketchesArgumentException(
+ "Normalized rank must be in the range [0, 1.0]: " + normRank);
}
if (normRank == 0.0) { return minValue; }
if (normRank == 1.0) { return maxValue; }
@@ -254,8 +255,8 @@ public class ReqSketch {
while (getNumLevels() < other.getNumLevels()) { grow(); }
//Append the items in same height compactors
for (int i = 0; i < getNumLevels(); i++) {
- final boolean mergeSort = i > 0;
- compactors.get(i).merge(other.compactors.get(i), mergeSort);
+ final ReqCompactor c = compactors.get(i);
+ compactors.get(i).merge(other.compactors.get(i));
}
updateRetainedItems();
// After merging, we should not be lazy when compressing the sketch (as the maxNomSize bound may
@@ -277,17 +278,19 @@ public class ReqSketch {
int nnRank = 0;
for (int i = 0; i < getNumLevels(); i++) {
final ReqCompactor c = compactors.get(i);
- nnRank += c.rank(value) * (1 << c.getLgWeight());
+ final int count = c.rank(value);
+ nnRank += count * (1 << c.getLgWeight());
}
return (float)nnRank / totalN;
}
/**
* Returns a summary of the sketch and the horizontal lists for all compactors.
- * @param decimals number of digits after the decimal point
+ * @param fmt The format for each printed item.
+ * @param dataDetail show all the retained data from all the compactors.
* @return a summary of the sketch and the horizontal lists for all compactors.
*/
- public String toString(final int decimals) {
+ public String toString(final String fmt, final boolean dataDetail) {
final StringBuilder sb = new StringBuilder();
sb.append("**********Relative Error Quantiles Sketch Summary**********").append(LS);
final int numC = compactors.size();
@@ -298,11 +301,13 @@ public class ReqSketch {
sb.append(" Max Value : " + maxValue).append(LS);
sb.append(" Levels : " + compactors.size()).append(LS);
- for (int i = 0; i < numC; i++) {
- final ReqCompactor c = compactors.get(i);
- sb.append(" " + c.toHorizontalList(decimals));
+ if (dataDetail) {
+ for (int i = 0; i < numC; i++) {
+ final ReqCompactor c = compactors.get(i);
+ sb.append(" " + c.toHorizontalList(fmt, 24, 12));
+ }
}
- sb.append("************************End Summary************************").append(LS);
+ sb.append("************************End Summary************************").append(LS + LS);
return sb.toString();
}
@@ -368,10 +373,9 @@ private void printStartCompress() {
private void printAllHorizList() {
for (int h = 0; h < compactors.size(); h++) {
final ReqCompactor c = compactors.get(h);
- print(c.toHorizontalList(0));
+ print(c.toHorizontalList("%4.0f", 24, 10));
}
- println("COMPRESS: DONE: sKsize: " + size
- + "\tMaxNomSize: " + maxNomSize + LS);
+ println("COMPRESS: DONE: sKsize: " + size + TAB + "MaxNomSize: " + maxNomSize + LS);
}
//temporary
diff --git a/src/test/java/org/apache/datasketches/req/ReqAuxiliaryTest.java b/src/test/java/org/apache/datasketches/req/ReqAuxiliaryTest.java
new file mode 100644
index 0000000..0bafac7
--- /dev/null
+++ b/src/test/java/org/apache/datasketches/req/ReqAuxiliaryTest.java
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.req;
+
+import static org.apache.datasketches.req.ReqAuxiliary.binarySearch;
+import static org.testng.Assert.assertEquals;
+
+import org.testng.annotations.Test;
+
+
+/**
+ * @author Lee Rhodes
+ */
+@SuppressWarnings("javadoc")
+public class ReqAuxiliaryTest {
+
+ @Test
+ public void checkBinSearch() {
+ // index {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12};
+ // range | |
+ float[] arr1 = {1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6};
+ assertEquals(binarySearch(arr1, 0, 12, 0.5f), -1);
+ assertEquals(binarySearch(arr1, 0, 12, 1.5f), 1);
+ assertEquals(binarySearch(arr1, 0, 12, 2.5f), 3);
+ assertEquals(binarySearch(arr1, 0, 12, 3.5f), 5);
+ assertEquals(binarySearch(arr1, 0, 12, 4.5f), 7);
+ assertEquals(binarySearch(arr1, 0, 12, 5.5f), 9);
+ assertEquals(binarySearch(arr1, 0, 12, 6.5f), 12);
+ assertEquals(binarySearch(arr1, 0, 12, 1f), -1);
+ assertEquals(binarySearch(arr1, 0, 12, 2f), 1);
+ assertEquals(binarySearch(arr1, 0, 12, 3f), 3);
+ assertEquals(binarySearch(arr1, 0, 12, 4f), 5);
+ assertEquals(binarySearch(arr1, 0, 12, 5f), 7);
+ assertEquals(binarySearch(arr1, 0, 12, 6f), 9);
+ assertEquals(binarySearch(arr1, 0, 12, 7f), 12);
+
+ //Check internal range:
+ assertEquals(binarySearch(arr1, 3, 11, 0.5f), -1);
+ assertEquals(binarySearch(arr1, 3, 11, 1.5f), -1);
+ assertEquals(binarySearch(arr1, 3, 11, 2.5f), 3);
+ assertEquals(binarySearch(arr1, 3, 11, 3.5f), 5);
+ assertEquals(binarySearch(arr1, 3, 11, 4.5f), 7);
+ assertEquals(binarySearch(arr1, 3, 11, 5.5f), 9);
+ assertEquals(binarySearch(arr1, 3, 11, 6.5f), 11);
+ assertEquals(binarySearch(arr1, 3, 11, 1f), -1);
+ assertEquals(binarySearch(arr1, 3, 11, 2f), -1);
+ assertEquals(binarySearch(arr1, 3, 11, 3f), 3);
+ assertEquals(binarySearch(arr1, 3, 11, 4f), 5);
+ assertEquals(binarySearch(arr1, 3, 11, 5f), 7);
+ assertEquals(binarySearch(arr1, 3, 11, 6f), 9);
+ assertEquals(binarySearch(arr1, 3, 11, 7f), 11);
+ }
+
+ @Test
+ public void checkBinSearch2() {
+ // index { 0, 1, 2, 3, 4, 5, 6};
+ // range | |
+ float[] arr1 = {178, 182, 186, 190, 194, 197, 200};
+ assertEquals(binarySearch(arr1, 0, 6, 170f), -1);
+ assertEquals(binarySearch(arr1, 0, 5, 170f), -1);
+ assertEquals(binarySearch(arr1, 0, 6, 180f), 0);
+ assertEquals(binarySearch(arr1, 0, 5, 180f), 0);
+ }
+
+
+ static final void printf(final String format, final Object ...args) {
+ System.out.printf(format, args);
+ }
+
+ static final void print(final Object o) { System.out.print(o.toString()); }
+
+ static final void println(final Object o) { System.out.println(o.toString()); }
+}
diff --git a/src/test/java/org/apache/datasketches/req/ReqSketchTest.java b/src/test/java/org/apache/datasketches/req/ReqSketchTest.java
index 3222543..07599de 100644
--- a/src/test/java/org/apache/datasketches/req/ReqSketchTest.java
+++ b/src/test/java/org/apache/datasketches/req/ReqSketchTest.java
@@ -20,58 +20,88 @@
package org.apache.datasketches.req;
import static org.apache.datasketches.QuantilesHelper.getEvenlySpacedRanks;
+import static org.apache.datasketches.req.FloatBuffer.LS;
+//import static org.apache.datasketches.req.FloatBuffer.TAB;
import org.apache.datasketches.req.ReqAuxiliary.Row;
import org.testng.annotations.Test;
/**
* @author Lee Rhodes
*/
-@SuppressWarnings("javadoc")
+@SuppressWarnings({"javadoc", "unused"})
public class ReqSketchTest {
-
@Test
public void test1() {
- ReqSketch sk = new ReqSketch(6, true); //w debug
- int max = 200;
int min = 1;
- boolean up = false;
+ int max = 200;
+ ReqSketch sk = loadSketch(min, max, true, 6, true); //up, debug
- if (up) {
- for (int i = min; i <= max; i++) {
- sk.update(i);
- }
- } else { //down
- for (int i = max + 1; i-- > min; ) {
- sk.update(i);
- }
- }
- print(sk.toString(0));
+ checkToString(sk);
+ checkGetRank(sk, min, max);
+ checkAux(sk);
+ checkGetQuantiles(sk);
+ }
+ private static void checkToString(ReqSketch sk) {
+ println("ToString() Test");
+ print(sk.toString("%4.0f", true)); //print dataDetail
+ }
+
+ private static void checkGetRank(ReqSketch sk, int min, int max) {
+ println(LS + "GetRank Test:");
double[] ranks = getEvenlySpacedRanks(11);
- println("Ranks Test:");
+ String dfmt = "%10.2f%10.6f" + LS;
+ String sfmt = "%10s%10s";
+ printf(sfmt, "Value", "Rank");
for (int i = 0; i < ranks.length; i++) {
- printRank(sk, ((float)ranks[i] * (max - min)) + min);
+ float rank = (float) ranks[i];
+ float scaledV = (rank * (max - min)) + min;
+ double r = sk.getRank(scaledV);
+ printf(dfmt, scaledV, r);
}
+ }
+ private static void checkAux(ReqSketch sk) {
+ println(LS + "Aux Test");
ReqAuxiliary aux = new ReqAuxiliary(sk);
aux.buildAuxTable(sk);
- String fmt = "%12.1f%12d%12.5f\n";
+ String dfmt = "%12.1f%12d%12.5f" + LS;
+ String sfmt = "%12s%12s%12s" + LS;
+ printf(sfmt, "Item", "Weight", "NormRank");
final int totalCount = sk.getRetainedEntries();
for (int i = 0; i < totalCount; i++) {
Row row = aux.getRow(i);
- printf(fmt, row.item, row.weight, row.normRank);
+ printf(dfmt, row.item, row.weight, row.normRank);
}
+ }
+ private static void checkGetQuantiles(ReqSketch sk) {
+ println(LS + "GetQuantiles() Test");
float[] rArr = {0, .1F, .2F, .3F, .4F, .5F, .6F, .7F, .8F, .9F, 1.0F};
float[] qOut = sk.getQuantiles(rArr);
for (int i = 0; i < qOut.length; i++) {
println("nRank: " + rArr[i] + ", q: " + qOut[i]);
}
+ }
+
+ private static ReqSketch loadSketch(int min, int max, boolean up, int k, boolean debug) {
+ ReqSketch sk = new ReqSketch(k, debug);
+ if (up) {
+ for (int i = min; i <= max; i++) {
+ sk.update(i);
+ }
+ } else { //down
+ for (int i = max + 1; i-- > min; ) {
+ sk.update(i);
+ }
+ }
+ return sk;
}
+
private static void printRank(ReqSketch sk, float v) {
double r = sk.getRank(v);
String rstr = String.format("%.2f", r);
@@ -79,18 +109,6 @@ public class ReqSketchTest {
println("Value: " + vstr + ", Rank: " + rstr);
}
- @Test
- public void strTest() {
- StringBuilder sb = new StringBuilder();
- float[] arr = {1, 2, 3};
- String fmt = " %.0f";
- for (int i = 0; i < arr.length; i++) {
- String str = String.format(fmt, arr[i]);
- sb.append(str);
- }
- println(sb.toString());
- }
-
static final void printf(final String format, final Object ...args) {
System.out.printf(format, args);
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org