3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-27 08:30:09 +01:00

Clean up BiMappings impl

Dieser Commit ist enthalten in:
Nassim Jahnke 2023-03-06 12:42:46 +01:00
Ursprung a3efb676cf
Commit d8a9480bf1
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 6BE3B555EBC5982B
13 geänderte Dateien mit 42 neuen und 117 gelöschten Zeilen

Datei anzeigen

@ -63,7 +63,7 @@ public interface ViaAPI<T> {
* @return API version incremented with meaningful API changes
*/
default int apiVersion() {
return 17;
return 18;
}
/**

Datei anzeigen

@ -29,5 +29,14 @@ public interface BiMappings extends Mappings {
*
* @return inverse of the bimappings
*/
@Override
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);
}
}

Datei anzeigen

@ -22,48 +22,21 @@
*/
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;
private final IntArrayBiMappings inverse;
protected IntArrayBiMappings(final IntArrayMappings mappings) {
protected BiMappingsBase(final Mappings mappings, final Mappings inverse) {
this.mappings = mappings;
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);
this.inverse = new BiMappingsBase(inverse, this);
}
protected IntArrayBiMappings(final Mappings mappings, final IntArrayBiMappings inverse) {
private BiMappingsBase(final Mappings mappings, final BiMappingsBase inverse) {
this.mappings = mappings;
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
public int getNewId(final int id) {
return mappings.getNewId(id);
@ -86,7 +59,7 @@ public class IntArrayBiMappings implements BiMappings {
}
@Override
public Mappings createInverse() {
return mappings.createInverse();
public BiMappings inverse() {
return this.inverse;
}
}

Datei anzeigen

@ -74,5 +74,6 @@ public interface FullMappings extends Mappings {
*/
@Nullable String mappedIdentifier(String identifier);
FullMappings createInverse();
@Override
FullMappings inverse();
}

Datei anzeigen

@ -41,7 +41,7 @@ public class FullMappingsBase implements FullMappings {
this.stringToId = toInverseMap(unmappedIdentifiers);
this.mappedStringToId = toInverseMap(mappedIdentifiers);
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) {
@ -111,8 +111,8 @@ public class FullMappingsBase implements FullMappings {
}
@Override
public FullMappings createInverse() {
return new FullMappingsBase(mappedStringToId, stringToId, mappedIdToString, idToString, mappings.createInverse());
public FullMappings inverse() {
return new FullMappingsBase(mappedStringToId, stringToId, mappedIdToString, idToString, mappings.inverse());
}
private static Object2IntMap<String> toInverseMap(final List<String> list) {

Datei anzeigen

@ -53,7 +53,7 @@ public class IdentityMappings implements Mappings {
}
@Override
public Mappings createInverse() {
public Mappings inverse() {
return new IdentityMappings(mappedSize, size);
}
}

Datei anzeigen

@ -22,9 +22,7 @@
*/
package com.viaversion.viaversion.api.data;
import com.viaversion.viaversion.util.Int2IntBiHashMap;
import com.viaversion.viaversion.util.Int2IntBiMap;
import it.unimi.dsi.fastutil.ints.Int2IntMap;
public class Int2IntMapBiMappings implements BiMappings {
@ -46,11 +44,6 @@ public class Int2IntMapBiMappings implements BiMappings {
return new Int2IntMapBiMappings(mappings);
}
@Override
public BiMappings inverse() {
return this.inverse;
}
@Override
public int getNewId(final int id) {
return mappings.get(id);
@ -72,12 +65,7 @@ public class Int2IntMapBiMappings implements BiMappings {
}
@Override
public Mappings createInverse() {
final Int2IntBiMap inverseCopy = new Int2IntBiHashMap(inverse.mappings.size());
inverseCopy.defaultReturnValue(-1);
for (final Int2IntMap.Entry entry : inverse.mappings.int2IntEntrySet()) {
inverseCopy.put(entry.getIntKey(), entry.getIntValue());
}
return of(inverseCopy);
public BiMappings inverse() {
return this.inverse;
}
}

Datei anzeigen

@ -64,11 +64,13 @@ public class Int2IntMapMappings implements Mappings {
}
@Override
public Mappings createInverse() {
public Mappings inverse() {
final Int2IntMap inverse = new Int2IntOpenHashMap();
inverse.defaultReturnValue(-1);
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());
}

Datei anzeigen

@ -37,6 +37,7 @@ public class IntArrayMappings implements Mappings {
return new IntArrayMappings(mappings, mappedIds);
}
@Deprecated
public static Builder<IntArrayMappings> builder() {
return Mappings.builder(IntArrayMappings::new);
}
@ -62,12 +63,12 @@ public class IntArrayMappings implements Mappings {
}
@Override
public Mappings createInverse() {
public Mappings inverse() {
final int[] inverse = new int[mappedIds];
Arrays.fill(inverse, -1);
for (int id = 0; id < mappings.length; id++) {
final int mappedId = mappings[id];
if (mappedId != -1) {
if (mappedId != -1 && inverse[mappedId] == -1) {
inverse[mappedId] = id;
}
}

Datei anzeigen

@ -110,20 +110,16 @@ public class MappingDataBase implements MappingData {
}
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) {
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) {
if (!shouldLoad(key)) {
return null;
}
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) {
@ -246,10 +242,6 @@ public class MappingDataBase implements MappingData {
return mappedId;
}
protected boolean shouldLoad(final String key) {
return true;
}
protected void loadExtras(final CompoundTag data) {
}
}

Datei anzeigen

@ -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) {
final Object2IntMap<String> newIdentifierMap = MappingDataLoader.indexedObjectToMap(mappedIdentifiers);
for (final Map.Entry<String, JsonElement> entry : unmappedIdentifiers.entrySet()) {
@ -285,6 +286,7 @@ public final class MappingDataLoader {
return mappedId;
}
@Deprecated
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);
for (int id = 0; id < unmappedIdentifiers.size(); id++) {

Datei anzeigen

@ -84,11 +84,11 @@ public interface Mappings {
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) {
return new Builder<>(supplier);
@ -100,6 +100,7 @@ public interface Mappings {
T supply(int[] mappings, int mappedIds);
}
@Deprecated
class Builder<T extends Mappings> {
protected final MappingsSupplier<T> supplier;

Datei anzeigen

@ -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());
}
}