Rename AutoIncrementMap

This commit is contained in:
themode 2022-05-16 08:39:18 +02:00
parent 8d7175af73
commit bfa2dbd3f7
3 changed files with 10 additions and 15 deletions

View File

@ -2,7 +2,7 @@ package net.minestom.server.tag;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.minestom.server.item.ItemStack; import net.minestom.server.item.ItemStack;
import net.minestom.server.utils.collection.IndexMap; import net.minestom.server.utils.collection.AutoIncrementMap;
import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -30,7 +30,7 @@ import java.util.function.UnaryOperator;
*/ */
@ApiStatus.NonExtendable @ApiStatus.NonExtendable
public class Tag<T> { public class Tag<T> {
private static final IndexMap<String> INDEX_MAP = new IndexMap<>(); private static final AutoIncrementMap<String> INDEX_MAP = new AutoIncrementMap<>();
record PathEntry(String name, int index) { record PathEntry(String name, int index) {
} }

View File

@ -6,13 +6,14 @@ import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ApiStatus.Internal @ApiStatus.Internal
public final class IndexMap<K> { public final class AutoIncrementMap<K> {
private final Object2IntOpenHashMap<K> write = new Object2IntOpenHashMap<>(); private final Object2IntOpenHashMap<K> write = new Object2IntOpenHashMap<>();
private Object2IntOpenHashMap<K> read = copy(); private Object2IntOpenHashMap<K> read;
private int lastIndex; private int lastIndex;
public IndexMap() { public AutoIncrementMap() {
write.defaultReturnValue(-1); this.write.defaultReturnValue(-1);
this.read = write.clone();
} }
@Contract(pure = true) @Contract(pure = true)
@ -24,16 +25,10 @@ public final class IndexMap<K> {
index = write.getInt(key); index = write.getInt(key);
if (index == -1) { if (index == -1) {
write.put(key, (index = lastIndex++)); write.put(key, (index = lastIndex++));
read = copy(); read = write.clone();
} }
} }
} }
return index; return index;
} }
private Object2IntOpenHashMap<K> copy() {
var map = new Object2IntOpenHashMap<>(write);
map.defaultReturnValue(-1);
return map;
}
} }

View File

@ -4,10 +4,10 @@ import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
public class IndexMapTest { public class AutoIncrementMapTest {
@Test @Test
public void test() { public void test() {
IndexMap<String> map = new IndexMap<>(); AutoIncrementMap<String> map = new AutoIncrementMap<>();
for (int i = 0; i < 1000; i++) { for (int i = 0; i < 1000; i++) {
assertEquals(i, map.get("test" + i)); assertEquals(i, map.get("test" + i));
for (int j = 0; j < i; j++) { for (int j = 0; j < i; j++) {