3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-11-03 14:50:30 +01:00

Create soft wrapper for FU Int2ObjectMap

Dieser Commit ist enthalten in:
KennyTV 2020-06-08 13:49:26 +02:00
Ursprung 30e10bb645
Commit 1ad559212e
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 6BE3B555EBC5982B
18 geänderte Dateien mit 465 neuen und 136 gelöschten Zeilen

Datei anzeigen

@ -91,12 +91,11 @@ public class Entity1_13Types {
// Fish // Fish
ABSTRACT_FISHES(-1, ABSTRACT_CREATURE), // agb ABSTRACT_FISHES(-1, ABSTRACT_CREATURE), // agb
COD_MOB(8, ABSTRACT_FISHES), // agf COD(8, ABSTRACT_FISHES), // agf
PUFFERFISH(52, ABSTRACT_FISHES), // agn PUFFERFISH(52, ABSTRACT_FISHES), // agn
SALMON(57, ABSTRACT_FISHES), // agp SALMON(57, ABSTRACT_FISHES), // agp
TROPICAL_FISH(72, ABSTRACT_FISHES), // agu TROPICAL_FISH(72, ABSTRACT_FISHES), // agu
// Monsters // Monsters
ABSTRACT_MONSTER(-1, ABSTRACT_CREATURE), // ajs ABSTRACT_MONSTER(-1, ABSTRACT_CREATURE), // ajs
BLAZE(4, ABSTRACT_MONSTER), // ajd BLAZE(4, ABSTRACT_MONSTER), // ajd
@ -248,7 +247,7 @@ public class Entity1_13Types {
ENDER_PEARL(65, EntityType.ENDER_PEARL), ENDER_PEARL(65, EntityType.ENDER_PEARL),
WITHER_SKULL(66, EntityType.WITHER_SKULL), WITHER_SKULL(66, EntityType.WITHER_SKULL),
SHULKER_BULLET(67, EntityType.SHULKER_BULLET), SHULKER_BULLET(67, EntityType.SHULKER_BULLET),
LIAMA_SPIT(68, EntityType.LLAMA_SPIT), LLAMA_SPIT(68, EntityType.LLAMA_SPIT),
FALLING_BLOCK(70, EntityType.FALLING_BLOCK), FALLING_BLOCK(70, EntityType.FALLING_BLOCK),
ITEM_FRAME(71, EntityType.ITEM_FRAME), ITEM_FRAME(71, EntityType.ITEM_FRAME),
EYE_OF_ENDER(72, EntityType.EYE_OF_ENDER), EYE_OF_ENDER(72, EntityType.EYE_OF_ENDER),

Datei anzeigen

@ -31,6 +31,8 @@ import us.myles.ViaVersion.protocols.protocol1_9_1to1_9.Protocol1_9_1To1_9;
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.Protocol1_9_3To1_9_1_2; import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.Protocol1_9_3To1_9_1_2;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9To1_8; import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9To1_8;
import us.myles.ViaVersion.protocols.protocol1_9to1_9_1.Protocol1_9To1_9_1; import us.myles.ViaVersion.protocols.protocol1_9to1_9_1.Protocol1_9To1_9_1;
import us.myles.ViaVersion.util.fastutil.CollectionUtil;
import us.myles.ViaVersion.util.fastutil.IntObjectMap;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -52,7 +54,7 @@ public class ProtocolRegistry {
public static final Protocol BASE_PROTOCOL = new BaseProtocol(); public static final Protocol BASE_PROTOCOL = new BaseProtocol();
public static int SERVER_PROTOCOL = -1; public static int SERVER_PROTOCOL = -1;
// Input Version -> Output Version & Protocol (Allows fast lookup) // Input Version -> Output Version & Protocol (Allows fast lookup)
private static final Map<Integer, Map<Integer, Protocol>> registryMap = new ConcurrentHashMap<>(); private static final IntObjectMap<IntObjectMap<Protocol>> registryMap = CollectionUtil.createIntObjectMap(32);
private static final Map<Class<? extends Protocol>, Protocol> protocols = new HashMap<>(); private static final Map<Class<? extends Protocol>, Protocol> protocols = new HashMap<>();
private static final Map<Pair<Integer, Integer>, List<Pair<Integer, Protocol>>> pathCache = new ConcurrentHashMap<>(); private static final Map<Pair<Integer, Integer>, List<Pair<Integer, Protocol>>> pathCache = new ConcurrentHashMap<>();
private static final Set<Integer> supportedVersions = new HashSet<>(); private static final Set<Integer> supportedVersions = new HashSet<>();
@ -135,7 +137,12 @@ public class ProtocolRegistry {
protocols.put(protocol.getClass(), protocol); protocols.put(protocol.getClass(), protocol);
for (int version : supported) { for (int version : supported) {
Map<Integer, Protocol> protocolMap = registryMap.computeIfAbsent(version, k -> new HashMap<>()); IntObjectMap<Protocol> protocolMap = registryMap.get(version);
if (protocolMap == null) {
protocolMap = CollectionUtil.createIntObjectMap(1);
registryMap.put(version, protocolMap);
}
protocolMap.put(output, protocol); protocolMap.put(output, protocol);
} }
@ -204,7 +211,7 @@ public class ProtocolRegistry {
* @return True if there is a useful pipe * @return True if there is a useful pipe
*/ */
public static boolean isWorkingPipe() { public static boolean isWorkingPipe() {
for (Map<Integer, Protocol> maps : registryMap.values()) { for (IntObjectMap<Protocol> maps : registryMap.getMap().values()) {
if (maps.containsKey(SERVER_PROTOCOL)) return true; if (maps.containsKey(SERVER_PROTOCOL)) return true;
} }
return false; // No destination for protocol return false; // No destination for protocol
@ -234,20 +241,22 @@ public class ProtocolRegistry {
if (current.size() > 50) return null; // Fail safe, protocol too complicated. if (current.size() > 50) return null; // Fail safe, protocol too complicated.
// First check if there is any protocols for this // First check if there is any protocols for this
Map<Integer, Protocol> inputMap = registryMap.get(clientVersion); IntObjectMap<Protocol> inputMap = registryMap.get(clientVersion);
if (inputMap == null) { if (inputMap == null) {
return null; // Not supported return null; // Not supported
} }
// Next check there isn't an obvious path // Next check there isn't an obvious path
Protocol protocol = inputMap.get(serverVersion); Protocol protocol = inputMap.get(serverVersion);
if (protocol != null) { if (protocol != null) {
current.add(new Pair<>(serverVersion, protocol)); current.add(new Pair<>(serverVersion, protocol));
return current; // Easy solution return current; // Easy solution
} }
// There might be a more advanced solution... So we'll see if any of the others can get us there // There might be a more advanced solution... So we'll see if any of the others can get us there
List<Pair<Integer, Protocol>> shortest = null; List<Pair<Integer, Protocol>> shortest = null;
for (Map.Entry<Integer, Protocol> entry : inputMap.entrySet()) { for (Map.Entry<Integer, Protocol> entry : inputMap.getMap().entrySet()) {
// Ensure it wasn't caught by the other loop // Ensure it wasn't caught by the other loop
if (!entry.getKey().equals(serverVersion)) { if (!entry.getKey().equals(serverVersion)) {
Pair<Integer, Protocol> pair = new Pair<>(entry.getKey(), entry.getValue()); Pair<Integer, Protocol> pair = new Pair<>(entry.getKey(), entry.getValue());

Datei anzeigen

@ -192,7 +192,7 @@ public abstract class MetadataRewriter {
} catch (IllegalArgumentException notFound) { } catch (IllegalArgumentException notFound) {
if (!typeMapping.containsKey(oldType.getId())) { if (!typeMapping.containsKey(oldType.getId())) {
Via.getPlatform().getLogger().warning("Could not find new entity type for " + oldType + "! " + Via.getPlatform().getLogger().warning("Could not find new entity type for " + oldType + "! " +
"Old type: " + oldType.getClass().getSimpleName() + " New type: " + newTypeClass.getSimpleName()); "Old type: " + oldType.getClass().getEnclosingClass().getSimpleName() + ", new type: " + newTypeClass.getEnclosingClass().getSimpleName());
} }
} }
} }

Datei anzeigen

@ -17,18 +17,23 @@ import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.blockconnections.providers.BlockConnectionProvider; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.blockconnections.providers.BlockConnectionProvider;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.blockconnections.providers.PacketBlockConnectionProvider; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.blockconnections.providers.PacketBlockConnectionProvider;
import us.myles.ViaVersion.util.fastutil.CollectionUtil; import us.myles.ViaVersion.util.fastutil.CollectionUtil;
import us.myles.ViaVersion.util.fastutil.IntObjectMap;
import us.myles.ViaVersion.util.fastutil.IntSet; import us.myles.ViaVersion.util.fastutil.IntSet;
import java.util.*; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
public class ConnectionData { public class ConnectionData {
private static final BlockChangeRecord[] A = new BlockChangeRecord[0]; private static final BlockChangeRecord[] A = new BlockChangeRecord[0];
public static BlockConnectionProvider blockConnectionProvider; public static BlockConnectionProvider blockConnectionProvider;
static Map<Integer, String> idToKey = new HashMap<>(); static IntObjectMap<String> idToKey = CollectionUtil.createIntObjectMap(8581);
static Map<String, Integer> keyToId = new HashMap<>(); static Map<String, Integer> keyToId = new HashMap<>(8581);
static Map<Integer, ConnectionHandler> connectionHandlerMap = new HashMap<>(); static IntObjectMap<ConnectionHandler> connectionHandlerMap = CollectionUtil.createIntObjectMap(1);
static Map<Integer, BlockData> blockConnectionData = new HashMap<>(); static IntObjectMap<BlockData> blockConnectionData = CollectionUtil.createIntObjectMap(1);
static IntSet occludingStates = CollectionUtil.createIntSet(377); static IntSet occludingStates = CollectionUtil.createIntSet(377);
public static void update(UserConnection user, Position position) { public static void update(UserConnection user, Position position) {
@ -196,17 +201,21 @@ public class ConnectionData {
public static void init() { public static void init() {
if (!Via.getConfig().isServersideBlockConnections()) return; if (!Via.getConfig().isServersideBlockConnections()) return;
Via.getPlatform().getLogger().info("Loading block connection mappings ..."); Via.getPlatform().getLogger().info("Loading block connection mappings ...");
JsonObject mapping1_13 = MappingDataLoader.loadData("mapping-1.13.json", true); JsonObject mapping1_13 = MappingDataLoader.loadData("mapping-1.13.json", true);
JsonObject blocks1_13 = mapping1_13.getAsJsonObject("blocks"); JsonObject blocks1_13 = mapping1_13.getAsJsonObject("blocks");
for (Entry<String, JsonElement> blockState : blocks1_13.entrySet()) { for (Entry<String, JsonElement> blockState : blocks1_13.entrySet()) {
Integer id = Integer.parseInt(blockState.getKey()); int id = Integer.parseInt(blockState.getKey());
String key = blockState.getValue().getAsString(); String key = blockState.getValue().getAsString();
idToKey.put(id, key); idToKey.put(id, key);
keyToId.put(key, id); keyToId.put(key, id);
} }
connectionHandlerMap = CollectionUtil.createIntObjectMap(3650);
if (!Via.getConfig().isReduceBlockStorageMemory()) { if (!Via.getConfig().isReduceBlockStorageMemory()) {
blockConnectionData = CollectionUtil.createIntObjectMap(1146);
JsonObject mappingBlockConnections = MappingDataLoader.loadData("blockConnections.json"); JsonObject mappingBlockConnections = MappingDataLoader.loadData("blockConnections.json");
for (Entry<String, JsonElement> entry : mappingBlockConnections.entrySet()) { for (Entry<String, JsonElement> entry : mappingBlockConnections.entrySet()) {
int id = keyToId.get(entry.getKey()); int id = keyToId.get(entry.getKey());

Datei anzeigen

@ -3,6 +3,8 @@ package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data;
import com.google.common.collect.ObjectArrays; import com.google.common.collect.ObjectArrays;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import us.myles.ViaVersion.util.GsonUtil; import us.myles.ViaVersion.util.GsonUtil;
import us.myles.ViaVersion.util.fastutil.CollectionUtil;
import us.myles.ViaVersion.util.fastutil.IntObjectMap;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -13,14 +15,14 @@ import java.util.Map;
public class BlockIdData { public class BlockIdData {
public static Map<String, String[]> blockIdMapping; public static Map<String, String[]> blockIdMapping;
public static Map<String, String[]> fallbackReverseMapping; public static Map<String, String[]> fallbackReverseMapping;
public static Map<Integer, String> numberIdToString; public static IntObjectMap<String> numberIdToString;
public static void init() { public static void init() {
InputStream stream = MappingData.class.getClassLoader() InputStream stream = MappingData.class.getClassLoader()
.getResourceAsStream("assets/viaversion/data/blockIds1.12to1.13.json"); .getResourceAsStream("assets/viaversion/data/blockIds1.12to1.13.json");
InputStreamReader reader = new InputStreamReader(stream); InputStreamReader reader = new InputStreamReader(stream);
try { try {
blockIdMapping = new HashMap<>((Map<String, String[]>) GsonUtil.getGson().fromJson( blockIdMapping = new HashMap<>(GsonUtil.getGson().fromJson(
reader, reader,
new TypeToken<Map<String, String[]>>() { new TypeToken<Map<String, String[]>>() {
}.getType() }.getType()
@ -45,11 +47,11 @@ public class BlockIdData {
.getResourceAsStream("assets/viaversion/data/blockNumberToString1.12.json"); .getResourceAsStream("assets/viaversion/data/blockNumberToString1.12.json");
InputStreamReader blockR = new InputStreamReader(blockS); InputStreamReader blockR = new InputStreamReader(blockS);
try { try {
numberIdToString = new HashMap<>((Map<Integer, String>) GsonUtil.getGson().fromJson( numberIdToString = CollectionUtil.createIntObjectMap(new HashMap<>(GsonUtil.getGson().fromJson(
blockR, blockR,
new TypeToken<Map<Integer, String>>() { new TypeToken<Map<Integer, String>>() {
}.getType() }.getType()
)); )));
} finally { } finally {
try { try {
blockR.close(); blockR.close();

Datei anzeigen

@ -0,0 +1,102 @@
package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data;
import us.myles.ViaVersion.util.fastutil.CollectionUtil;
import us.myles.ViaVersion.util.fastutil.IntMap;
public class EntityTypeRewriter {
private static final IntMap ENTITY_TYPES = CollectionUtil.createIntMap(93);
static {
registerEntity(1, 32); // item - ajl
registerEntity(2, 22); // xp_orb - abx
registerEntity(3, 0); // area_effect_cloud - abp
registerEntity(4, 15); // elder_guardian - aju
registerEntity(5, 84); // wither_skeleton - aku
registerEntity(6, 71); // stray - akq
registerEntity(7, 74); // egg - alz
registerEntity(8, 35); // leash_knot - ajb
registerEntity(9, 49); // painting - ajd
registerEntity(10, 2); // arrow - all
registerEntity(11, 67); // snowball - alw
registerEntity(12, 34); // fireball - alq
registerEntity(13, 65); // small_fireball - alv
registerEntity(14, 75); // ender_pearl - ama
registerEntity(15, 23); // eye_of_ender_signal - alo
registerEntity(16, 77); // potion - amc
registerEntity(17, 76); // xp_bottle - amb
registerEntity(18, 33); // item_frame - aja
registerEntity(19, 85); // wither_skull - ame
registerEntity(20, 55); // tnt - ajm
registerEntity(21, 24); // falling_block - ajk
registerEntity(22, 25); // fireworks_rocket - alp
registerEntity(23, 30); // husk - akc
registerEntity(24, 68); // spectral_arrow - alx
registerEntity(25, 60); // shulker_bullet - alu
registerEntity(26, 13); // dragon_fireball - alm
registerEntity(27, 89); // zombie_villager - akw
registerEntity(28, 63); // skeleton_horse - aht
registerEntity(29, 88); // zombie_horse - ahv
registerEntity(30, 1); // armor_stand - aiy
registerEntity(31, 11); // donkey - aho
registerEntity(32, 46); // mule - ahs
registerEntity(33, 20); // evocation_fangs - aln
registerEntity(34, 21); // evocation_illager - ajy
registerEntity(35, 78); // vex - akr
registerEntity(36, 81); // vindication_illager - aks
registerEntity(37, 31); // illusion_illager - akd
registerEntity(40, 41); // commandblock_minecart - aml
registerEntity(41, 5); // boat - ami
registerEntity(42, 39); // minecart - amj
registerEntity(43, 40); // chest_minecart - amk
registerEntity(44, 42); // furnace_minecart - amm
registerEntity(45, 45); // tnt_minecart - amp
registerEntity(46, 43); // hopper_minecart - amn
registerEntity(47, 44); // spawner_minecart - amo
registerEntity(50, 10); // creeper - ajs
registerEntity(51, 62); // skeleton - akm
registerEntity(52, 69); // spider - akp
registerEntity(53, 27); // giant - aka
registerEntity(54, 87); // zombie - akv
registerEntity(55, 64); // slime - akn
registerEntity(56, 26); // ghast - ajz
registerEntity(57, 53); // zombie_pigman - akh
registerEntity(58, 18); // enderman - ajv
registerEntity(59, 6); // cave_spider - ajr
registerEntity(60, 61); // silverfish - akl
registerEntity(61, 4); // blaze - ajq
registerEntity(62, 38); // magma_cube - ake
registerEntity(63, 17); // ender_dragon - aic
registerEntity(64, 83); // wither - aiw
registerEntity(65, 3); // bat - agl
registerEntity(66, 82); // witch - akt
registerEntity(67, 19); // endermite - ajw
registerEntity(68, 28); // guardian - akb
registerEntity(69, 59); // shulker - akk
registerEntity(200, 16); // ender_crystal - aib
registerEntity(90, 51); // pig - agy
registerEntity(91, 58); // sheep - ahd
registerEntity(92, 9); // cow - ags
registerEntity(93, 7); // chicken - agq
registerEntity(94, 70); // squid - ahg
registerEntity(95, 86); // wolf - ahl
registerEntity(96, 47); // mooshroom - agv
registerEntity(97, 66); // snowman - ahf
registerEntity(98, 48); // ocelot - agw
registerEntity(99, 80); // villager_golem - ahj
registerEntity(100, 29); // horse - ahp
registerEntity(101, 56); // rabbit - ahb
registerEntity(102, 54); // polar_bear - agz
registerEntity(103, 36); // llama - ahr
registerEntity(104, 37); // llama_spit - alr
registerEntity(105, 50); // parrot - agx
registerEntity(120, 79); // villager - ala
}
private static void registerEntity(int type1_12, int type1_13) {
ENTITY_TYPES.put(type1_12, type1_13);
}
public static int getNewId(int type1_12) {
return ENTITY_TYPES.getOrDefault(type1_12, type1_12);
}
}

Datei anzeigen

@ -1,7 +1,6 @@
package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.metadata; package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.metadata;
import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.entities.Entity1_12Types;
import us.myles.ViaVersion.api.entities.Entity1_13Types; import us.myles.ViaVersion.api.entities.Entity1_13Types;
import us.myles.ViaVersion.api.entities.EntityType; import us.myles.ViaVersion.api.entities.EntityType;
import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.minecraft.item.Item;
@ -11,6 +10,7 @@ import us.myles.ViaVersion.api.rewriters.MetadataRewriter;
import us.myles.ViaVersion.api.type.types.Particle; import us.myles.ViaVersion.api.type.types.Particle;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.EntityTypeRewriter;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.ParticleRewriter; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.ParticleRewriter;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.packets.InventoryPackets; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.packets.InventoryPackets;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.packets.WorldPackets; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.packets.WorldPackets;
@ -23,7 +23,6 @@ public class MetadataRewriter1_13To1_12_2 extends MetadataRewriter {
public MetadataRewriter1_13To1_12_2(Protocol1_13To1_12_2 protocol) { public MetadataRewriter1_13To1_12_2(Protocol1_13To1_12_2 protocol) {
super(protocol, EntityTracker1_13.class); super(protocol, EntityTracker1_13.class);
mapTypes(Entity1_12Types.EntityType.values(), Entity1_13Types.EntityType.class);
} }
@Override @Override
@ -101,6 +100,11 @@ public class MetadataRewriter1_13To1_12_2 extends MetadataRewriter {
// TODO: Boat has changed // TODO: Boat has changed
} }
@Override
public int getNewEntityId(final int oldId) {
return EntityTypeRewriter.getNewId(oldId);
}
@Override @Override
protected EntityType getTypeFromId(int type) { protected EntityType getTypeFromId(int type) {
return Entity1_13Types.getTypeFromId(type, false); return Entity1_13Types.getTypeFromId(type, false);

Datei anzeigen

@ -16,7 +16,7 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class BlockConnectionStorage extends StoredObject { public class BlockConnectionStorage extends StoredObject {
private static final short[] REVERSE_BLOCK_MAPPINGS = new short[8581]; private static final short[] REVERSE_BLOCK_MAPPINGS = new short[8582];
private static Constructor<?> fastUtilLongObjectHashMap; private static Constructor<?> fastUtilLongObjectHashMap;
private final Map<Long, Pair<byte[], NibbleArray>> blockStorage = createLongObjectMap(); private final Map<Long, Pair<byte[], NibbleArray>> blockStorage = createLongObjectMap();

Datei anzeigen

@ -210,5 +210,4 @@ public enum MetaIndex {
return null; return null;
} }
} }

Datei anzeigen

@ -1,11 +1,8 @@
package us.myles.ViaVersion.util.fastutil; package us.myles.ViaVersion.util.fastutil;
import it.unimi.dsi.fastutil.ints.Int2IntMap; import com.google.common.base.Preconditions;
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -14,9 +11,9 @@ import java.util.Set;
* These should only be used for high access and low/no change collections, since resizing FastUtil collections is expensive. * These should only be used for high access and low/no change collections, since resizing FastUtil collections is expensive.
*/ */
public class CollectionUtil { public class CollectionUtil {
private static final boolean FAST_UTIL = hasFastUtil(); private static final boolean FAST_UTIL = checkForFastUtil();
private static boolean hasFastUtil() { private static boolean checkForFastUtil() {
try { try {
Class.forName("Int2IntMap"); Class.forName("Int2IntMap");
return true; return true;
@ -32,7 +29,8 @@ public class CollectionUtil {
* @return wrapped int map * @return wrapped int map
*/ */
public static IntMap createIntMap(Map<Integer, Integer> originalMap) { public static IntMap createIntMap(Map<Integer, Integer> originalMap) {
return FAST_UTIL ? new WrappedFUIntMap(new Int2IntOpenHashMap(originalMap)) : new WrappedIntMap(originalMap); Preconditions.checkNotNull(originalMap);
return FAST_UTIL ? new WrappedFUIntMap(originalMap) : new WrappedIntMap(originalMap);
} }
/** /**
@ -42,11 +40,32 @@ public class CollectionUtil {
* @return wrapped int map * @return wrapped int map
*/ */
public static IntMap createIntMap(int size) { public static IntMap createIntMap(int size) {
return FAST_UTIL ? new WrappedFUIntMap(new Int2IntOpenHashMap(size)) : new WrappedIntMap(new HashMap<>(size)); return FAST_UTIL ? new WrappedFUIntMap(size) : new WrappedIntMap(size);
} }
public static IntMap createIntMap() { public static IntMap createIntMap() {
return FAST_UTIL ? new WrappedFUIntMap(new Int2IntOpenHashMap()) : new WrappedIntMap(new HashMap<>()); return FAST_UTIL ? new WrappedFUIntMap(16) : new WrappedIntMap(new HashMap<>());
}
/**
* Creates a new FastUtil collection from the given map if present, else simply wraps the original.
*
* @param originalMap map to be reflected
* @return wrapped int map
*/
public static <V> IntObjectMap<V> createIntObjectMap(Map<Integer, V> originalMap) {
Preconditions.checkNotNull(originalMap);
return FAST_UTIL ? new WrappedFUIntObjectMap<>(originalMap) : new WrappedIntObjectMap<>(originalMap);
}
/**
* Creates a new FastUtil collection if present, else simply wraps a normal HashMap.
*
* @param size expected size of the collection
* @return wrapped int map
*/
public static <V> IntObjectMap<V> createIntObjectMap(int size) {
return FAST_UTIL ? new WrappedFUIntObjectMap<>(size) : new WrappedIntObjectMap<>(size);
} }
/** /**
@ -56,7 +75,8 @@ public class CollectionUtil {
* @return wrapped int set * @return wrapped int set
*/ */
public static IntSet createIntSet(Set<Integer> originalSet) { public static IntSet createIntSet(Set<Integer> originalSet) {
return FAST_UTIL ? new WrappedFUIntSet(new IntOpenHashSet(originalSet)) : new WrappedIntSet(originalSet); Preconditions.checkNotNull(originalSet);
return FAST_UTIL ? new WrappedFUIntSet(originalSet) : new WrappedIntSet(originalSet);
} }
/** /**
@ -66,108 +86,10 @@ public class CollectionUtil {
* @return wrapped int set * @return wrapped int set
*/ */
public static IntSet createIntSet(int size) { public static IntSet createIntSet(int size) {
return FAST_UTIL ? new WrappedFUIntSet(new IntOpenHashSet(size)) : new WrappedIntSet(new HashSet<>(size)); return FAST_UTIL ? new WrappedFUIntSet(size) : new WrappedIntSet(size);
} }
private static final class WrappedFUIntMap implements IntMap { public static boolean hasFastUtil() {
private final Int2IntMap map; return FAST_UTIL;
private WrappedFUIntMap(Int2IntMap map) {
this.map = map;
}
@Override
public int getOrDefault(int key, int def) {
return map.getOrDefault(key, def);
}
@Override
public boolean containsKey(int key) {
return map.containsKey(key);
}
@Override
public int put(int key, int value) {
return map.put(key, value);
}
@Override
public int remove(int key) {
return map.remove(key);
}
}
private static final class WrappedIntMap implements IntMap {
private final Map<Integer, Integer> map;
private WrappedIntMap(Map<Integer, Integer> map) {
this.map = map;
}
@Override
public int getOrDefault(int key, int def) {
return map.getOrDefault(key, def);
}
@Override
public boolean containsKey(int key) {
return map.containsKey(key);
}
@Override
public int put(int key, int value) {
return map.put(key, value);
}
@Override
public int remove(int key) {
return map.remove(key);
}
}
private static final class WrappedFUIntSet implements IntSet {
private final it.unimi.dsi.fastutil.ints.IntSet set;
private WrappedFUIntSet(it.unimi.dsi.fastutil.ints.IntSet set) {
this.set = set;
}
@Override
public boolean contains(int key) {
return set.contains(key);
}
@Override
public boolean add(int key) {
return set.add(key);
}
@Override
public boolean remove(int key) {
return set.remove(key);
}
}
private static final class WrappedIntSet implements IntSet {
private final Set<Integer> set;
private WrappedIntSet(Set<Integer> set) {
this.set = set;
}
@Override
public boolean contains(int key) {
return set.contains(key);
}
@Override
public boolean add(int key) {
return set.add(key);
}
@Override
public boolean remove(int key) {
return set.remove(key);
}
} }
} }

Datei anzeigen

@ -14,7 +14,7 @@ public interface IntMap {
} }
/** /**
* @see java.util.HashMap#getOrDefault(Object, Object) (Object) * @see java.util.HashMap#getOrDefault(Object, Object)
*/ */
int getOrDefault(int key, int def); int getOrDefault(int key, int def);

Datei anzeigen

@ -0,0 +1,51 @@
package us.myles.ViaVersion.util.fastutil;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
/**
* Very simple wrapping interface to either be implemented by a HashMap or FastUtil's OpenHashMap.
*
* @param <V> Object value type
*/
public interface IntObjectMap<V> {
/**
* @see java.util.HashMap#get(Object)
*/
@Nullable
V get(int key);
/**
* @see java.util.HashMap#getOrDefault(Object, Object)
*/
@Nullable
V getOrDefault(int key, V def);
/**
* @see java.util.HashMap#containsKey(Object)
*/
boolean containsKey(int key);
/**
* @see java.util.HashMap#put(Object, Object)
*/
@Nullable
V put(int key, V value);
/**
* @see java.util.HashMap#remove(Object)
*/
@Nullable
V remove(int key);
/**
* Returns the underlying map for usage of not implemented methods of this class.
*
* @return original map
* @deprecated will cause wrapping if it is a FastUtil collection
*/
@Deprecated
Map<Integer, V> getMap();
}

Datei anzeigen

@ -0,0 +1,38 @@
package us.myles.ViaVersion.util.fastutil;
import it.unimi.dsi.fastutil.ints.Int2IntMap;
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
import java.util.Map;
final class WrappedFUIntMap implements IntMap {
private final Int2IntMap map;
WrappedFUIntMap(Map<Integer, Integer> originalMap) {
this.map = new Int2IntOpenHashMap(originalMap);
}
WrappedFUIntMap(int size) {
this.map = new Int2IntOpenHashMap(size);
}
@Override
public int getOrDefault(int key, int def) {
return map.getOrDefault(key, def);
}
@Override
public boolean containsKey(int key) {
return map.containsKey(key);
}
@Override
public int put(int key, int value) {
return map.put(key, value);
}
@Override
public int remove(int key) {
return map.remove(key);
}
}

Datei anzeigen

@ -0,0 +1,48 @@
package us.myles.ViaVersion.util.fastutil;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import java.util.Map;
final class WrappedFUIntObjectMap<V> implements IntObjectMap<V> {
private final Int2ObjectMap<V> map;
WrappedFUIntObjectMap(Map<Integer, V> originalMap) {
this.map = new Int2ObjectOpenHashMap<>(originalMap);
}
WrappedFUIntObjectMap(int size) {
this.map = new Int2ObjectOpenHashMap(size);
}
@Override
public V get(int key) {
return map.get(key);
}
@Override
public V getOrDefault(int key, V def) {
return map.getOrDefault(key, def);
}
@Override
public boolean containsKey(int key) {
return map.containsKey(key);
}
@Override
public V put(int key, V value) {
return map.put(key, value);
}
@Override
public V remove(int key) {
return map.remove(key);
}
@Override
public Map<Integer, V> getMap() {
return map;
}
}

Datei anzeigen

@ -0,0 +1,32 @@
package us.myles.ViaVersion.util.fastutil;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import java.util.Set;
final class WrappedFUIntSet implements IntSet {
private final it.unimi.dsi.fastutil.ints.IntSet set;
WrappedFUIntSet(Set<Integer> originalSet) {
this.set = new IntOpenHashSet(originalSet);
}
WrappedFUIntSet(int size) {
this.set = new IntOpenHashSet(size);
}
@Override
public boolean contains(int key) {
return set.contains(key);
}
@Override
public boolean add(int key) {
return set.add(key);
}
@Override
public boolean remove(int key) {
return set.remove(key);
}
}

Datei anzeigen

@ -0,0 +1,37 @@
package us.myles.ViaVersion.util.fastutil;
import java.util.HashMap;
import java.util.Map;
final class WrappedIntMap implements IntMap {
private final Map<Integer, Integer> map;
WrappedIntMap(Map<Integer, Integer> originalMap) {
this.map = originalMap;
}
WrappedIntMap(int size) {
this.map = new HashMap<>(size);
}
@Override
public int getOrDefault(int key, int def) {
return map.getOrDefault(key, def);
}
@Override
public boolean containsKey(int key) {
return map.containsKey(key);
}
@Override
public int put(int key, int value) {
Integer oldValue = map.put(key, value);
return oldValue != null ? oldValue : -1;
}
@Override
public int remove(int key) {
return map.remove(key);
}
}

Datei anzeigen

@ -0,0 +1,46 @@
package us.myles.ViaVersion.util.fastutil;
import java.util.HashMap;
import java.util.Map;
final class WrappedIntObjectMap<V> implements IntObjectMap<V> {
private final Map<Integer, V> map;
WrappedIntObjectMap(Map<Integer, V> originalMap) {
this.map = originalMap;
}
WrappedIntObjectMap(int size) {
this.map = new HashMap<>(size);
}
@Override
public V get(int key) {
return map.get(key);
}
@Override
public V getOrDefault(int key, V def) {
return map.getOrDefault(key, def);
}
@Override
public boolean containsKey(int key) {
return map.containsKey(key);
}
@Override
public V put(int key, V value) {
return map.put(key, value);
}
@Override
public V remove(int key) {
return map.remove(key);
}
@Override
public Map<Integer, V> getMap() {
return map;
}
}

Datei anzeigen

@ -0,0 +1,31 @@
package us.myles.ViaVersion.util.fastutil;
import java.util.HashSet;
import java.util.Set;
final class WrappedIntSet implements IntSet {
private final Set<Integer> set;
WrappedIntSet(Set<Integer> originalSet) {
this.set = originalSet;
}
WrappedIntSet(int size) {
this.set = new HashSet<>(size);
}
@Override
public boolean contains(int key) {
return set.contains(key);
}
@Override
public boolean add(int key) {
return set.add(key);
}
@Override
public boolean remove(int key) {
return set.remove(key);
}
}