diff options
| author | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2018-06-03 20:57:27 -0300 |
|---|---|---|
| committer | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2018-06-03 20:57:27 -0300 |
| commit | 8dfddc586f49b2d7ce4a4d712334a28584c0b315 (patch) | |
| tree | f6706114f5ec090b753b511902817dc3481a9053 /base/src/main | |
| parent | f121b5499f4a2965422d1457e91f03ebb7fc5a35 (diff) | |
Efficency updates
Change a few things to use more efficent implementations
Diffstat (limited to 'base/src/main')
| -rw-r--r-- | base/src/main/java/bjc/utils/data/Pair.java | 8 | ||||
| -rw-r--r-- | base/src/main/java/bjc/utils/gen/WeightedRandom.java | 36 |
2 files changed, 25 insertions, 19 deletions
diff --git a/base/src/main/java/bjc/utils/data/Pair.java b/base/src/main/java/bjc/utils/data/Pair.java index 40e4849..df7e199 100644 --- a/base/src/main/java/bjc/utils/data/Pair.java +++ b/base/src/main/java/bjc/utils/data/Pair.java @@ -102,6 +102,14 @@ public class Pair<LeftType, RightType> implements IPair<LeftType, RightType> { return String.format("Pair [leftValue='%s', rightValue='%s']", leftValue, rightValue); } + public LeftType getLeft() { + return leftValue; + } + + public RightType getRight() { + return rightValue; + } + @Override public int hashCode() { final int prime = 31; diff --git a/base/src/main/java/bjc/utils/gen/WeightedRandom.java b/base/src/main/java/bjc/utils/gen/WeightedRandom.java index be8b8c2..c7cee19 100644 --- a/base/src/main/java/bjc/utils/gen/WeightedRandom.java +++ b/base/src/main/java/bjc/utils/gen/WeightedRandom.java @@ -24,7 +24,7 @@ public class WeightedRandom<E> { private final IList<E> results; /* The source for any needed random numbers */ - private final Random source; + private Random source; /* The total chance for all values. */ private int totalChance; @@ -37,7 +37,7 @@ public class WeightedRandom<E> { */ public WeightedRandom(final Random src) { probabilities = new FunctionalList<>(); - results = new FunctionalList<>(); + results = new FunctionalList<>(); if(src == null) throw new NullPointerException("Source of randomness must not be null"); @@ -66,25 +66,23 @@ public class WeightedRandom<E> { * @return A random value selected in a weighted fashion. */ public E generateValue() { - final IHolder<Integer> value = new Identity<>(source.nextInt(totalChance)); - final IHolder<E> current = new Identity<>(); - final IHolder<Boolean> picked = new Identity<>(true); - - probabilities.forEachIndexed((index, probability) -> { - if(picked.unwrap(bool -> bool)) { - if(value.unwrap((number) -> number < probability)) { - current.transform((result) -> results.getByIndex(index)); - - picked.transform((bool) -> false); - } else { - value.transform((number) -> number - probability); - } - } - }); - - return current.unwrap((result) -> result); + return generateValue(source); } + public E generateValue(Random rn) { + int target = rn.nextInt(totalChance); + + int i = 0; + + for(int prob : probabilities) { + if(target <= prob) return results.getByIndex(i); + + target -= prob; + i += 1; + } + + throw new NullPointerException("Fell off the end of the results list"); + } /** * Return a list of values that can be generated by this generator * |
