using System;
using System.Collections.Generic;
namespace PKHeX.Core;
///
/// Array reusable logic
///
public static class ArrayUtil
{
public static T Find(this Span data, Func value) where T : unmanaged
{
foreach (var x in data)
{
if (value(x))
return x;
}
return default;
}
///
/// Checks the range (exclusive max) if the is inside.
///
public static bool WithinRange(int value, int min, int max) => min <= value && value < max;
public static IEnumerable EnumerateSplit(T[] bin, int size, int start = 0)
{
for (int i = start; i < bin.Length; i += size)
yield return bin.AsSpan(i, size).ToArray();
}
///
/// Copies a list to the destination list, with an option to copy to a starting point.
///
/// Source list to copy from
/// Destination list/array
/// Criteria for skipping a slot
/// Starting point to copy to
/// Count of copied.
public static int CopyTo(this IEnumerable list, IList dest, Func 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;
}
public static int FindNextValidIndex(IList dest, Func skip, int ctr)
{
while (true)
{
if ((uint)ctr >= dest.Count)
return -1;
var exist = dest[ctr];
if (exist == null || !skip(ctr))
return ctr;
ctr++;
}
}
}