mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2024-12-22 17:27:38 +00:00
Clean up RandomCollection architecture
This commit is contained in:
parent
bb05bd24d9
commit
6fb202443b
@ -5,12 +5,25 @@ import com.boydti.fawe.util.MathMan;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
public class FastRandomCollection<T> extends RandomCollection<T> {
|
public class FastRandomCollection<T> extends RandomCollection<T> {
|
||||||
private T[] values;
|
private final T[] values;
|
||||||
|
|
||||||
public FastRandomCollection(Map<T, Double> weights, SimpleRandom random) {
|
private FastRandomCollection(T[] values, SimpleRandom random) {
|
||||||
super(weights, random);
|
super(random);
|
||||||
|
this.values = values;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new FastRandomCollection if the given values and weights match the criteria.
|
||||||
|
* @param weights the weight of the values.
|
||||||
|
* @param random the random generator to use for this collection.
|
||||||
|
* @param <T> the value type.
|
||||||
|
* @return an {@link Optional} containing the new collection if it could
|
||||||
|
* be created, {@link Optional#empty()} otherwise.
|
||||||
|
*/
|
||||||
|
public static <T> Optional<RandomCollection<T>> create(Map<T, Double> weights, SimpleRandom random) {
|
||||||
int max = 0;
|
int max = 0;
|
||||||
int[] counts = new int[weights.size()];
|
int[] counts = new int[weights.size()];
|
||||||
Double[] weightDoubles = weights.values().toArray(new Double[0]);
|
Double[] weightDoubles = weights.values().toArray(new Double[0]);
|
||||||
@ -18,7 +31,7 @@ public class FastRandomCollection<T> extends RandomCollection<T> {
|
|||||||
int weight = (int) (weightDoubles[i] * 100);
|
int weight = (int) (weightDoubles[i] * 100);
|
||||||
counts[i] = weight;
|
counts[i] = weight;
|
||||||
if (weight != (weightDoubles[i] * 100)) {
|
if (weight != (weightDoubles[i] * 100)) {
|
||||||
throw new IllegalArgumentException("Too small");
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
if (weight > max) {
|
if (weight > max) {
|
||||||
max = weight;
|
max = weight;
|
||||||
@ -26,7 +39,7 @@ public class FastRandomCollection<T> extends RandomCollection<T> {
|
|||||||
}
|
}
|
||||||
int gcd = MathMan.gcd(counts);
|
int gcd = MathMan.gcd(counts);
|
||||||
if (max / gcd > 100000) {
|
if (max / gcd > 100000) {
|
||||||
throw new IllegalArgumentException("Too large");
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
ArrayList<T> parsed = new ArrayList<>();
|
ArrayList<T> parsed = new ArrayList<>();
|
||||||
for (Map.Entry<T, Double> entry : weights.entrySet()) {
|
for (Map.Entry<T, Double> entry : weights.entrySet()) {
|
||||||
@ -35,11 +48,14 @@ public class FastRandomCollection<T> extends RandomCollection<T> {
|
|||||||
parsed.add(entry.getKey());
|
parsed.add(entry.getKey());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.values = (T[]) parsed.toArray();
|
@SuppressWarnings("unchecked")
|
||||||
|
T[] values = (T[]) parsed.toArray();
|
||||||
|
FastRandomCollection<T> fastRandomCollection = new FastRandomCollection<>(values, random);
|
||||||
|
return Optional.of(fastRandomCollection);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T next(int x, int y, int z) {
|
public T next(int x, int y, int z) {
|
||||||
return values[random.nextInt(x, y, z, values.length)];
|
return values[getRandom().nextInt(x, y, z, values.length)];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,18 +7,16 @@ import java.util.Map;
|
|||||||
import static com.google.common.base.Preconditions.checkNotNull;
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
|
|
||||||
public abstract class RandomCollection<T> {
|
public abstract class RandomCollection<T> {
|
||||||
protected SimpleRandom random;
|
private SimpleRandom random;
|
||||||
|
|
||||||
public RandomCollection(Map<T, Double> weights, SimpleRandom random) {
|
protected RandomCollection(SimpleRandom random) {
|
||||||
this.random = random;
|
this.random = random;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> RandomCollection<T> of(Map<T, Double> weights, SimpleRandom random) {
|
public static <T> RandomCollection<T> of(Map<T, Double> weights, SimpleRandom random) {
|
||||||
try {
|
checkNotNull(random);
|
||||||
return new FastRandomCollection<>(weights, random);
|
return FastRandomCollection.create(weights, random)
|
||||||
} catch (IllegalArgumentException ignore) {
|
.orElse(new SimpleRandomCollection<>(weights, random));
|
||||||
return new SimpleRandomCollection<>(weights, random);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRandom(SimpleRandom random) {
|
public void setRandom(SimpleRandom random) {
|
||||||
|
@ -12,7 +12,7 @@ public class SimpleRandomCollection<E> extends RandomCollection<E> {
|
|||||||
private double total = 0;
|
private double total = 0;
|
||||||
|
|
||||||
public SimpleRandomCollection(Map<E, Double> weights, SimpleRandom random) {
|
public SimpleRandomCollection(Map<E, Double> weights, SimpleRandom random) {
|
||||||
super(weights, random);
|
super(random);
|
||||||
for (Map.Entry<E, Double> entry : weights.entrySet()) {
|
for (Map.Entry<E, Double> entry : weights.entrySet()) {
|
||||||
add(entry.getValue(), entry.getKey());
|
add(entry.getValue(), entry.getKey());
|
||||||
}
|
}
|
||||||
@ -24,7 +24,8 @@ public class SimpleRandomCollection<E> extends RandomCollection<E> {
|
|||||||
map.put(total, result);
|
map.put(total, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public E next(int x, int y, int z) {
|
public E next(int x, int y, int z) {
|
||||||
return map.ceilingEntry(random.nextDouble(x, y, z)).getValue();
|
return map.ceilingEntry(getRandom().nextDouble(x, y, z)).getValue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user