Skip to content

Commit

Permalink
Simplify AbstractRandomSampling.feed (#51)
Browse files Browse the repository at this point in the history
Remove nested conditional indentations.
  • Loading branch information
gstamatelat authored Sep 3, 2022
1 parent ad40d7f commit cd4c2c2
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/main/java/gr/james/sampling/AbstractRandomSampling.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,24 +94,26 @@ public boolean feed(T item) {
this.streamSize++;
assert this.streamSize > 0;

// Skip items and add to reservoir
// Fill the reservoir
if (sample.size() < sampleSize) {
sample.add(item);
assert sample.size() == Math.min(sampleSize(), streamSize());
return true;
} else {
assert sample.size() == sampleSize;
if (skip > 0) {
skip--;
return false;
} else {
assert skip == 0;
sample.set(random.nextInt(sampleSize), item);
skip = skipLength(streamSize, sampleSize, random);
assert this.skip >= 0;
return true;
}
}

// Skip items
assert sample.size() == sampleSize;
if (skip > 0) {
skip--;
return false;
}

// Accept and generate new skip
assert skip == 0;
sample.set(random.nextInt(sampleSize), item);
skip = skipLength(streamSize, sampleSize, random);
assert this.skip >= 0;
return true;
}

/**
Expand Down

0 comments on commit cd4c2c2

Please sign in to comment.