net.minestom.server.utils.collection.AutoIncrementMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of minestom-snapshots Show documentation
Show all versions of minestom-snapshots Show documentation
1.20.4 Lightweight Minecraft server
package net.minestom.server.utils.collection;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
@ApiStatus.Internal
public final class AutoIncrementMap {
private final Object2IntOpenHashMap write = new Object2IntOpenHashMap<>();
private Object2IntOpenHashMap read;
private int lastIndex;
public AutoIncrementMap() {
this.write.defaultReturnValue(-1);
this.read = write.clone();
}
@Contract(pure = true)
public int get(@NotNull K key) {
int index = read.getInt(key);
if (index == -1) {
synchronized (write) {
var write = this.write;
index = write.getInt(key);
if (index == -1) {
write.put(key, (index = lastIndex++));
read = write.clone();
}
}
}
return index;
}
}