All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.minestom.server.utils.collection.AutoIncrementMap Maven / Gradle / Ivy

There is a newer version: 7320437640
Show newest version
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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy