WeightedMap class
Пакет: com.hypixel.hytale.builtin.hytalegenerator
Файл: com/hypixel/hytale/builtin/hytalegenerator/WeightedMap.java
Поля (1)
| Модификаторы | Тип | Имя |
|---|---|---|
|
double |
var2 |
Методы (16)
| Модификаторы | Возврат | Сигнатура |
|---|---|---|
abstract |
throw new |
IllegalStateExceptionthrow new IllegalStateException("method can't be called when object is immutable") |
abstract |
throw new |
IllegalStateExceptionthrow new IllegalStateException("can't be empty when calling this method") |
abstract |
throw new |
NullPointerExceptionthrow new NullPointerException() |
abstract |
|
for for(int var4 = 0; var4 < this.elements.size() |
abstract |
|
for for(int var2 = 0; var2 < this.elements.size() |
public |
void |
forEachvoid forEach(@Nonnull BiConsumer<T, Double> var1) |
public |
double |
getdouble get(@Nonnull T var1) |
|
|
if if(var2 <= 0.0) |
|
|
if if(var1 == null) |
|
|
if if(var1 == null) |
|
|
if if(var2 <= 0.0) |
public |
boolean |
isImmutableboolean isImmutable() |
public |
void |
makeImmutablevoid makeImmutable() |
public |
T |
pickT pick(@Nonnull Random var1) |
public |
int |
sizeint size() |
abstract |
|
this this(2) |
Исходный код
Показать/скрыть
class="kw">package com.hypixel.hytale.builtin.hytalegenerator;
class="kw">import it.unimi.dsi.fastutil.doubles.DoubleArrayList;
class="kw">import it.unimi.dsi.fastutil.doubles.DoubleList;
class="kw">import it.unimi.dsi.fastutil.objects.Object2IntMap;
class="kw">import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
class="kw">import java.util.ArrayList;
class="kw">import java.util.Collections;
class="kw">import java.util.HashSet;
class="kw">import java.util.List;
class="kw">import java.util.Random;
class="kw">import java.util.Set;
class="kw">import java.util.function.BiConsumer;
class="kw">import javax.annotation.Nonnull;
class="kw">public class WeightedMap<T> {
@Nonnull
class="kw">private class="kw">final Set<T> elementSet;
@Nonnull
class="kw">private class="kw">final List<T> elements;
@Nonnull
class="kw">private class="kw">final List<T> elementsUnmodifiable;
@Nonnull
class="kw">private class="kw">final DoubleList weights;
@Nonnull
class="kw">private class="kw">final Object2IntMap<T> indices;
class="kw">private double totalWeight = 0.0;
class="kw">private boolean immutable = false;
class="kw">public WeightedMap(@Nonnull WeightedMap<T> var1) {
this.totalWeight = var1.totalWeight;
this.elementSet = new HashSet<>(var1.elementSet);
this.elements = new ArrayList<>(var1.elements);
this.elementsUnmodifiable = Collections.unmodifiableList(this.elements);
this.weights = new DoubleArrayList(var1.weights);
this.indices = new Object2IntOpenHashMap(var1.indices);
this.immutable = var1.immutable;
}
class="kw">public WeightedMap() {
this(2);
}
class="kw">public WeightedMap(int var1) {
this.elementSet = new HashSet<>(var1);
this.elements = new ArrayList<>(var1);
this.elementsUnmodifiable = Collections.unmodifiableList(this.elements);
this.weights = new DoubleArrayList(var1);
this.indices = new Object2IntOpenHashMap(var1);
}
@Nonnull
class="kw">public WeightedMap<T> add(@Nonnull T var1, double var2) {
assert !this.immutable;
if (var2 <= 0.0) {
class="kw">return this;
}
this.elements.add(var1);
this.weights.add(var2);
this.elementSet.add(var1);
this.totalWeight += var2;
this.indices.put(var1, this.elements.size() - 1);
class="kw">return this;
}
class="kw">public double get(@Nonnull T var1) {
if (var1 == null) {
throw new NullPointerException();
} else if (this.immutable) {
throw new IllegalStateException("method can't be called when object is immutable");
} else {
class="kw">return !this.elementSet.contains(var1) ? 0.0 : this.weights.getDouble(this.indices.getInt(var1));
}
}
class="kw">public T pick(@Nonnull Random var1) {
if (var1 == null) {
throw new NullPointerException();
}
if (this.elements.isEmpty()) {
throw new IllegalStateException("can't be empty when calling this method");
}
double var2 = var1.nextDouble() * this.totalWeight;
for (int var4 = 0; var4 < this.elements.size(); var4++) {
var2 -= this.weights.getDouble(var4);
if (var2 <= 0.0) {
class="kw">return this.elements.get(var4);
}
}
class="kw">return this.elements.getLast();
}
class="kw">public int size() {
class="kw">return this.elements.size();
}
@Nonnull
class="kw">public List<T> elements() {
class="kw">return this.elementsUnmodifiable;
}
class="kw">public void makeImmutable() {
this.immutable = true;
}
class="kw">public boolean isImmutable() {
class="kw">return this.immutable;
}
class="kw">public void forEach(@Nonnull BiConsumer<T, Double> var1) {
for (int var2 = 0; var2 < this.elements.size(); var2++) {
var1.accept(this.elements.get(var2), this.weights.getDouble(var2));
}
}
@Nonnull
@Override
class="kw">public String toString() {
class="kw">return "WeightedMap{elementSet="
+ this.elementSet
+ ", elements="
+ this.elements
+ ", weights="
+ this.weights
+ ", indices="
+ this.indices
+ ", totalWeight="
+ this.totalWeight
+ ", immutable="
+ this.immutable
+ "}";
}
}