PKHeX/PKHeX.Core/Legality/Structures/IndividualValueSet.cs
Kurt 6441bdadd8
Add specialized struct for Moveset and IV specs (#3572)
`Moveset` struct stores 4 moves, and exposes methods to interact with a moveset.
`IndividualValueSet` stores a 6 IV template (signed).

Performance impact:
* Less allocating on the heap: Moves - (8 bytes member ptr, 20 bytes heap->8 bytes member)
* Less allocating on the heap: IVs - (8 bytes member ptr, 28 bytes heap->8 bytes member)
* No heap pointers, no need to jump to grab data.
* Easy to inline logic for checking if moves are present (no linq usage with temporary collections).

End result is faster ctor times, less memory used, faster program.
2022-08-21 17:34:32 -07:00

51 lines
1.5 KiB
C#

using System;
// ReSharper disable RedundantExplicitPositionalPropertyDeclaration
namespace PKHeX.Core;
/// <summary>
/// Stores an IV template.
/// </summary>
/// <param name="HP "><see cref="PKM.IV_HP "/></param>
/// <param name="ATK"><see cref="PKM.IV_ATK"/></param>
/// <param name="DEF"><see cref="PKM.IV_DEF"/></param>
/// <param name="SPE"><see cref="PKM.IV_SPE"/></param>
/// <param name="SPA"><see cref="PKM.IV_SPA"/></param>
/// <param name="SPD"><see cref="PKM.IV_SPD"/></param>
/// <param name="Type">Differentiate between different IV templates, or lack thereof (0).</param>
public readonly record struct IndividualValueSet(sbyte HP, sbyte ATK, sbyte DEF, sbyte SPE, sbyte SPA, sbyte SPD, byte Type = 1)
{
// 8 BYTES MAX STRUCTURE
public byte Type { get; init; } = Type;
public sbyte HP { get; init; } = HP;
public sbyte ATK { get; init; } = ATK;
public sbyte DEF { get; init; } = DEF;
public sbyte SPE { get; init; } = SPE;
public sbyte SPA { get; init; } = SPA;
public sbyte SPD { get; init; } = SPD;
// Default struct will be zero type.
public bool IsSpecified => Type != 0;
public sbyte this[int index] => index switch
{
0 => HP,
1 => ATK,
2 => DEF,
3 => SPE,
4 => SPA,
5 => SPD,
_ => throw new ArgumentOutOfRangeException(nameof(index), index, null),
};
public void CopyToSpeedLast(Span<int> span)
{
span[5] = SPE;
span[4] = SPD;
span[3] = SPA;
span[2] = DEF;
span[1] = ATK;
span[0] = HP;
}
}