Grasscutter/src/main/java/emu/grasscutter/utils/WeightedList.java

30 lines
696 B
Java
Raw Normal View History

2022-04-17 12:43:07 +00:00
package emu.grasscutter.utils;
import java.util.NavigableMap;
import java.util.TreeMap;
import java.util.concurrent.ThreadLocalRandom;
public class WeightedList<E> {
private final NavigableMap<Double, E> map = new TreeMap<Double, E>();
private double total = 0;
public WeightedList() {
}
public WeightedList<E> add(double weight, E result) {
if (weight <= 0) return this;
total += weight;
map.put(total, result);
return this;
}
public E next() {
double value = ThreadLocalRandom.current().nextDouble() * total;
return map.higherEntry(value).getValue();
}
public int size() {
return map.size();
}
}