Skip to content

Commit

Permalink
AbstractOrderSampling comparison based on compareTo (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
gstamatelat committed Aug 27, 2022
1 parent e7a190d commit 08ba34b
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/main/java/gr/james/sampling/AbstractOrderSampling.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,18 @@ public final boolean feed(T item, double weight) {
// Calculate item weight
final Weighted<T> newItem = new Weighted<>(item, this.key(weight, random));

// Add item to reservoir
// Add if reservoir is not full
if (pq.size() < sampleSize) {
pq.add(newItem);
return true;
} else if (pq.peek().weight < newItem.weight) {
// Seems unfair for equal weight items to not have a chance to get in the sample
// Of course in the long run it hardly matters
assert pq.size() == sampleSize();
}

// Add if key is high
assert pq.size() == sampleSize();
final Weighted<T> least = pq.peek();
assert least != null;
assert least.compareTo(newItem) != 0;
if (least.compareTo(newItem) < 0) {
pq.poll();
pq.add(newItem);
return true;
Expand Down

0 comments on commit 08ba34b

Please sign in to comment.