Avoid performance degradation of indexed access when using a linked list.
Thanks to Artavazd Balaian. Closes #134.
This commit is contained in:
parent
7b005845af
commit
2d8adbf514
|
@ -86,9 +86,8 @@ public class EnumeratedDistribution<T> implements Serializable {
|
||||||
NotANumberException {
|
NotANumberException {
|
||||||
singletons = new ArrayList<>(pmf.size());
|
singletons = new ArrayList<>(pmf.size());
|
||||||
final double[] probs = new double[pmf.size()];
|
final double[] probs = new double[pmf.size()];
|
||||||
|
int count = 0;
|
||||||
for (int i = 0; i < pmf.size(); i++) {
|
for (Pair<T, Double> sample : pmf) {
|
||||||
final Pair<T, Double> sample = pmf.get(i);
|
|
||||||
singletons.add(sample.getKey());
|
singletons.add(sample.getKey());
|
||||||
final double p = sample.getValue();
|
final double p = sample.getValue();
|
||||||
if (p < 0) {
|
if (p < 0) {
|
||||||
|
@ -100,7 +99,7 @@ public class EnumeratedDistribution<T> implements Serializable {
|
||||||
if (Double.isNaN(p)) {
|
if (Double.isNaN(p)) {
|
||||||
throw new NotANumberException();
|
throw new NotANumberException();
|
||||||
}
|
}
|
||||||
probs[i] = p;
|
probs[count++] = p;
|
||||||
}
|
}
|
||||||
|
|
||||||
probabilities = MathArrays.normalizeArray(probs, 1.0);
|
probabilities = MathArrays.normalizeArray(probs, 1.0);
|
||||||
|
|
Loading…
Reference in New Issue