mirror of
https://github.com/Melledy/Grasscutter.git
synced 2024-11-24 23:50:14 +00:00
30 lines
696 B
Java
30 lines
696 B
Java
|
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();
|
||
|
}
|
||
|
}
|