Skip to content

Commit

Permalink
Merge pull request #48 from RADAR-base/release-0.8.2
Browse files Browse the repository at this point in the history
Release 0.8.2
  • Loading branch information
yatharthranjan authored Apr 25, 2018
2 parents 20d3508 + fc1b664 commit a9acb95
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ repositories {
}
dependencies {
compile group: 'org.radarcns', name: 'radar-commons', version: '0.8.1'
compile group: 'org.radarcns', name: 'radar-commons', version: '0.8.2'
}
```

Expand All @@ -26,7 +26,7 @@ repositories {
}
dependencies {
testCompile group: 'org.radarcns', name: 'radar-commons-testing', version: '0.8.1'
testCompile group: 'org.radarcns', name: 'radar-commons-testing', version: '0.8.2'
}
```

Expand All @@ -51,7 +51,7 @@ configurations.all {
}
dependencies {
compile group: 'org.radarcns', name: 'radar-commons', version: '0.8.2-SNAPSHOT', changing: true
compile group: 'org.radarcns', name: 'radar-commons', version: '0.8.3-SNAPSHOT', changing: true
}
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ allprojects {
// Configuration //
//---------------------------------------------------------------------------//

version = '0.8.1'
version = '0.8.2'
group = 'org.radarcns'
ext.githubRepoName = 'RADAR-CNS/RADAR-Commons'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;

/**
Expand Down Expand Up @@ -50,8 +53,9 @@ public UniformSamplingReservoir(
@JsonProperty("samples") List<Double> samples,
@JsonProperty("count") int count,
@JsonProperty("maxSize") int maxSize) {
this.samples = new ArrayList<>(Objects.requireNonNull(samples));

if (samples == null) {
throw new IllegalArgumentException("Samples may not be null");
}
if (maxSize <= 0) {
throw new IllegalArgumentException("Reservoir size must be strictly positive");
}
Expand All @@ -61,14 +65,41 @@ public UniformSamplingReservoir(
throw new IllegalArgumentException("Reservoir size must be positive");
}
this.count = count;
this.samples = new ArrayList<>(maxSize);
initializeReservoir(samples);
}


int toRemove = this.samples.size() - maxSize;
if (toRemove > 0) {
/** Sample from given list of samples to initialize the reservoir. */
private void initializeReservoir(List<Double> samples) {
int numSamples = samples.size();
// There are much more samples than the size permits. Random sample from the
// given list. Duplicate addresses are retried, and for each hit there is at least a
// 50% probability of getting a number that has not yet been picked.
if (numSamples > maxSize * 2) {
ThreadLocalRandom random = ThreadLocalRandom.current();
for (int i = 0; i < toRemove; i++) {
this.samples.remove(random.nextInt(this.samples.size()));
Set<Integer> indexes = new HashSet<>();
while (indexes.size() < maxSize) {
indexes.add(random.nextInt(numSamples));
}
for (Integer index : indexes) {
this.samples.add(samples.get(index));
}
// There are not much more samples than the size permits. Make a list from all indexes
// and at random pick and remove index from that. Put all the samples at given
// indexes in the reservoir. Do not do retry sampling as above, as the final entry may
// have a probability of 1/maxSize of actually being picked.
} else if (numSamples > maxSize) {
LinkedList<Integer> indexes = new LinkedList<>();
for (int i = 0; i < numSamples; i++) {
indexes.add(i);
}
ThreadLocalRandom random = ThreadLocalRandom.current();
for (int i = 0; i < maxSize; i++) {
int index = indexes.remove(random.nextInt(indexes.size()));
this.samples.add(samples.get(index));
}
} else {
this.samples.addAll(samples);
}
Collections.sort(this.samples);
}
Expand Down

0 comments on commit a9acb95

Please sign in to comment.