using System;
using System.Collections.Generic;
namespace PKHeX.Core;
///
/// Array reusable logic
///
public static class ArrayUtil
{
///
/// 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++;
}
}
}