using System;
using System.Buffers;
namespace PKHeX.Core;
public sealed class LegalMoveInfo
{
// Use a bool array instead of a HashSet; we have a limited range of moves.
// This implementation is faster (no hashcode or bucket search) with lower memory overhead (1 byte per move ID).
private readonly bool[] AllowedMoves = new bool[(int)Move.MAX_COUNT + 1];
///
/// Checks if the requested is legally able to be learned.
///
/// Move to check if can be learned
/// True if can learn the move
public bool CanLearn(int move) => AllowedMoves[move];
///
/// Reloads the legality sources to permit the provided legal info.
///
/// Details of analysis, moves to allow
public bool ReloadMoves(LegalityAnalysis la)
{
var rent = ArrayPool.Shared.Rent(AllowedMoves.Length);
var span = rent.AsSpan(0, AllowedMoves.Length);
LearnPossible.Get(la.Entity, la.EncounterOriginal, la.Info.EvoChainsAllGens, span);
// check prior move-pool to not needlessly refresh the data set
bool diff = !span.SequenceEqual(AllowedMoves);
if (diff) // keep
span.CopyTo(AllowedMoves);
span.Clear();
ArrayPool.Shared.Return(rent);
return diff;
}
}