From 8d781fd16753f6f1f99dc18cbb699aed96096e5e Mon Sep 17 00:00:00 2001 From: Kurt Date: Tue, 28 May 2019 22:55:41 -0700 Subject: [PATCH] Simplify sorting to remove linq usage create custom comparer to do the work reorder some parameters simplify gb hm set (All and FindIndex is duplicate work) only usages of linq remaining are for the bulk action Where filtering (ok) --- .../Substructures/Inventory/InventoryItem.cs | 2 +- .../Substructures/Inventory/InventoryPouch.cs | 74 ++++++++----------- .../Inventory/InventoryPouchGB.cs | 7 +- .../Subforms/Save Editors/SAV_Inventory.cs | 2 +- 4 files changed, 35 insertions(+), 50 deletions(-) diff --git a/PKHeX.Core/Saves/Substructures/Inventory/InventoryItem.cs b/PKHeX.Core/Saves/Substructures/Inventory/InventoryItem.cs index fd86f0479..bea6783ce 100644 --- a/PKHeX.Core/Saves/Substructures/Inventory/InventoryItem.cs +++ b/PKHeX.Core/Saves/Substructures/Inventory/InventoryItem.cs @@ -10,7 +10,7 @@ public class InventoryItem public InventoryItem Clone() => (InventoryItem) MemberwiseClone(); // Check Pouch Compatibility - public bool Valid(IList LegalItems, bool HaX, int MaxItemID) + public bool Valid(IList LegalItems, int MaxItemID, bool HaX = false) { if (Index == 0) return true; diff --git a/PKHeX.Core/Saves/Substructures/Inventory/InventoryPouch.cs b/PKHeX.Core/Saves/Substructures/Inventory/InventoryPouch.cs index 75e50038a..7add15937 100644 --- a/PKHeX.Core/Saves/Substructures/Inventory/InventoryPouch.cs +++ b/PKHeX.Core/Saves/Substructures/Inventory/InventoryPouch.cs @@ -29,57 +29,43 @@ protected InventoryPouch(InventoryType type, ushort[] legal, int maxcount, int o public abstract void GetPouch(byte[] Data); public abstract void SetPouch(byte[] Data); - public void SortByCount(bool reverse = false) + public void SortByCount(bool reverse = false) => Array.Sort(Items, (x, y) => Compare(x.Count, y.Count, reverse)); + public void SortByIndex(bool reverse = false) => Array.Sort(Items, (x, y) => Compare(x.Index, y.Index, reverse)); + public void SortByName(string[] names, bool reverse = false) => Array.Sort(Items, (x, y) => Compare(x.Index, y.Index, names, reverse)); + public void SortByEmpty() => Array.Sort(Items, (x, y) => (x.Count == 0).CompareTo(y.Count == 0)); + + private static int Compare(int i1, int i2, IReadOnlyList n, bool rev) where T : IComparable { - var list = Items.Where(item => item.Index != 0).OrderBy(item => item.Count == 0); - list = reverse - ? list.ThenByDescending(item => item.Count) - : list.ThenBy(item => item.Count); - Items = list.Concat(Items.Where(item => item.Index == 0)).ToArray(); + if (i1 == 0 || i1 >= n.Count) + return 1; + if (i2 == 0 || i2 >= n.Count) + return -1; + return rev + ? n[i2].CompareTo(n[i1]) + : n[i1].CompareTo(n[i2]); } - public void SortByIndex(bool reverse = false) + private static int Compare(int i1, int i2, bool rev) { - var list = Items.Where(item => item.Index != 0).OrderBy(item => item.Count == 0); - list = reverse - ? list.ThenByDescending(item => item.Index) - : list.ThenBy(item => item.Index); - Items = list.Concat(Items.Where(item => item.Index == 0)).ToArray(); + if (i1 == 0) + return 1; + if (i2 == 0) + return -1; + return rev + ? i2.CompareTo(i1) + : i1.CompareTo(i2); } - public void SortByName(string[] names, bool reverse = false) + public void Sanitize(int MaxItemID, bool HaX = false) { - var list = Items.Where(item => item.Index != 0 && item.Index < names.Length).OrderBy(item => item.Count == 0); - list = reverse - ? list.ThenByDescending(item => names[item.Index]) - : list.ThenBy(item => names[item.Index]); - Items = list.Concat(Items.Where(item => item.Index == 0 || item.Index >= names.Length)).ToArray(); - } - - public void Sanitize(bool HaX, int MaxItemID) - { - var x = GetValidItems(HaX, MaxItemID); - var count = PouchDataSize - x.Count; - Items = x.Concat(Enumerable.Range(0, count).Select(_ => new InventoryItem())).ToArray(); - } - - public IList GetValidItems(bool HaX, int MaxItemID) - { - return Items - .Where(item => item.Valid(LegalItems, HaX, MaxItemID)) - .ToList(); - } - - public IList GetInvalidItems(bool HaX, int MaxItemID) - { - return Items - .Where(item => !item.Valid(LegalItems, HaX, MaxItemID)) - .ToList(); - } - - public void MoveEmptySlots() - { - Items = Items.OrderBy(z => z.Count == 0).ToArray(); + int ctr = 0; + for (int i = 0; i < Items.Length; i++) + { + if (Items[i].Valid(LegalItems, MaxItemID, HaX)) + Items[ctr++] = Items[i]; + } + for (int i = ctr; i < Items.Length; i++) + Items[i] = new InventoryItem(); } public void RemoveAll() diff --git a/PKHeX.Core/Saves/Substructures/Inventory/InventoryPouchGB.cs b/PKHeX.Core/Saves/Substructures/Inventory/InventoryPouchGB.cs index ab19a84c8..de962d378 100644 --- a/PKHeX.Core/Saves/Substructures/Inventory/InventoryPouchGB.cs +++ b/PKHeX.Core/Saves/Substructures/Inventory/InventoryPouchGB.cs @@ -1,5 +1,4 @@ using System; -using System.Linq; namespace PKHeX.Core { @@ -85,10 +84,10 @@ public override void SetPouch(byte[] Data) case InventoryType.TMHMs: foreach (InventoryItem t in Items) { - if (LegalItems.All(z => z != t.Index)) + int index = Array.FindIndex(LegalItems, it => t.Index == it); + if (index < 0) // enforce correct pouch continue; - int index = Offset + Array.FindIndex(LegalItems, it => t.Index == it); - Data[index] = (byte)t.Count; + Data[Offset + index] = (byte)t.Count; } break; case InventoryType.KeyItems: diff --git a/PKHeX.WinForms/Subforms/Save Editors/SAV_Inventory.cs b/PKHeX.WinForms/Subforms/Save Editors/SAV_Inventory.cs index d29bdf52c..994e1af25 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/SAV_Inventory.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/SAV_Inventory.cs @@ -184,7 +184,7 @@ private void LoadAllBags() if (!Main.HaX && incorrectPouch.Length > 0) WinFormsUtil.Alert(string.Format(MsgItemPouchRemoved, pouch.Type), string.Join(", ", incorrectPouch.Select(item => itemlist[item.Index])), MsgItemPouchWarning); - pouch.Sanitize(Main.HaX, itemlist.Length - 1); + pouch.Sanitize(itemlist.Length - 1, Main.HaX); GetBag(dgv, pouch); } }