Mirror von
https://github.com/ViaVersion/ViaVersion.git
synchronisiert 2024-11-03 14:50:30 +01:00
Clean up BiMappings impl
Dieser Commit ist enthalten in:
Ursprung
a3efb676cf
Commit
d8a9480bf1
@ -63,7 +63,7 @@ public interface ViaAPI<T> {
|
|||||||
* @return API version incremented with meaningful API changes
|
* @return API version incremented with meaningful API changes
|
||||||
*/
|
*/
|
||||||
default int apiVersion() {
|
default int apiVersion() {
|
||||||
return 17;
|
return 18;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -29,5 +29,14 @@ public interface BiMappings extends Mappings {
|
|||||||
*
|
*
|
||||||
* @return inverse of the bimappings
|
* @return inverse of the bimappings
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
BiMappings inverse();
|
BiMappings inverse();
|
||||||
|
|
||||||
|
static BiMappings of(final Mappings mappings) {
|
||||||
|
return of(mappings, mappings.inverse());
|
||||||
|
}
|
||||||
|
|
||||||
|
static BiMappings of(final Mappings mappings, final Mappings inverse) {
|
||||||
|
return new BiMappingsBase(mappings, inverse);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,48 +22,21 @@
|
|||||||
*/
|
*/
|
||||||
package com.viaversion.viaversion.api.data;
|
package com.viaversion.viaversion.api.data;
|
||||||
|
|
||||||
import java.util.Arrays;
|
public class BiMappingsBase implements BiMappings {
|
||||||
|
|
||||||
public class IntArrayBiMappings implements BiMappings {
|
protected final Mappings mappings;
|
||||||
|
private final BiMappingsBase inverse;
|
||||||
|
|
||||||
private final Mappings mappings;
|
protected BiMappingsBase(final Mappings mappings, final Mappings inverse) {
|
||||||
private final IntArrayBiMappings inverse;
|
|
||||||
|
|
||||||
protected IntArrayBiMappings(final IntArrayMappings mappings) {
|
|
||||||
this.mappings = mappings;
|
this.mappings = mappings;
|
||||||
|
this.inverse = new BiMappingsBase(inverse, this);
|
||||||
final int[] raw = mappings.raw();
|
|
||||||
final int[] inverseMappings = new int[mappings.mappedSize()];
|
|
||||||
Arrays.fill(inverseMappings, -1);
|
|
||||||
for (int id = 0; id < raw.length; id++) {
|
|
||||||
final int mappedId = raw[id];
|
|
||||||
inverseMappings[mappedId] = id;
|
|
||||||
}
|
|
||||||
this.inverse = new IntArrayBiMappings(new IntArrayMappings(inverseMappings, raw.length), this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected IntArrayBiMappings(final Mappings mappings, final IntArrayBiMappings inverse) {
|
private BiMappingsBase(final Mappings mappings, final BiMappingsBase inverse) {
|
||||||
this.mappings = mappings;
|
this.mappings = mappings;
|
||||||
this.inverse = inverse;
|
this.inverse = inverse;
|
||||||
}
|
}
|
||||||
|
|
||||||
private IntArrayBiMappings(final int[] mappings, final int mappedIds) {
|
|
||||||
this(new IntArrayMappings(mappings, mappedIds));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static IntArrayBiMappings of(final IntArrayMappings mappings) {
|
|
||||||
return new IntArrayBiMappings(mappings);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Mappings.Builder<IntArrayBiMappings> builder() {
|
|
||||||
return new Builder<>(IntArrayBiMappings::new);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public BiMappings inverse() {
|
|
||||||
return this.inverse;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getNewId(final int id) {
|
public int getNewId(final int id) {
|
||||||
return mappings.getNewId(id);
|
return mappings.getNewId(id);
|
||||||
@ -86,7 +59,7 @@ public class IntArrayBiMappings implements BiMappings {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Mappings createInverse() {
|
public BiMappings inverse() {
|
||||||
return mappings.createInverse();
|
return this.inverse;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -74,5 +74,6 @@ public interface FullMappings extends Mappings {
|
|||||||
*/
|
*/
|
||||||
@Nullable String mappedIdentifier(String identifier);
|
@Nullable String mappedIdentifier(String identifier);
|
||||||
|
|
||||||
FullMappings createInverse();
|
@Override
|
||||||
|
FullMappings inverse();
|
||||||
}
|
}
|
||||||
|
@ -41,7 +41,7 @@ public class FullMappingsBase implements FullMappings {
|
|||||||
this.stringToId = toInverseMap(unmappedIdentifiers);
|
this.stringToId = toInverseMap(unmappedIdentifiers);
|
||||||
this.mappedStringToId = toInverseMap(mappedIdentifiers);
|
this.mappedStringToId = toInverseMap(mappedIdentifiers);
|
||||||
this.idToString = unmappedIdentifiers.toArray(EMPTY_ARRAY);
|
this.idToString = unmappedIdentifiers.toArray(EMPTY_ARRAY);
|
||||||
this. mappedIdToString = mappedIdentifiers.toArray(EMPTY_ARRAY);
|
this.mappedIdToString = mappedIdentifiers.toArray(EMPTY_ARRAY);
|
||||||
}
|
}
|
||||||
|
|
||||||
private FullMappingsBase(final Object2IntMap<String> stringToId, final Object2IntMap<String> mappedStringToId, final String[] idToString, final String[] mappedIdToString, final Mappings mappings) {
|
private FullMappingsBase(final Object2IntMap<String> stringToId, final Object2IntMap<String> mappedStringToId, final String[] idToString, final String[] mappedIdToString, final Mappings mappings) {
|
||||||
@ -111,8 +111,8 @@ public class FullMappingsBase implements FullMappings {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FullMappings createInverse() {
|
public FullMappings inverse() {
|
||||||
return new FullMappingsBase(mappedStringToId, stringToId, mappedIdToString, idToString, mappings.createInverse());
|
return new FullMappingsBase(mappedStringToId, stringToId, mappedIdToString, idToString, mappings.inverse());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Object2IntMap<String> toInverseMap(final List<String> list) {
|
private static Object2IntMap<String> toInverseMap(final List<String> list) {
|
||||||
|
@ -53,7 +53,7 @@ public class IdentityMappings implements Mappings {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Mappings createInverse() {
|
public Mappings inverse() {
|
||||||
return new IdentityMappings(mappedSize, size);
|
return new IdentityMappings(mappedSize, size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,9 +22,7 @@
|
|||||||
*/
|
*/
|
||||||
package com.viaversion.viaversion.api.data;
|
package com.viaversion.viaversion.api.data;
|
||||||
|
|
||||||
import com.viaversion.viaversion.util.Int2IntBiHashMap;
|
|
||||||
import com.viaversion.viaversion.util.Int2IntBiMap;
|
import com.viaversion.viaversion.util.Int2IntBiMap;
|
||||||
import it.unimi.dsi.fastutil.ints.Int2IntMap;
|
|
||||||
|
|
||||||
public class Int2IntMapBiMappings implements BiMappings {
|
public class Int2IntMapBiMappings implements BiMappings {
|
||||||
|
|
||||||
@ -46,11 +44,6 @@ public class Int2IntMapBiMappings implements BiMappings {
|
|||||||
return new Int2IntMapBiMappings(mappings);
|
return new Int2IntMapBiMappings(mappings);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public BiMappings inverse() {
|
|
||||||
return this.inverse;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getNewId(final int id) {
|
public int getNewId(final int id) {
|
||||||
return mappings.get(id);
|
return mappings.get(id);
|
||||||
@ -72,12 +65,7 @@ public class Int2IntMapBiMappings implements BiMappings {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Mappings createInverse() {
|
public BiMappings inverse() {
|
||||||
final Int2IntBiMap inverseCopy = new Int2IntBiHashMap(inverse.mappings.size());
|
return this.inverse;
|
||||||
inverseCopy.defaultReturnValue(-1);
|
|
||||||
for (final Int2IntMap.Entry entry : inverse.mappings.int2IntEntrySet()) {
|
|
||||||
inverseCopy.put(entry.getIntKey(), entry.getIntValue());
|
|
||||||
}
|
|
||||||
return of(inverseCopy);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -64,11 +64,13 @@ public class Int2IntMapMappings implements Mappings {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Mappings createInverse() {
|
public Mappings inverse() {
|
||||||
final Int2IntMap inverse = new Int2IntOpenHashMap();
|
final Int2IntMap inverse = new Int2IntOpenHashMap();
|
||||||
inverse.defaultReturnValue(-1);
|
inverse.defaultReturnValue(-1);
|
||||||
for (final Int2IntMap.Entry entry : mappings.int2IntEntrySet()) {
|
for (final Int2IntMap.Entry entry : mappings.int2IntEntrySet()) {
|
||||||
inverse.put(entry.getIntValue(), entry.getIntKey());
|
if (entry.getIntValue() != -1) {
|
||||||
|
inverse.putIfAbsent(entry.getIntValue(), entry.getIntKey());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return of(inverse, size());
|
return of(inverse, size());
|
||||||
}
|
}
|
||||||
|
@ -37,6 +37,7 @@ public class IntArrayMappings implements Mappings {
|
|||||||
return new IntArrayMappings(mappings, mappedIds);
|
return new IntArrayMappings(mappings, mappedIds);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
public static Builder<IntArrayMappings> builder() {
|
public static Builder<IntArrayMappings> builder() {
|
||||||
return Mappings.builder(IntArrayMappings::new);
|
return Mappings.builder(IntArrayMappings::new);
|
||||||
}
|
}
|
||||||
@ -62,12 +63,12 @@ public class IntArrayMappings implements Mappings {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Mappings createInverse() {
|
public Mappings inverse() {
|
||||||
final int[] inverse = new int[mappedIds];
|
final int[] inverse = new int[mappedIds];
|
||||||
Arrays.fill(inverse, -1);
|
Arrays.fill(inverse, -1);
|
||||||
for (int id = 0; id < mappings.length; id++) {
|
for (int id = 0; id < mappings.length; id++) {
|
||||||
final int mappedId = mappings[id];
|
final int mappedId = mappings[id];
|
||||||
if (mappedId != -1) {
|
if (mappedId != -1 && inverse[mappedId] == -1) {
|
||||||
inverse[mappedId] = id;
|
inverse[mappedId] = id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -110,20 +110,16 @@ public class MappingDataBase implements MappingData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected @Nullable Mappings loadMappings(final CompoundTag data, final String key) {
|
protected @Nullable Mappings loadMappings(final CompoundTag data, final String key) {
|
||||||
return shouldLoad(key) ? MappingDataLoader.loadMappings(data, key) : null;
|
return MappingDataLoader.loadMappings(data, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected @Nullable FullMappings loadFullMappings(final CompoundTag data, final CompoundTag unmappedIdentifiers, final CompoundTag mappedIdentifiers, final String key) {
|
protected @Nullable FullMappings loadFullMappings(final CompoundTag data, final CompoundTag unmappedIdentifiers, final CompoundTag mappedIdentifiers, final String key) {
|
||||||
return shouldLoad(key) ? MappingDataLoader.loadFullMappings(data, unmappedIdentifiers, mappedIdentifiers, key) : null;
|
return MappingDataLoader.loadFullMappings(data, unmappedIdentifiers, mappedIdentifiers, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected @Nullable BiMappings loadBiMappings(final CompoundTag data, final String key) {
|
protected @Nullable BiMappings loadBiMappings(final CompoundTag data, final String key) {
|
||||||
if (!shouldLoad(key)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
final Mappings mappings = loadMappings(data, key);
|
final Mappings mappings = loadMappings(data, key);
|
||||||
return mappings != null ? IntArrayBiMappings.of((IntArrayMappings) mappings) : null;
|
return mappings != null ? BiMappings.of(mappings) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadTags(final RegistryType type, final CompoundTag data) {
|
private void loadTags(final RegistryType type, final CompoundTag data) {
|
||||||
@ -246,10 +242,6 @@ public class MappingDataBase implements MappingData {
|
|||||||
return mappedId;
|
return mappedId;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean shouldLoad(final String key) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void loadExtras(final CompoundTag data) {
|
protected void loadExtras(final CompoundTag data) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -248,6 +248,7 @@ public final class MappingDataLoader {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
public static void mapIdentifiers(final int[] output, final JsonObject unmappedIdentifiers, final JsonObject mappedIdentifiers, @Nullable final JsonObject diffIdentifiers, final boolean warnOnMissing) {
|
public static void mapIdentifiers(final int[] output, final JsonObject unmappedIdentifiers, final JsonObject mappedIdentifiers, @Nullable final JsonObject diffIdentifiers, final boolean warnOnMissing) {
|
||||||
final Object2IntMap<String> newIdentifierMap = MappingDataLoader.indexedObjectToMap(mappedIdentifiers);
|
final Object2IntMap<String> newIdentifierMap = MappingDataLoader.indexedObjectToMap(mappedIdentifiers);
|
||||||
for (final Map.Entry<String, JsonElement> entry : unmappedIdentifiers.entrySet()) {
|
for (final Map.Entry<String, JsonElement> entry : unmappedIdentifiers.entrySet()) {
|
||||||
@ -285,6 +286,7 @@ public final class MappingDataLoader {
|
|||||||
return mappedId;
|
return mappedId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
public static void mapIdentifiers(final int[] output, final JsonArray unmappedIdentifiers, final JsonArray mappedIdentifiers, @Nullable final JsonObject diffIdentifiers, final boolean warnOnMissing) {
|
public static void mapIdentifiers(final int[] output, final JsonArray unmappedIdentifiers, final JsonArray mappedIdentifiers, @Nullable final JsonObject diffIdentifiers, final boolean warnOnMissing) {
|
||||||
final Object2IntMap<String> newIdentifierMap = MappingDataLoader.arrayToMap(mappedIdentifiers);
|
final Object2IntMap<String> newIdentifierMap = MappingDataLoader.arrayToMap(mappedIdentifiers);
|
||||||
for (int id = 0; id < unmappedIdentifiers.size(); id++) {
|
for (int id = 0; id < unmappedIdentifiers.size(); id++) {
|
||||||
|
@ -84,11 +84,11 @@ public interface Mappings {
|
|||||||
int mappedSize();
|
int mappedSize();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new Mappings instance of the same type with the same mappings, but with the keys and values swapped.
|
* Mappings with keys and values swapped.
|
||||||
*
|
*
|
||||||
* @return a new Mappings instance with the same mappings, but with the keys and values swapped
|
* @return mappings with keys and values swapped
|
||||||
*/
|
*/
|
||||||
Mappings createInverse();
|
Mappings inverse();
|
||||||
|
|
||||||
static <T extends Mappings> Builder<T> builder(final MappingsSupplier<T> supplier) {
|
static <T extends Mappings> Builder<T> builder(final MappingsSupplier<T> supplier) {
|
||||||
return new Builder<>(supplier);
|
return new Builder<>(supplier);
|
||||||
@ -100,6 +100,7 @@ public interface Mappings {
|
|||||||
T supply(int[] mappings, int mappedIds);
|
T supply(int[] mappings, int mappedIds);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
class Builder<T extends Mappings> {
|
class Builder<T extends Mappings> {
|
||||||
|
|
||||||
protected final MappingsSupplier<T> supplier;
|
protected final MappingsSupplier<T> supplier;
|
||||||
|
@ -1,44 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
|
|
||||||
* Copyright (C) 2023 ViaVersion and contributors
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in all
|
|
||||||
* copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
package com.viaversion.viaversion.api.data.entity;
|
|
||||||
|
|
||||||
import com.viaversion.viaversion.api.data.BiMappings;
|
|
||||||
import com.viaversion.viaversion.api.data.IdentityMappings;
|
|
||||||
import com.viaversion.viaversion.api.data.Mappings;
|
|
||||||
|
|
||||||
public final class IdentityBiMappings extends IdentityMappings implements BiMappings {
|
|
||||||
|
|
||||||
public IdentityBiMappings(final int size, final int mappedSize) {
|
|
||||||
super(size, mappedSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public BiMappings inverse() {
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Mappings createInverse() {
|
|
||||||
return new IdentityBiMappings(mappedSize(), size());
|
|
||||||
}
|
|
||||||
}
|
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren