2021-04-27 13:25:18 +02:00
|
|
|
/*
|
|
|
|
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
|
2023-01-12 12:45:53 +01:00
|
|
|
* Copyright (C) 2016-2023 ViaVersion and contributors
|
2021-04-27 13:25:18 +02:00
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
2023-03-04 17:17:47 +01:00
|
|
|
import java.util.Arrays;
|
|
|
|
|
2021-04-27 13:25:18 +02:00
|
|
|
public class IntArrayMappings implements Mappings {
|
2023-03-03 22:00:00 +01:00
|
|
|
private final int[] mappings;
|
2021-11-15 21:49:02 +01:00
|
|
|
private final int mappedIds;
|
2021-04-27 13:25:18 +02:00
|
|
|
|
2023-03-03 22:00:00 +01:00
|
|
|
protected IntArrayMappings(final int[] mappings, final int mappedIds) {
|
|
|
|
this.mappings = mappings;
|
2021-11-15 21:49:02 +01:00
|
|
|
this.mappedIds = mappedIds;
|
|
|
|
}
|
|
|
|
|
2023-03-03 22:00:00 +01:00
|
|
|
public static IntArrayMappings of(final int[] mappings, final int mappedIds) {
|
|
|
|
return new IntArrayMappings(mappings, mappedIds);
|
2021-11-16 13:15:27 +01:00
|
|
|
}
|
|
|
|
|
2021-04-27 13:25:18 +02:00
|
|
|
@Override
|
|
|
|
public int getNewId(int id) {
|
2023-03-03 22:00:00 +01:00
|
|
|
return id >= 0 && id < mappings.length ? mappings[id] : -1;
|
2021-04-27 13:25:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-03-03 22:00:00 +01:00
|
|
|
public void setNewId(int id, int mappedId) {
|
|
|
|
mappings[id] = mappedId;
|
2021-04-27 13:25:18 +02:00
|
|
|
}
|
|
|
|
|
2021-09-16 18:12:33 +02:00
|
|
|
@Override
|
|
|
|
public int size() {
|
2023-03-03 22:00:00 +01:00
|
|
|
return mappings.length;
|
2021-09-16 18:12:33 +02:00
|
|
|
}
|
|
|
|
|
2021-11-15 21:49:02 +01:00
|
|
|
@Override
|
|
|
|
public int mappedSize() {
|
|
|
|
return mappedIds;
|
|
|
|
}
|
|
|
|
|
2023-03-04 17:17:47 +01:00
|
|
|
@Override
|
2023-03-06 12:42:46 +01:00
|
|
|
public Mappings inverse() {
|
2023-03-04 17:17:47 +01:00
|
|
|
final int[] inverse = new int[mappedIds];
|
|
|
|
Arrays.fill(inverse, -1);
|
|
|
|
for (int id = 0; id < mappings.length; id++) {
|
|
|
|
final int mappedId = mappings[id];
|
2023-03-06 12:42:46 +01:00
|
|
|
if (mappedId != -1 && inverse[mappedId] == -1) {
|
2023-03-05 14:45:58 +01:00
|
|
|
inverse[mappedId] = id;
|
|
|
|
}
|
2023-03-04 17:17:47 +01:00
|
|
|
}
|
|
|
|
return of(inverse, mappings.length);
|
|
|
|
}
|
|
|
|
|
2023-03-03 22:00:00 +01:00
|
|
|
public int[] raw() {
|
|
|
|
return mappings;
|
2021-04-27 13:25:18 +02:00
|
|
|
}
|
|
|
|
}
|