From e18c2d8fa439ddb10b64e55229f6c22f8259d9df Mon Sep 17 00:00:00 2001 From: Kurt Date: Thu, 5 Aug 2021 20:47:32 -0700 Subject: [PATCH] Replace linq with similar but faster logic --- .../Legality/Encounters/Data/EncounterUtil.cs | 55 +++++++++++++++---- 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/PKHeX.Core/Legality/Encounters/Data/EncounterUtil.cs b/PKHeX.Core/Legality/Encounters/Data/EncounterUtil.cs index c9daf0d7a..603403656 100644 --- a/PKHeX.Core/Legality/Encounters/Data/EncounterUtil.cs +++ b/PKHeX.Core/Legality/Encounters/Data/EncounterUtil.cs @@ -1,5 +1,5 @@ -using System.Collections.Generic; -using System.Linq; +using System; +using System.Collections.Generic; namespace PKHeX.Core { @@ -14,31 +14,62 @@ internal static class EncounterUtil /// Table of valid encounters that appear for the game pairing /// Game to filter for /// Array of encounter objects that can be encountered in the input game - internal static T[] GetEncounters(IEnumerable source, GameVersion game) where T : IVersion + internal static T[] GetEncounters(T[] source, GameVersion game) where T : EncounterStatic { - return source.Where(s => s.Version.Contains(game)).ToArray(); + return Array.FindAll(source, s => s.Version.Contains(game)); } + /// + /// Loads the language string lists into the objects. + /// + /// Encounter template type + /// Trade templates + /// Localization strings, grouped by language. + /// + /// The first half of strings in the language resource array are + /// The second half of strings in the language resource strings are + /// internal static void MarkEncounterTradeStrings(T[] table, string[][] strings) where T : EncounterTrade { - int half = strings[1].Length / 2; - for (int i = 0; i < half; i++) + uint languageCount = (uint)strings[1].Length / 2; + for (uint i = 0; i < languageCount; i++) { var t = table[i]; - t.Nicknames = getNames(i, strings); - t.TrainerNames = getNames(i + half, strings); + t.Nicknames = GetNamesForLanguage(strings, i); + t.TrainerNames = GetNamesForLanguage(strings, languageCount + i); } - static string[] getNames(int i, IEnumerable names) => names.Select(z => z.Length > i ? z[i] : string.Empty).ToArray(); } + /// + /// Loads the language string lists into the objects. + /// + /// Encounter template type + /// Trade templates + /// Localization strings, grouped by language. internal static void MarkEncounterTradeNicknames(T[] table, string[][] strings) where T : EncounterTrade { - for (int i = 0; i < table.Length; i++) + for (uint i = 0; i < table.Length; i++) { var t = table[i]; - t.Nicknames = getNames(i, strings); + t.Nicknames = GetNamesForLanguage(strings, i); } - static string[] getNames(int i, IEnumerable names) => names.Select(z => z.Length > i ? z[i] : string.Empty).ToArray(); + } + + /// + /// Grabs the localized names for individual templates for all languages from the specified of the list. + /// + /// Arrays of strings grouped by language + /// Index to grab from the language arrays + /// Row of localized strings for the template. + private static string[] GetNamesForLanguage(IReadOnlyList names, uint index) + { + var result = new string[names.Count]; + for (int i = 0; i < result.Length; i++) + { + var arr = names[i]; + result[i] = index < arr.Length ? arr[index] : string.Empty; + } + return result; } } }