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 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
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Checks the range (exclusive max) if the <see cref="value"/> is inside.
|
|
|
|
/// </summary>
|
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)
|
2023-12-04 04:13:20 +00:00
|
|
|
yield return bin.AsSpan(i, size).ToArray();
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2019-09-10 07:21:51 +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
|
|
|
}
|
2021-05-07 06:26:38 +00:00
|
|
|
}
|