Make sort skip delegate more general

This commit is contained in:
Kurt 2019-04-29 17:20:16 -07:00
parent 9838ea9a2b
commit 7c380a29c3
2 changed files with 21 additions and 17 deletions

View file

@ -844,31 +844,36 @@ namespace PKHeX.Core
/// <param name="skip">Criteria for skipping a slot</param>
/// <param name="start">Starting point to copy to</param>
/// <returns>Count of <see cref="PKM"/> copied.</returns>
public static int CopyTo(this IEnumerable<PKM> list, IList<PKM> dest, Func<int, int, bool> skip, int start = 0)
public static int CopyTo(this IEnumerable<PKM> list, IList<PKM> dest, Func<PKM, bool> skip, int start = 0)
{
int ctr = start;
if (ctr >= dest.Count)
return 0;
int skipped = 0;
foreach (var z in list)
{
// seek forward to next open slot
while (true)
{
var exist = dest[ctr];
if (exist == null || !skip(exist.Box, exist.Slot))
break;
skipped++;
ctr++;
}
if (dest.Count <= ctr)
int next = FindNextSlot(dest, skip, ctr);
if (next == -1)
break;
skipped += next - ctr;
ctr = next;
dest[ctr++] = z;
}
return ctr - start - skipped;
}
private static int FindNextSlot(IList<PKM> dest, Func<PKM, bool> skip, int ctr)
{
while (true)
{
if (ctr >= dest.Count)
return -1;
var exist = dest[ctr];
if (exist == null || !skip(exist))
return ctr;
ctr++;
}
}
/// <summary>
/// Copies an <see cref="Enumerable"/> list to the destination list, with an option to copy to a starting point.
/// </summary>

View file

@ -461,6 +461,7 @@ namespace PKHeX.Core
public bool IsSlotLocked(int index) => GetSlotFlags(index).HasFlagFast(StorageSlotFlag.Locked);
public bool IsSlotOverwriteProtected(int box, int slot) => GetSlotFlags(box, slot).IsOverwriteProtected();
public bool IsSlotOverwriteProtected(int index) => GetSlotFlags(index).IsOverwriteProtected();
public bool IsSlotOverwriteProtected(PKM pkm) => GetSlotFlags(pkm.Box, pkm.Slot).IsOverwriteProtected();
public bool MoveBox(int box, int insertBeforeBox)
{
@ -622,7 +623,6 @@ namespace PKHeX.Core
}
SetData(pkm.EncryptedPartyData, offset);
Edited = true;
}
protected void UpdatePKM(PKM pkm, PKMImportSetting trade, PKMImportSetting dex)
@ -665,7 +665,6 @@ namespace PKHeX.Core
UpdatePKM(pkm, trade, dex);
SetPartyValues(pkm, isParty: false);
SetData(pkm.EncryptedBoxData, offset);
Edited = true;
}
public void DeletePartySlot(int slot)
@ -717,8 +716,8 @@ namespace PKHeX.Core
if (BoxEnd >= BoxStart)
Section = Section.Take(BoxSlotCount * (BoxEnd - BoxStart + 1));
Func<int, int, bool> skip = IsSlotSwapProtected;
Section = Section.Where(z => !skip(z.Box, z.Slot));
Func<PKM, bool> skip = IsSlotOverwriteProtected;
Section = Section.Where(z => !skip(z));
var Sorted = (sortMethod ?? PKMSorting.OrderBySpecies)(Section);
if (reverse)
Sorted = Sorted.ReverseSort();