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="skip">Criteria for skipping a slot</param>
/// <param name="start">Starting point to copy to</param> /// <param name="start">Starting point to copy to</param>
/// <returns>Count of <see cref="PKM"/> copied.</returns> /// <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; int ctr = start;
if (ctr >= dest.Count)
return 0;
int skipped = 0; int skipped = 0;
foreach (var z in list) foreach (var z in list)
{ {
// seek forward to next open slot // seek forward to next open slot
while (true) int next = FindNextSlot(dest, skip, ctr);
{ if (next == -1)
var exist = dest[ctr];
if (exist == null || !skip(exist.Box, exist.Slot))
break;
skipped++;
ctr++;
}
if (dest.Count <= ctr)
break; break;
skipped += next - ctr;
ctr = next;
dest[ctr++] = z; dest[ctr++] = z;
} }
return ctr - start - skipped; 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> /// <summary>
/// Copies an <see cref="Enumerable"/> list to the destination list, with an option to copy to a starting point. /// Copies an <see cref="Enumerable"/> list to the destination list, with an option to copy to a starting point.
/// </summary> /// </summary>

View file

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