Skip to content

Commit

Permalink
The feed method now returns boolean (gstamatelat#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
gstamatelat committed Jul 1, 2018
1 parent e76b145 commit 5994a68
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 268 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ Select 2 terms from a vocabulary, based on their weight.

```java
WeightedRandomSampling<String> rs = new EfraimidisSampling<>(2, new Random());
rs.feed("collection", 1)
.feed("algorithms", 2)
.feed("java", 2)
.feed("random", 3)
.feed("sampling", 4)
.feed("reservoir", 5);
rs.feed("collection", 1);
rs.feed("algorithms", 2);
rs.feed("java", 2);
rs.feed("random", 3);
rs.feed("sampling", 4);
rs.feed("reservoir", 5);
System.out.println(rs.sample());
```

Expand Down
12 changes: 6 additions & 6 deletions demo/src/SelectWeightedFromVocabulary.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
public final class SelectWeightedFromVocabulary {
public static void main(String[] args) {
WeightedRandomSampling<String> rs = new EfraimidisSampling<>(2, new Random());
rs.feed("collection", 1)
.feed("algorithms", 2)
.feed("java", 2)
.feed("random", 3)
.feed("sampling", 4)
.feed("reservoir", 5);
rs.feed("collection", 1);
rs.feed("algorithms", 2);
rs.feed("java", 2);
rs.feed("random", 3);
rs.feed("sampling", 4);
rs.feed("reservoir", 5);
System.out.println(rs.sample());
}
}
26 changes: 12 additions & 14 deletions src/main/java/gr/james/sampling/AbstractRandomSampling.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ abstract class AbstractRandomSampling<T> implements RandomSampling<T> {
* @throws StreamOverflowException if the number of items feeded exceeds {@link Long#MAX_VALUE}
*/
@Override
public RandomSampling<T> feed(T item) {
public final boolean feed(T item) {
// Checks
if (item == null) {
throw new NullPointerException("Item was null");
Expand All @@ -68,21 +68,21 @@ public RandomSampling<T> feed(T item) {
// Skip items and add to 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;
}
}

assert sample.size() == Math.min(sampleSize(), streamSize());
assert this.skip >= 0;

return this;
}

/**
Expand All @@ -91,12 +91,11 @@ public RandomSampling<T> feed(T item) {
* @param items {@inheritDoc}
* @return {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws StreamOverflowException {@inheritDoc}
* @throws StreamOverflowException if the number of items feeded exceeds {@link Long#MAX_VALUE}
*/
@Override
public RandomSampling<T> feed(Iterator<T> items) {
RandomSampling.super.feed(items);
return this;
public final boolean feed(Iterator<T> items) {
return RandomSampling.super.feed(items);
}

/**
Expand All @@ -105,12 +104,11 @@ public RandomSampling<T> feed(Iterator<T> items) {
* @param items {@inheritDoc}
* @return {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws StreamOverflowException {@inheritDoc}
* @throws StreamOverflowException if the number of items feeded exceeds {@link Long#MAX_VALUE}
*/
@Override
public RandomSampling<T> feed(Iterable<T> items) {
RandomSampling.super.feed(items);
return this;
public final boolean feed(Iterable<T> items) {
return RandomSampling.super.feed(items);
}

/**
Expand Down
53 changes: 27 additions & 26 deletions src/main/java/gr/james/sampling/ChaoSampling.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,11 @@ 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 {@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}
*/
@Override
public ChaoSampling<T> feed(T item, double weight) {
public boolean feed(T item, double weight) {
// Checks
if (item == null) {
throw new NullPointerException("Item was null");
Expand Down Expand Up @@ -144,7 +145,7 @@ public ChaoSampling<T> feed(T item, double weight) {
// The first k items go straight into the A list
if (streamSize <= sampleSize) {
this.impossible.add(new Weighted<>(item, weight));
return this;
return true;
}

// First order inclusion probability of the new item
Expand Down Expand Up @@ -204,7 +205,7 @@ public ChaoSampling<T> feed(T item, double weight) {

assert impossible.size() + sample.size() == sampleSize;

return this;
return w > add;
}

/**
Expand All @@ -215,13 +216,13 @@ public ChaoSampling<T> feed(T item, double weight) {
* @return {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
* @throws IllegalWeightException {@inheritDoc}
* @throws StreamOverflowException {@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}
*/
@Override
public ChaoSampling<T> feed(Iterator<T> items, Iterator<Double> weights) {
WeightedRandomSampling.super.feed(items, weights);
return this;
public boolean feed(Iterator<T> items, Iterator<Double> weights) {
return WeightedRandomSampling.super.feed(items, weights);
}

/**
Expand All @@ -230,13 +231,13 @@ public ChaoSampling<T> feed(Iterator<T> items, Iterator<Double> weights) {
* @param items {@inheritDoc}
* @return {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws IllegalWeightException {@inheritDoc}
* @throws StreamOverflowException {@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}
*/
@Override
public ChaoSampling<T> feed(Map<T, Double> items) {
WeightedRandomSampling.super.feed(items);
return this;
public boolean feed(Map<T, Double> items) {
return WeightedRandomSampling.super.feed(items);
}

/**
Expand Down Expand Up @@ -277,12 +278,12 @@ public final long streamSize() {
* @param item {@inheritDoc}
* @return {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws StreamOverflowException {@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}
*/
@Override
public ChaoSampling<T> feed(T item) {
feed(item, 1.0);
return this;
public boolean feed(T item) {
return WeightedRandomSampling.super.feed(item);
}

/**
Expand All @@ -291,12 +292,12 @@ public ChaoSampling<T> feed(T item) {
* @param items {@inheritDoc}
* @return {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws StreamOverflowException {@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}
*/
@Override
public ChaoSampling<T> feed(Iterator<T> items) {
WeightedRandomSampling.super.feed(items);
return this;
public boolean feed(Iterator<T> items) {
return WeightedRandomSampling.super.feed(items);
}

/**
Expand All @@ -305,11 +306,11 @@ public ChaoSampling<T> feed(Iterator<T> items) {
* @param items {@inheritDoc}
* @return {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws StreamOverflowException {@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}
*/
@Override
public ChaoSampling<T> feed(Iterable<T> items) {
WeightedRandomSampling.super.feed(items);
return this;
public boolean feed(Iterable<T> items) {
return WeightedRandomSampling.super.feed(items);
}
}
34 changes: 14 additions & 20 deletions src/main/java/gr/james/sampling/EfraimidisSampling.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static <E> WeightedRandomSamplingCollector<E> weightedCollector(int sampl
* @throws IllegalWeightException if {@code weight} is outside the range (0,+Inf)
*/
@Override
public EfraimidisSampling<T> feed(T item, double weight) {
public boolean feed(T item, double weight) {
// Checks
if (item == null) {
throw new NullPointerException("Item was null");
Expand All @@ -127,18 +127,17 @@ public EfraimidisSampling<T> feed(T item, double weight) {
// Add item to reservoir
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();
pq.poll();
pq.add(newItem);
return true;
}

assert !pq.isEmpty();
assert pq.stream().allMatch(Objects::nonNull);

return this;
return false;
}

/**
Expand All @@ -152,9 +151,8 @@ public EfraimidisSampling<T> feed(T item, double weight) {
* @throws IllegalWeightException {@inheritDoc}
*/
@Override
public EfraimidisSampling<T> feed(Iterator<T> items, Iterator<Double> weights) {
WeightedRandomSampling.super.feed(items, weights);
return this;
public boolean feed(Iterator<T> items, Iterator<Double> weights) {
return WeightedRandomSampling.super.feed(items, weights);
}

/**
Expand All @@ -166,9 +164,8 @@ public EfraimidisSampling<T> feed(Iterator<T> items, Iterator<Double> weights) {
* @throws IllegalWeightException {@inheritDoc}
*/
@Override
public EfraimidisSampling<T> feed(Map<T, Double> items) {
WeightedRandomSampling.super.feed(items);
return this;
public boolean feed(Map<T, Double> items) {
return WeightedRandomSampling.super.feed(items);
}

/**
Expand Down Expand Up @@ -210,9 +207,8 @@ public final long streamSize() {
* @throws NullPointerException {@inheritDoc}
*/
@Override
public EfraimidisSampling<T> feed(T item) {
WeightedRandomSampling.super.feed(item);
return this;
public boolean feed(T item) {
return WeightedRandomSampling.super.feed(item);
}

/**
Expand All @@ -223,9 +219,8 @@ public EfraimidisSampling<T> feed(T item) {
* @throws NullPointerException {@inheritDoc}
*/
@Override
public EfraimidisSampling<T> feed(Iterator<T> items) {
WeightedRandomSampling.super.feed(items);
return this;
public boolean feed(Iterator<T> items) {
return WeightedRandomSampling.super.feed(items);
}

/**
Expand All @@ -236,8 +231,7 @@ public EfraimidisSampling<T> feed(Iterator<T> items) {
* @throws NullPointerException {@inheritDoc}
*/
@Override
public EfraimidisSampling<T> feed(Iterable<T> items) {
WeightedRandomSampling.super.feed(items);
return this;
public boolean feed(Iterable<T> items) {
return WeightedRandomSampling.super.feed(items);
}
}
15 changes: 6 additions & 9 deletions src/main/java/gr/james/sampling/IdentityRandomSampling.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,24 @@ public int size() {
}

@Override
public RS feed(T item) {
public boolean feed(T item) {
if (item == null) {
throw new NullPointerException();
}
if (!set.add(item)) {
throw new UnsupportedOperationException();
}
source.feed(item);
return source;
return source.feed(item);
}

@Override
public RS feed(Iterator<T> items) {
source.feed(items);
return source;
public boolean feed(Iterator<T> items) {
return source.feed(items);
}

@Override
public RS feed(Iterable<T> items) {
source.feed(items);
return source;
public boolean feed(Iterable<T> items) {
return source.feed(items);
}

@Override
Expand Down
Loading

0 comments on commit 5994a68

Please sign in to comment.