mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-06 16:37:38 +01:00
Cleanup ArrayUtils
This commit is contained in:
parent
f4d280a240
commit
2ee1470470
@ -1,30 +1,28 @@
|
|||||||
package net.minestom.server.utils;
|
package net.minestom.server.utils;
|
||||||
|
|
||||||
import it.unimi.dsi.fastutil.ints.IntList;
|
import it.unimi.dsi.fastutil.ints.IntList;
|
||||||
import net.minestom.server.utils.validate.Check;
|
import org.jetbrains.annotations.ApiStatus;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.function.Supplier;
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@ApiStatus.Internal
|
||||||
public final class ArrayUtils {
|
public final class ArrayUtils {
|
||||||
|
|
||||||
private ArrayUtils() {
|
private ArrayUtils() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int[] concatenateIntArrays(@NotNull int[]... arrays) {
|
public static int[] concatenateIntArrays(int @NotNull []... arrays) {
|
||||||
int totalLength = 0;
|
int totalLength = 0;
|
||||||
for (int[] array : arrays) {
|
for (int[] array : arrays) {
|
||||||
totalLength += array.length;
|
totalLength += array.length;
|
||||||
}
|
}
|
||||||
int[] result = new int[totalLength];
|
int[] result = new int[totalLength];
|
||||||
|
|
||||||
int startingPos = 0;
|
int startingPos = 0;
|
||||||
for (int[] array : arrays) {
|
for (int[] array : arrays) {
|
||||||
System.arraycopy(array, 0, result, startingPos, array.length);
|
System.arraycopy(array, 0, result, startingPos, array.length);
|
||||||
startingPos += array.length;
|
startingPos += array.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,11 +30,6 @@ public final class ArrayUtils {
|
|||||||
System.arraycopy(arr, index + 1, arr, index, arr.length - 1 - index);
|
System.arraycopy(arr, index + 1, arr, index, arr.length - 1 - index);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void copyToDestination(short[] src, short[] dest) {
|
|
||||||
Check.argCondition(src.length != dest.length, "The two arrays need to have the same length.");
|
|
||||||
System.arraycopy(src, 0, dest, 0, src.length);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the differences between 2 arrays.
|
* Gets the differences between 2 arrays.
|
||||||
*
|
*
|
||||||
@ -44,8 +37,7 @@ public final class ArrayUtils {
|
|||||||
* @param b the second array
|
* @param b the second array
|
||||||
* @return an array containing a's indexes that aren't in b array
|
* @return an array containing a's indexes that aren't in b array
|
||||||
*/
|
*/
|
||||||
@NotNull
|
public static int @NotNull [] getDifferencesBetweenArray(long @NotNull [] a, long @NotNull [] b) {
|
||||||
public static int[] getDifferencesBetweenArray(@NotNull long[] a, @NotNull long[] b) {
|
|
||||||
int counter = 0;
|
int counter = 0;
|
||||||
int[] indexes = new int[Math.max(a.length, b.length)];
|
int[] indexes = new int[Math.max(a.length, b.length)];
|
||||||
|
|
||||||
@ -69,50 +61,30 @@ public final class ArrayUtils {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
public static int @NotNull [] toArray(@NotNull IntList list) {
|
||||||
public static int[] toArray(@NotNull IntList list) {
|
|
||||||
int[] array = new int[list.size()];
|
int[] array = new int[list.size()];
|
||||||
for (int i = 0; i < array.length; i++) {
|
list.getElements(0, array, 0, array.length);
|
||||||
array[i] = list.getInt(i);
|
|
||||||
}
|
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets if two arrays share the same start until {@code length}.
|
* Gets if two arrays share the same start until {@code length}.
|
||||||
*
|
*
|
||||||
* @param array1 the first array
|
* @param first the first array
|
||||||
* @param array2 the second array
|
* @param second the second array
|
||||||
* @param length the length to check (0-length)
|
* @param length the length to check (0-length)
|
||||||
* @param <T> the type of the arrays
|
* @param <T> the type of the arrays
|
||||||
* @return true if both arrays share the same start
|
* @return true if both arrays share the same start
|
||||||
*/
|
*/
|
||||||
public static <T> boolean sameStart(T[] array1, T[] array2, int length) {
|
public static <T> boolean sameStart(@NotNull T[] first, @NotNull T[] second, int length) {
|
||||||
if (length > array1.length || length > array2.length) {
|
if (Math.min(first.length, second.length) < length) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < length; i++) {
|
for (int i = 0; i < length; i++) {
|
||||||
final T value1 = array1[i];
|
if (!Objects.equals(first[i], second[i])) {
|
||||||
final T value2 = array2[i];
|
|
||||||
if (!value1.equals(value2)) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Fills an array using a supplier.
|
|
||||||
*
|
|
||||||
* @param array the array to fill
|
|
||||||
* @param supplier the supplier to fill the array
|
|
||||||
* @param <T> the array type
|
|
||||||
*/
|
|
||||||
public static <T> void fill(@NotNull T[] array, @NotNull Supplier<T> supplier) {
|
|
||||||
for (int i = 0; i < array.length; i++) {
|
|
||||||
array[i] = supplier.get();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user