2018-02-05 00:30:50 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
2022-09-04 19:03:37 +00:00
|
|
|
/// Frame List used to cache <see cref="XDRNG"/> results, lazily reversing backwards and keeping the previous results.
|
2022-06-18 18:04:24 +00:00
|
|
|
/// </summary>
|
|
|
|
public sealed class FrameCache
|
2018-02-05 00:30:50 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
private const int DefaultSize = 32;
|
|
|
|
private readonly List<uint> Seeds = new(DefaultSize);
|
|
|
|
private readonly List<uint> Values = new(DefaultSize);
|
2022-09-04 19:03:37 +00:00
|
|
|
private uint Last;
|
2022-06-18 18:04:24 +00:00
|
|
|
|
2018-02-05 00:30:50 +00:00
|
|
|
/// <summary>
|
2022-06-18 18:04:24 +00:00
|
|
|
/// Creates a new instance of a <see cref="FrameCache"/>.
|
2018-02-05 00:30:50 +00:00
|
|
|
/// </summary>
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <param name="origin">Seed at frame 0.</param>
|
2022-09-04 19:03:37 +00:00
|
|
|
public FrameCache(uint origin) => Add(origin);
|
2018-02-05 00:30:50 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
private void Add(uint seed)
|
|
|
|
{
|
2022-09-04 19:03:37 +00:00
|
|
|
Seeds.Add(Last = seed);
|
2022-06-18 18:04:24 +00:00
|
|
|
Values.Add(seed >> 16);
|
|
|
|
}
|
2018-02-05 00:30:50 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the 16 bit value from <see cref="Values"/> at a given <see cref="index"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="index">Index to grab the value from</param>
|
|
|
|
public uint this[int index]
|
|
|
|
{
|
|
|
|
get
|
2018-02-05 00:30:50 +00:00
|
|
|
{
|
|
|
|
while (index >= Seeds.Count)
|
2022-09-04 19:03:37 +00:00
|
|
|
Add(XDRNG.Prev(Last));
|
2022-06-18 18:04:24 +00:00
|
|
|
return Values[index];
|
2018-02-05 00:30:50 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the Seed at a specified frame index.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="index">Frame number</param>
|
|
|
|
/// <returns>Seed at index</returns>
|
|
|
|
public uint GetSeed(int index)
|
|
|
|
{
|
|
|
|
while (index >= Seeds.Count)
|
2022-09-04 19:03:37 +00:00
|
|
|
Add(XDRNG.Prev(Last));
|
2022-06-18 18:04:24 +00:00
|
|
|
return Seeds[index];
|
|
|
|
}
|
2018-02-05 00:30:50 +00:00
|
|
|
}
|