Skip to content

Commit

Permalink
fix: thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
tk-yoshimura committed Nov 13, 2024
1 parent 5ad990d commit b24cf11
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using DoubleDoubleStatistic.InternalUtils;
using DoubleDoubleStatistic.RandomGeneration;
using DoubleDoubleStatistic.Utils;
using System.Collections.Concurrent;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Numerics;
Expand Down Expand Up @@ -229,7 +230,7 @@ public override string ToString() {
public override string Formula => "p(x; n) := sum((-1)^k * binom(n, k) * (x - k)^(n - 1), k, 0, floor(x)) / (n - 1)!";

private static class PolynomialCoef {
static readonly Dictionary<(int n, int k, int j), BigInteger> table = [];
static readonly ConcurrentDictionary<(int n, int k, int j), BigInteger> table = [];

private static BigInteger ACoef(int n, int k, int j) {
if (n <= 0 || k < 0 || j < 0 || j >= n || k >= n) {
Expand All @@ -240,19 +241,16 @@ private static BigInteger ACoef(int n, int k, int j) {
return j < n - 1 ? BigInteger.Zero : BigInteger.One;
}

if (table.TryGetValue((n, k, j), out BigInteger value)) {
return value;
}

BigInteger c = Binom.Value(n, k) * Binom.Value(n - 1, j) * BigInteger.Pow(k, n - j - 1);
if (!table.TryGetValue((n, k, j), out BigInteger value)) {
BigInteger c = Binom.Value(n, k) * Binom.Value(n - 1, j) * BigInteger.Pow(k, n - j - 1);

int s = n + k - j - 1;
int s = n + k - j - 1;

BigInteger new_value = ACoef(n, k - 1, j) + ((s & 1) == 0 ? c : -c);

table.Add((n, k, j), new_value);
value = ACoef(n, k - 1, j) + ((s & 1) == 0 ? c : -c);
table[(n, k, j)] = value;
}

return new_value;
return value;
}

public static Polynomial PDF(int n, int k) {
Expand Down
6 changes: 3 additions & 3 deletions DoubleDoubleStatistic/DoubleDoubleStatistic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="TYoshimura.Algebra" Version="2.3.0" />
<PackageReference Include="TYoshimura.DoubleDouble" Version="4.0.0" />
<PackageReference Include="TYoshimura.DoubleDouble.Complex" Version="1.6.0" />
<PackageReference Include="TYoshimura.Algebra" Version="2.3.2" />
<PackageReference Include="TYoshimura.DoubleDouble" Version="4.2.0" />
<PackageReference Include="TYoshimura.DoubleDouble.Complex" Version="1.6.3" />
</ItemGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Expand Down
16 changes: 7 additions & 9 deletions DoubleDoubleStatistic/InternalUtils/Binom.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.Numerics;
using System.Collections.Concurrent;
using System.Numerics;

namespace DoubleDoubleStatistic.InternalUtils {
internal static class Binom {
static readonly Dictionary<(int n, int k), BigInteger> table = [];
static readonly ConcurrentDictionary<(int n, int k), BigInteger> table = [];

public static BigInteger Value(int n, int k) {
if (n < 0 || k > n) {
Expand All @@ -13,15 +14,12 @@ public static BigInteger Value(int n, int k) {
return BigInteger.One;
}

if (table.TryGetValue((n, k), out BigInteger value)) {
return value;
if (!table.TryGetValue((n, k), out BigInteger value)) {
value = Value(n - 1, k - 1) + Value(n - 1, k);
table[(n, k)] = value;
}

BigInteger new_value = Value(n - 1, k - 1) + Value(n - 1, k);

table.Add((n, k), new_value);

return new_value;
return value;
}
}
}
2 changes: 1 addition & 1 deletion DoubleDoubleStatistic/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@

[assembly: Guid("8135883D-8ED2-4099-86C2-BE77FED9079A")]

[assembly: AssemblyVersion("1.7.0.*")]
[assembly: AssemblyVersion("1.8.0.*")]

[assembly: InternalsVisibleTo("DoubleDoubleStatisticTest")]
13 changes: 8 additions & 5 deletions DoubleDoubleStatisticTest/DoubleDoubleStatisticTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="MSTest.TestAdapter" Version="3.6.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.6.2" />
<PackageReference Include="Microsoft.Testing.Extensions.CodeCoverage" Version="17.12.6" />
<PackageReference Include="Microsoft.Testing.Extensions.TrxReport" Version="1.4.3" />
<PackageReference Include="MSTest" Version="3.6.3" />
<PackageReference Include="MSTest.TestAdapter" Version="3.6.3" />
<PackageReference Include="MSTest.TestFramework" Version="3.6.3" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="TYoshimura.Algebra" Version="2.3.0" />
<PackageReference Include="TYoshimura.DoubleDouble" Version="4.0.0" />
<PackageReference Include="TYoshimura.DoubleDouble.Complex" Version="1.6.0" />
<PackageReference Include="TYoshimura.Algebra" Version="2.3.2" />
<PackageReference Include="TYoshimura.DoubleDouble" Version="4.2.0" />
<PackageReference Include="TYoshimura.DoubleDouble.Complex" Version="1.6.3" />
</ItemGroup>

<ItemGroup Condition="'$(Configuration)'=='Debug'">
Expand Down
3 changes: 3 additions & 0 deletions DoubleDoubleStatisticTest/MSTestSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

[assembly: Parallelize(Scope = ExecutionScope.MethodLevel)]

0 comments on commit b24cf11

Please sign in to comment.