From b1cd68e3f3afe21c06deb9eeb06bc028295fd9ec Mon Sep 17 00:00:00 2001 From: Kurt Date: Thu, 7 Jun 2018 19:05:28 -0700 Subject: [PATCH] Convert GetLevelLearnMove to use lazy dictionary faster move lookup --- PKHeX.Core/Legality/Learnset/Learnset.cs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/PKHeX.Core/Legality/Learnset/Learnset.cs b/PKHeX.Core/Legality/Learnset/Learnset.cs index 4f021f5ba..88b29fe43 100644 --- a/PKHeX.Core/Legality/Learnset/Learnset.cs +++ b/PKHeX.Core/Legality/Learnset/Learnset.cs @@ -66,13 +66,23 @@ public int GetMinMoveLevel(int level) return Math.Max(end - 4, 1); } + + private Dictionary Learn; + private Dictionary GetDictionary() + { + var dict = new Dictionary(); + for (int i = 0; i < Moves.Length; i++) + if (!dict.ContainsKey(Moves[i])) + dict.Add(Moves[i], Levels[i]); + return dict; + } + /// Returns the level that a Pokémon can learn the specified move. /// Move ID /// Level the move is learned at. If the result is below 0, it cannot be learned by levelup. public int GetLevelLearnMove(int move) { - int index = Array.IndexOf(Moves, move); - return index < 0 ? index : Levels[index]; + return (Learn ?? (Learn = GetDictionary())).TryGetValue(move, out var level) ? level : -1; } } }