Skip to content

Commit

Permalink
Interfaceを追加
Browse files Browse the repository at this point in the history
  • Loading branch information
yatsuna827 committed Aug 3, 2023
1 parent e10e4a6 commit b9091c2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
20 changes: 20 additions & 0 deletions PokemonPRNG.Xoroshiro128p/BDSP.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

namespace PokemonPRNG.Xoroshiro128p.BDSP
{
public static class BDSPExtension
{
public static (ulong S0, ulong S1) Initialize(this uint seed)
=> (seed.Temper(0x9E3779B97F4A7C15), seed.Temper(0x3C6EF372FE94F82A));
private static ulong Temper(this uint seed, ulong state)
{
var value = seed + state;
value = 0xBF58476D1CE4E5B9 * (value ^ (value >> 30));
value = 0x94D049BB133111EB * (value ^ (value >> 27));
return value ^ (value >> 31);
}

// maxなのにmod取ってるの馬鹿です。
public static uint GetRand(ref this (ulong S0, ulong S1) state, uint max = 0xFFFFFFFF)
=> (uint)(Xoroshiro128p.GetRand(ref state) >> 32) % max;
}
}
2 changes: 1 addition & 1 deletion PokemonPRNG.Xoroshiro128p/PokemonPRNG.Xoroshiro128p.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>1.2.0</Version>
<Version>1.3.0</Version>
</PropertyGroup>

</Project>
37 changes: 27 additions & 10 deletions PokemonPRNG.Xoroshiro128p/Xoroshiro128p.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,6 @@ public static (ulong S0, ulong S1) FromU128String(this string hex)
return (s0, s1);
}

public static (ulong S0, ulong S1) Initialize(this ulong seed)
=> (seed.Temper(0x9E3779B97F4A7C15), seed.Temper(0x3C6EF372FE94F82A));
private static ulong Temper(this ulong seed, ulong state)
{
seed += state;
seed = 0xBF58476D1CE4E5B9 * (seed ^ (seed >> 30));
seed = 0x94D049BB133111EB * (seed ^ (seed >> 27));
return seed ^ (seed >> 31);
}
}

public static class Xoroshiro128pJumpExt
Expand Down Expand Up @@ -630,9 +621,35 @@ public static TResult Generate<TResult, TArg>(ref this (ulong S0, ulong S1) seed
public static IEnumerable<TResult> Enumerate<TResult>
(this IEnumerable<(ulong S0, ulong S1)> seedEnumerator, IGeneratable<TResult> igenerator)
=> seedEnumerator.Select(igenerator.Generate);

public static IEnumerable<TResult> Enumerate<TResult, TArg1>
(this IEnumerable<(ulong S0, ulong S1)> seedEnumerator, IGeneratable<TResult, TArg1> igenerator, TArg1 arg1)
=> seedEnumerator.Select(_ => igenerator.Generate(_, arg1));
}

public static class ApplyExtension
{
public static IGeneratable<TResult> Apply<TResult, TArg1>(this IGeneratable<TResult, TArg1> generatable, TArg1 arg1)
=> new AppliedGeneratable<TResult, TArg1>(generatable, arg1);

public static IGeneratableEffectful<TResult> Apply<TResult, TArg1>(this IGeneratableEffectful<TResult, TArg1> generatable, TArg1 arg1)
=> new AppliedRefGeneratable<TResult, TArg1>(generatable, arg1);
}

class AppliedGeneratable<TResult, TArg1> : IGeneratable<TResult>
{
private readonly TArg1 _arg;
private readonly IGeneratable<TResult, TArg1> _generatable;
public TResult Generate((ulong S0, ulong S1) seed) => _generatable.Generate(seed, _arg);
public AppliedGeneratable(IGeneratable<TResult, TArg1> generatable, TArg1 arg)
=> (_generatable, _arg) = (generatable, arg);
}
class AppliedRefGeneratable<TResult, TArg1> : IGeneratableEffectful<TResult>
{
private readonly TArg1 _arg;
private readonly IGeneratableEffectful<TResult, TArg1> _generatable;
public TResult Generate(ref (ulong S0, ulong S1) seed) => _generatable.Generate(ref seed, _arg);
public AppliedRefGeneratable(IGeneratableEffectful<TResult, TArg1> generatable, TArg1 arg)
=> (_generatable, _arg) = (generatable, arg);
}

}

0 comments on commit b9091c2

Please sign in to comment.