Skip to content

Commit

Permalink
Merge branch 'master' of [email protected]:DataSketches/sketches-core.git
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee Rhodes committed Mar 4, 2016
2 parents e541397 + e3f0e5b commit c5719be
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
package com.yahoo.sketches.tuple;

import com.yahoo.sketches.BinomialBoundsN;

/**
* This is a base class for a specialized version of a tuple sketch, where an array of double values is associated with each key.
* A primitive array is used here, as opposed to a generic Summary object, for performance reasons.
Expand Down Expand Up @@ -54,7 +56,7 @@ public double getEstimate() {
*/
public double getUpperBound(final int numStdDev) {
if (!isEstimationMode()) return getRetainedEntries();
return Util.upperBound(getEstimate(), getTheta(), numStdDev);
return BinomialBoundsN.getUpperBound(getRetainedEntries(), getTheta(), numStdDev, isEmpty_);
}

/**
Expand All @@ -66,7 +68,7 @@ public double getUpperBound(final int numStdDev) {
*/
public double getLowerBound(final int numStdDev) {
if (!isEstimationMode()) return getRetainedEntries();
return Util.lowerBound(getEstimate(), getTheta(), numStdDev);
return BinomialBoundsN.getLowerBound(getRetainedEntries(), getTheta(), numStdDev, isEmpty_);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public abstract class ArrayOfDoublesUpdatableSketch extends ArrayOfDoublesSketch
* @param values The given values
*/
public void update(final long key, final double[] values) {
update(Util.longToLongArray(key), values);
update(new long[] {key}, values);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/yahoo/sketches/tuple/Sketch.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
package com.yahoo.sketches.tuple;

import com.yahoo.sketches.BinomialBoundsN;

/**
* This is an equivalent to com.yahoo.sketches.theta.Sketch with
* addition of a user-defined Summary object associated with every unique entry
Expand Down Expand Up @@ -37,7 +39,7 @@ public double getEstimate() {
*/
public double getUpperBound(final int numStdDev) {
if (!isEstimationMode()) return getRetainedEntries();
return Util.upperBound(getEstimate(), getTheta(), numStdDev);
return BinomialBoundsN.getUpperBound(getRetainedEntries(), getTheta(), numStdDev, isEmpty_);
}

/**
Expand All @@ -49,7 +51,7 @@ public double getUpperBound(final int numStdDev) {
*/
public double getLowerBound(final int numStdDev) {
if (!isEstimationMode()) return getRetainedEntries();
return Util.lowerBound(getEstimate(), getTheta(), numStdDev);
return BinomialBoundsN.getLowerBound(getRetainedEntries(), getTheta(), numStdDev, isEmpty_);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class UpdatableSketch<U, S extends UpdatableSummary<U>> extends QuickSele
* @param value The given U value
*/
public void update(final long key, final U value) {
update(Util.longToLongArray(key), value);
update(new long[] {key}, value);
}

/**
Expand Down
15 changes: 0 additions & 15 deletions src/main/java/com/yahoo/sketches/tuple/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,6 @@ static final int startingSubMultiple(final int lgTarget, final int lgResizeRatio
return lgStart;
}

static final double upperBound(final double estimate, final double theta, final double numStdDev) {
double dsq = numStdDev * numStdDev * ((1.0 / theta) - 1.0);
return estimate + (dsq / 2.0) + ((Math.sqrt(dsq) / 2.0) * Math.sqrt((4.0 * estimate) + dsq));
}

static final double lowerBound(final double estimate, final double theta, final double numStdDev) {
double dsq = numStdDev * numStdDev * ((1.0 / theta) - 1.0);
return estimate + (dsq / 2.0) - ((Math.sqrt(dsq) / 2.0) * Math.sqrt((4.0 * estimate) + dsq));
}

static final long[] longToLongArray(final long value) {
final long[] array = { value };
return array;
}

static final long[] doubleToLongArray(final double value) {
final double d = (value == 0.0) ? 0.0 : value; // canonicalize -0.0, 0.0
final long[] array = { Double.doubleToLongBits(d) }; // canonicalize all NaN forms
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,8 @@ public void estimationMode() {
for (int i = 1; i <= 8192; i++) sketch.update(i, new double[] {1.0});
Assert.assertTrue(sketch.isEstimationMode());
Assert.assertEquals(sketch.getEstimate(), 8192, 8192 * 0.01);
Assert.assertEquals(sketch.getEstimate(), sketch.getLowerBound(0));
Assert.assertEquals(sketch.getEstimate(), sketch.getUpperBound(0));
Assert.assertTrue(sketch.getEstimate() >= sketch.getLowerBound(1));
Assert.assertTrue(sketch.getEstimate() <= sketch.getUpperBound(1));
Assert.assertTrue(sketch.getEstimate() < sketch.getUpperBound(1));

double[][] values = sketch.getValues();
Assert.assertTrue(values.length >= 4096);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,8 @@ public void estimationMode() {
for (int i = 1; i <= 8192; i++) sketch.update(i, new double[] {1.0});
Assert.assertTrue(sketch.isEstimationMode());
Assert.assertEquals(sketch.getEstimate(), 8192, 8192 * 0.01);
Assert.assertEquals(sketch.getEstimate(), sketch.getLowerBound(0));
Assert.assertEquals(sketch.getEstimate(), sketch.getUpperBound(0));
Assert.assertTrue(sketch.getEstimate() >= sketch.getLowerBound(1));
Assert.assertTrue(sketch.getEstimate() <= sketch.getUpperBound(1));
Assert.assertTrue(sketch.getEstimate() < sketch.getUpperBound(1));
Assert.assertTrue(sketch.getRetainedEntries() > 4096);
sketch.trim();
Assert.assertEquals(sketch.getRetainedEntries(), 4096);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ public void estimationMode() {
for (int i = 1; i <= 8192; i++) sketch.update(i, 1.0);
Assert.assertTrue(sketch.isEstimationMode());
Assert.assertEquals(sketch.getEstimate(), 8192, 8192 * 0.01);
Assert.assertEquals(sketch.getEstimate(), sketch.getLowerBound(0));
Assert.assertEquals(sketch.getEstimate(), sketch.getUpperBound(0));
Assert.assertTrue(sketch.getEstimate() >= sketch.getLowerBound(1));
Assert.assertTrue(sketch.getEstimate() < sketch.getUpperBound(1));

Expand Down

0 comments on commit c5719be

Please sign in to comment.