Skip to content

Commit

Permalink
ChaoSampling doesn't throw StreamOverflowException on Long.MAX_VALUE (g…
Browse files Browse the repository at this point in the history
  • Loading branch information
gstamatelat committed Jul 3, 2018
1 parent 7f07816 commit 5aa50b3
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions src/main/java/gr/james/sampling/ChaoSampling.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
* According to this algorithm, the probability of an item to be in the final sample is proportional to its relative
* weight. Weights are the range (0,+Inf), otherwise an {@link IllegalWeightException} is thrown.
* <p>
* This implementation throws {@link StreamOverflowException} if more than {@link Long#MAX_VALUE} are feeded or if the
* sum of the weights of the items feeded is {@link Double#POSITIVE_INFINITY}, whichever occurs first.
* This implementation throws {@link StreamOverflowException} if the sum of the weights of the items feeded is
* {@link Double#POSITIVE_INFINITY}.
* <p>
* The space complexity of this class is {@code O(k)}, where {@code k} is the sample size.
*
Expand Down Expand Up @@ -115,18 +115,14 @@ public static <E> WeightedRandomSamplingCollector<E> weightedCollector(int sampl
* @return {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws IllegalWeightException if {@code weight} is outside the range (0,+Inf)
* @throws StreamOverflowException if the number of items feeded exceeds {@link Long#MAX_VALUE} or if the sum of the
* weights of the items feeded is {@link Double#POSITIVE_INFINITY}
* @throws StreamOverflowException if the sum of the weights of the items feeded is {@link Double#POSITIVE_INFINITY}
*/
@Override
public boolean feed(T item, double weight) {
// Checks
if (item == null) {
throw new NullPointerException("Item was null");
}
if (streamSize == Long.MAX_VALUE) {
throw new StreamOverflowException();
}
if (weight <= 0) {
throw new IllegalWeightException("Weight was not positive, must be in (0,+Inf)");
}
Expand All @@ -136,7 +132,6 @@ public boolean feed(T item, double weight) {

// Increase stream size
this.streamSize++;
assert this.streamSize > 0;

// Increase weight sum
this.weightSum += weight;
Expand All @@ -146,7 +141,8 @@ public boolean feed(T item, double weight) {
assert this.weightSum > 0;

// The first k items go straight into the A list
if (streamSize <= sampleSize) {
if (this.impossible.size() + this.sample.size() < sampleSize) {
assert this.sample.isEmpty();
this.impossible.add(new Weighted<>(item, weight));
return true;
}
Expand Down Expand Up @@ -220,8 +216,8 @@ public boolean feed(T item, double weight) {
* @throws NullPointerException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
* @throws IllegalWeightException if {@code weight} is outside the range (0,+Inf)
* @throws StreamOverflowException if the number of items feeded exceeds {@link Long#MAX_VALUE} or if the sum of
* the weights of the items feeded is {@link Double#POSITIVE_INFINITY}
* @throws StreamOverflowException if the sum of the weights of the items feeded is
* {@link Double#POSITIVE_INFINITY}
*/
@Override
public boolean feed(Iterator<T> items, Iterator<Double> weights) {
Expand All @@ -235,8 +231,7 @@ public boolean feed(Iterator<T> items, Iterator<Double> weights) {
* @return {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws IllegalWeightException if {@code weight} is outside the range (0,+Inf)
* @throws StreamOverflowException if the number of items feeded exceeds {@link Long#MAX_VALUE} or if the sum of the
* weights of the items feeded is {@link Double#POSITIVE_INFINITY}
* @throws StreamOverflowException if the sum of the weights of the items feeded is {@link Double#POSITIVE_INFINITY}
*/
@Override
public boolean feed(Map<T, Double> items) {
Expand Down Expand Up @@ -265,16 +260,17 @@ public final int sampleSize() {
}

/**
* Get the number of items that have been feeded to the algorithm during the lifetime of this instance, which is a
* non-negative {@code long} value.
* Get the number of items that have been feeded to the algorithm during the lifetime of this instance.
* <p>
* If more than {@link Long#MAX_VALUE} items has been feeded to the instance, {@code streamSize()} will cycle the
* long values, continuing from {@link Long#MIN_VALUE}.
* <p>
* This method runs in constant time.
*
* @return the number of items that have been feeded to the algorithm
*/
@Override
public final long streamSize() {
assert this.streamSize >= 0;
return this.streamSize;
}

Expand All @@ -284,8 +280,7 @@ public final long streamSize() {
* @param item {@inheritDoc}
* @return {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws StreamOverflowException if the number of items feeded exceeds {@link Long#MAX_VALUE} or if the sum of the
* weights of the items feeded is {@link Double#POSITIVE_INFINITY}
* @throws StreamOverflowException if the sum of the weights of the items feeded is {@link Double#POSITIVE_INFINITY}
*/
@Override
public boolean feed(T item) {
Expand All @@ -298,8 +293,7 @@ public boolean feed(T item) {
* @param items {@inheritDoc}
* @return {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws StreamOverflowException if the number of items feeded exceeds {@link Long#MAX_VALUE} or if the sum of the
* weights of the items feeded is {@link Double#POSITIVE_INFINITY}
* @throws StreamOverflowException if the sum of the weights of the items feeded is {@link Double#POSITIVE_INFINITY}
*/
@Override
public boolean feed(Iterator<T> items) {
Expand All @@ -312,8 +306,7 @@ public boolean feed(Iterator<T> items) {
* @param items {@inheritDoc}
* @return {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws StreamOverflowException if the number of items feeded exceeds {@link Long#MAX_VALUE} or if the sum of the
* weights of the items feeded is {@link Double#POSITIVE_INFINITY}
* @throws StreamOverflowException if the sum of the weights of the items feeded is {@link Double#POSITIVE_INFINITY}
*/
@Override
public boolean feed(Iterable<T> items) {
Expand Down

0 comments on commit 5aa50b3

Please sign in to comment.