Skip to content

Commit

Permalink
Implement VitterXSkipFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
gstamatelat committed Jul 10, 2018
1 parent f177713 commit fb5ae91
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/main/java/gr/james/sampling/VitterXSampling.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,38 @@ long skipLength(long streamSize, int sampleSize, Random random) {

return skipCount;
}

@Deprecated
private static class VitterXSkipFunction implements SkipFunction {
private final int sampleSize;
private final Random random;
private long streamSize;

public VitterXSkipFunction(int sampleSize, Random random) {
this.sampleSize = sampleSize;
this.random = random;
this.streamSize = sampleSize;
}

@Override
public long skip() throws StreamOverflowException {
if (streamSize == Long.MAX_VALUE) {
throw new StreamOverflowException();
}

streamSize++;

final double r = random.nextDouble();
long skipCount = 0;

double quot = (streamSize - sampleSize) / (double) streamSize;
while (quot > r && streamSize > 0) {
skipCount++;
streamSize++;
quot = (quot * (streamSize - sampleSize)) / (double) streamSize;
}

return skipCount;
}
}
}

0 comments on commit fb5ae91

Please sign in to comment.