2022-06-18 18:04:24 +00:00
|
|
|
using System;
|
2019-09-10 07:21:51 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Array reusable logic
|
|
|
|
/// </summary>
|
|
|
|
public static class ArrayUtil
|
2019-09-10 07:21:51 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
public static bool IsRangeEmpty(this ReadOnlySpan<byte> data, byte value = 0)
|
2019-09-10 07:21:51 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
for (int i = data.Length - 1; i >= 0; i--)
|
2019-09-10 07:21:51 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (data[i] != value)
|
|
|
|
return false;
|
2019-09-10 07:21:51 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-09-10 07:21:51 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static int Count<T>(this Span<T> data, T value) where T : IEquatable<T>
|
|
|
|
{
|
|
|
|
return ((ReadOnlySpan<T>)data).Count(value);
|
|
|
|
}
|
2022-01-03 05:35:59 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static T Find<T>(this Span<T> data, Func<T, bool> value) where T : unmanaged
|
|
|
|
{
|
|
|
|
foreach (var x in data)
|
2022-03-14 02:24:08 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (value(x))
|
|
|
|
return x;
|
2022-03-14 02:24:08 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return default;
|
|
|
|
}
|
2022-03-14 02:24:08 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static int Count<T>(this ReadOnlySpan<T> data, T value) where T : IEquatable<T>
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
for (int i = data.Length - 1; i >= 0; i--)
|
2022-01-03 05:35:59 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (data[i].Equals(value))
|
|
|
|
count++;
|
2022-01-03 05:35:59 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return count;
|
|
|
|
}
|
2022-02-05 01:35:15 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static byte[] Slice(this byte[] src, int offset, int length) => src.AsSpan(offset, length).ToArray();
|
|
|
|
public static T[] Slice<T>(this T[] src, int offset, int length) => src.AsSpan(offset, length).ToArray();
|
2019-10-25 01:10:23 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static bool WithinRange(int value, int min, int max) => min <= value && value < max;
|
2019-09-10 07:21:51 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static IEnumerable<T[]> EnumerateSplit<T>(T[] bin, int size, int start = 0)
|
|
|
|
{
|
|
|
|
for (int i = start; i < bin.Length; i += size)
|
|
|
|
yield return bin.Slice(i, size);
|
|
|
|
}
|
2019-09-10 07:21:51 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static bool[] GitBitFlagArray(ReadOnlySpan<byte> data, int count)
|
|
|
|
{
|
|
|
|
bool[] result = new bool[count];
|
|
|
|
for (int i = 0; i < result.Length; i++)
|
|
|
|
result[i] = ((data[i >> 3] >> (i & 7)) & 0x1) == 1;
|
|
|
|
return result;
|
|
|
|
}
|
2019-09-12 01:39:49 +00:00
|
|
|
|
Refactoring: Move Source (Legality) (#3560)
Rewrites a good amount of legality APIs pertaining to:
* Legal moves that can be learned
* Evolution chains & cross-generation paths
* Memory validation with forgotten moves
In generation 8, there are 3 separate contexts an entity can exist in: SW/SH, BD/SP, and LA. Not every entity can cross between them, and not every entity from generation 7 can exist in generation 8 (Gogoat, etc). By creating class models representing the restrictions to cross each boundary, we are able to better track and validate data.
The old implementation of validating moves was greedy: it would iterate for all generations and evolutions, and build a full list of every move that can be learned, storing it on the heap. Now, we check one game group at a time to see if the entity can learn a move that hasn't yet been validated. End result is an algorithm that requires 0 allocation, and a smaller/quicker search space.
The old implementation of storing move parses was inefficient; for each move that was parsed, a new object is created and adjusted depending on the parse. Now, move parse results are `struct` and store the move parse contiguously in memory. End result is faster parsing and 0 memory allocation.
* `PersonalTable` objects have been improved with new API methods to check if a species+form can exist in the game.
* `IEncounterTemplate` objects have been improved to indicate the `EntityContext` they originate in (similar to `Generation`).
* Some APIs have been extended to accept `Span<T>` instead of Array/IEnumerable
2022-08-03 23:15:27 +00:00
|
|
|
public static void SetBitFlagArray(Span<byte> data, ReadOnlySpan<bool> value)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
for (int i = 0; i < value.Length; i++)
|
2019-09-12 01:39:49 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var ofs = i >> 3;
|
|
|
|
var mask = (1 << (i & 7));
|
|
|
|
if (value[i])
|
|
|
|
data[ofs] |= (byte)mask;
|
|
|
|
else
|
|
|
|
data[ofs] &= (byte)~mask;
|
2019-09-12 01:39:49 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2019-09-12 01:39:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Copies a <see cref="T"/> list to the destination list, with an option to copy to a starting point.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="list">Source list to copy from</param>
|
|
|
|
/// <param name="dest">Destination list/array</param>
|
|
|
|
/// <param name="skip">Criteria for skipping a slot</param>
|
|
|
|
/// <param name="start">Starting point to copy to</param>
|
|
|
|
/// <returns>Count of <see cref="T"/> copied.</returns>
|
|
|
|
public static int CopyTo<T>(this IEnumerable<T> list, IList<T> dest, Func<int, bool> skip, int start = 0)
|
|
|
|
{
|
|
|
|
int ctr = start;
|
|
|
|
int skipped = 0;
|
|
|
|
foreach (var z in list)
|
|
|
|
{
|
|
|
|
// seek forward to next open slot
|
|
|
|
int next = FindNextValidIndex(dest, skip, ctr);
|
|
|
|
if (next == -1)
|
|
|
|
break;
|
|
|
|
skipped += next - ctr;
|
|
|
|
ctr = next;
|
|
|
|
dest[ctr++] = z;
|
|
|
|
}
|
|
|
|
return ctr - start - skipped;
|
|
|
|
}
|
2019-09-19 02:58:23 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static int FindNextValidIndex<T>(IList<T> dest, Func<int, bool> skip, int ctr)
|
|
|
|
{
|
|
|
|
while (true)
|
2019-09-19 02:58:23 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if ((uint)ctr >= dest.Count)
|
|
|
|
return -1;
|
|
|
|
var exist = dest[ctr];
|
|
|
|
if (exist == null || !skip(ctr))
|
|
|
|
return ctr;
|
|
|
|
ctr++;
|
2019-09-19 02:58:23 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2019-09-19 02:58:23 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Copies an <see cref="IEnumerable{T}"/> list to the destination list, with an option to copy to a starting point.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T">Typed object to copy</typeparam>
|
|
|
|
/// <param name="list">Source list to copy from</param>
|
|
|
|
/// <param name="dest">Destination list/array</param>
|
|
|
|
/// <param name="start">Starting point to copy to</param>
|
|
|
|
/// <returns>Count of <see cref="T"/> copied.</returns>
|
|
|
|
public static int CopyTo<T>(this IEnumerable<T> list, IList<T> dest, int start = 0)
|
|
|
|
{
|
|
|
|
int ctr = start;
|
|
|
|
foreach (var z in list)
|
2019-09-19 02:58:23 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if ((uint)ctr >= dest.Count)
|
|
|
|
break;
|
|
|
|
dest[ctr++] = z;
|
2019-09-19 02:58:23 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return ctr - start;
|
|
|
|
}
|
2019-11-16 03:50:22 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
internal static T[] ConcatAll<T>(params T[][] arr)
|
|
|
|
{
|
|
|
|
int len = 0;
|
|
|
|
foreach (var a in arr)
|
|
|
|
len += a.Length;
|
2019-11-16 03:50:22 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
var result = new T[len];
|
2020-07-19 21:35:52 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
int ctr = 0;
|
|
|
|
foreach (var a in arr)
|
2020-07-19 21:35:52 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
a.CopyTo(result, ctr);
|
|
|
|
ctr += a.Length;
|
2020-07-19 21:35:52 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return result;
|
|
|
|
}
|
2021-05-07 06:26:38 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
internal static T[] ConcatAll<T>(T[] arr1, T[] arr2)
|
|
|
|
{
|
|
|
|
int len = arr1.Length + arr2.Length;
|
|
|
|
var result = new T[len];
|
|
|
|
arr1.CopyTo(result, 0);
|
|
|
|
arr2.CopyTo(result, arr1.Length);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static T[] ConcatAll<T>(T[] arr1, T[] arr2, T[] arr3)
|
|
|
|
{
|
|
|
|
int len = arr1.Length + arr2.Length + arr3.Length;
|
|
|
|
var result = new T[len];
|
|
|
|
arr1.CopyTo(result, 0);
|
|
|
|
arr2.CopyTo(result, arr1.Length);
|
|
|
|
arr3.CopyTo(result, arr1.Length + arr2.Length);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-11-25 01:42:17 +00:00
|
|
|
internal static T[] ConcatAll<T>(T[] arr1, ReadOnlySpan<T> arr2)
|
|
|
|
{
|
|
|
|
int len = arr1.Length + arr2.Length;
|
|
|
|
var result = new T[len];
|
|
|
|
arr1.CopyTo(result, 0);
|
|
|
|
arr2.CopyTo(result.AsSpan(arr1.Length));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
internal static T[] ConcatAll<T>(T[] arr1, T[] arr2, ReadOnlySpan<T> arr3)
|
|
|
|
{
|
|
|
|
int len = arr1.Length + arr2.Length + arr3.Length;
|
|
|
|
var result = new T[len];
|
|
|
|
arr1.CopyTo(result, 0);
|
|
|
|
arr2.CopyTo(result, arr1.Length);
|
|
|
|
arr3.CopyTo(result.AsSpan(arr1.Length + arr2.Length));
|
|
|
|
return result;
|
2019-09-10 07:21:51 +00:00
|
|
|
}
|
2021-05-07 06:26:38 +00:00
|
|
|
}
|