Skip to content

Commit b9c534e

Browse files
committed
Discrete distributions optimization
1 parent 930455e commit b9c534e

27 files changed

+472
-97
lines changed

Runtime/DiscreteDistributions/PoissonDistribution/GeneratorProviders/PoissonGeneratorDependentProvider.cs

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright (c) 2020 Vladimir Popov zor1994@gmail.com https://github.com/ZorPastaman/Random-Generators
22

3+
using System.Runtime.CompilerServices;
34
using JetBrains.Annotations;
45
using UnityEngine;
56
using Zor.RandomGenerators.ContinuousDistributions;
@@ -18,8 +19,8 @@ public sealed class PoissonGeneratorDependentProvider : DiscreteGeneratorProvide
1819
{
1920
#pragma warning disable CS0649
2021
[SerializeField] private ContinuousGeneratorProviderReference m_DependentGeneratorProvider;
21-
[SerializeField] private float m_Lambda;
22-
[SerializeField] private int m_StartPoint;
22+
[SerializeField] private float m_Lambda = PoissonDistribution.DefaultLambda;
23+
[SerializeField] private int m_StartPoint = PoissonDistribution.DefaultStartPoint;
2324
#pragma warning restore CS0649
2425

2526
private PoissonGeneratorDependent<IContinuousGenerator> m_sharedGenerator;
@@ -41,7 +42,6 @@ public override IDiscreteGenerator<int> generator
4142
/// </summary>
4243
public override IDiscreteGenerator<int> sharedGenerator
4344
{
44-
[Pure]
4545
get
4646
{
4747
if (m_sharedGenerator == null)
@@ -67,9 +67,9 @@ public PoissonGeneratorDependent<IContinuousGenerator> poissonGenerator
6767
/// <summary>
6868
/// Returns a shared <see cref="PoissonGeneratorDependent{T}"/>.
6969
/// </summary>
70+
[NotNull]
7071
public PoissonGeneratorDependent<IContinuousGenerator> sharedPoissonGenerator
7172
{
72-
[Pure]
7373
get
7474
{
7575
if (m_sharedGenerator == null)
@@ -80,5 +80,58 @@ public PoissonGeneratorDependent<IContinuousGenerator> sharedPoissonGenerator
8080
return m_sharedGenerator;
8181
}
8282
}
83+
84+
public ContinuousGeneratorProviderReference dependentGeneratorProvider
85+
{
86+
[MethodImpl(MethodImplOptions.AggressiveInlining), Pure]
87+
get => m_DependentGeneratorProvider;
88+
set
89+
{
90+
if (m_DependentGeneratorProvider == value)
91+
{
92+
return;
93+
}
94+
95+
m_DependentGeneratorProvider = value;
96+
m_sharedGenerator = null;
97+
}
98+
}
99+
100+
public float lambda
101+
{
102+
[MethodImpl(MethodImplOptions.AggressiveInlining), Pure]
103+
get => m_Lambda;
104+
set
105+
{
106+
if (m_Lambda == value)
107+
{
108+
return;
109+
}
110+
111+
m_Lambda = value;
112+
m_sharedGenerator = null;
113+
}
114+
}
115+
116+
public int startPoint
117+
{
118+
[MethodImpl(MethodImplOptions.AggressiveInlining), Pure]
119+
get => m_StartPoint;
120+
set
121+
{
122+
if (m_StartPoint == value)
123+
{
124+
return;
125+
}
126+
127+
m_StartPoint = value;
128+
m_sharedGenerator = null;
129+
}
130+
}
131+
132+
private void OnValidate()
133+
{
134+
m_sharedGenerator = null;
135+
}
83136
}
84137
}

Runtime/DiscreteDistributions/PoissonDistribution/GeneratorProviders/PoissonGeneratorDependentSimpleProvider.cs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright (c) 2020 Vladimir Popov zor1994@gmail.com https://github.com/ZorPastaman/Random-Generators
22

3+
using System.Runtime.CompilerServices;
34
using JetBrains.Annotations;
45
using UnityEngine;
56
using Zor.RandomGenerators.ContinuousDistributions;
@@ -18,7 +19,7 @@ public sealed class PoissonGeneratorDependentSimpleProvider : DiscreteGeneratorP
1819
{
1920
#pragma warning disable CS0649
2021
[SerializeField] private ContinuousGeneratorProviderReference m_DependentGeneratorProvider;
21-
[SerializeField] private float m_Lambda;
22+
[SerializeField] private float m_Lambda = PoissonDistribution.DefaultLambda;
2223
#pragma warning restore CS0649
2324

2425
private PoissonGeneratorDependentSimple<IContinuousGenerator> m_sharedGenerator;
@@ -40,7 +41,6 @@ public override IDiscreteGenerator<int> generator
4041
/// </summary>
4142
public override IDiscreteGenerator<int> sharedGenerator
4243
{
43-
[Pure]
4444
get
4545
{
4646
if (m_sharedGenerator == null)
@@ -66,9 +66,9 @@ public PoissonGeneratorDependentSimple<IContinuousGenerator> poissonGenerator
6666
/// <summary>
6767
/// Returns a shared <see cref="PoissonGeneratorDependentSimple{T}"/>.
6868
/// </summary>
69+
[NotNull]
6970
public PoissonGeneratorDependentSimple<IContinuousGenerator> sharedPoissonGenerator
7071
{
71-
[Pure]
7272
get
7373
{
7474
if (m_sharedGenerator == null)
@@ -79,5 +79,42 @@ public PoissonGeneratorDependentSimple<IContinuousGenerator> sharedPoissonGenera
7979
return m_sharedGenerator;
8080
}
8181
}
82+
83+
public ContinuousGeneratorProviderReference dependentGeneratorProvider
84+
{
85+
[MethodImpl(MethodImplOptions.AggressiveInlining), Pure]
86+
get => m_DependentGeneratorProvider;
87+
set
88+
{
89+
if (m_DependentGeneratorProvider == value)
90+
{
91+
return;
92+
}
93+
94+
m_DependentGeneratorProvider = value;
95+
m_sharedGenerator = null;
96+
}
97+
}
98+
99+
public float lambda
100+
{
101+
[MethodImpl(MethodImplOptions.AggressiveInlining), Pure]
102+
get => m_Lambda;
103+
set
104+
{
105+
if (m_Lambda == value)
106+
{
107+
return;
108+
}
109+
110+
m_Lambda = value;
111+
m_sharedGenerator = null;
112+
}
113+
}
114+
115+
private void OnValidate()
116+
{
117+
m_sharedGenerator = null;
118+
}
82119
}
83120
}

Runtime/DiscreteDistributions/PoissonDistribution/GeneratorProviders/PoissonGeneratorProvider.cs

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright (c) 2020 Vladimir Popov zor1994@gmail.com https://github.com/ZorPastaman/Random-Generators
22

3+
using System.Runtime.CompilerServices;
34
using JetBrains.Annotations;
45
using UnityEngine;
56

@@ -16,25 +17,35 @@ namespace Zor.RandomGenerators.DiscreteDistributions
1617
public sealed class PoissonGeneratorProvider : DiscreteGeneratorProvider<int>
1718
{
1819
#pragma warning disable CS0649
19-
[SerializeField] private PoissonGenerator m_PoissonGenerator;
20+
[SerializeField] private float m_Lambda = PoissonDistribution.DefaultLambda;
21+
[SerializeField] private int m_StartPoint = PoissonDistribution.DefaultStartPoint;
2022
#pragma warning restore CS0649
2123

24+
private PoissonGenerator m_sharedGenerator;
25+
2226
/// <summary>
2327
/// Creates a new <see cref="PoissonGenerator"/> and returns it as <see cref="IDiscreteGenerator{Int32}"/>.
2428
/// </summary>
2529
public override IDiscreteGenerator<int> generator
2630
{
2731
[Pure]
28-
get => new PoissonGenerator(m_PoissonGenerator);
32+
get => new PoissonGenerator(m_Lambda, m_StartPoint);
2933
}
3034

3135
/// <summary>
3236
/// Returns a shared <see cref="PoissonGenerator"/> as <see cref="IDiscreteGenerator{Int32}"/>.
3337
/// </summary>
3438
public override IDiscreteGenerator<int> sharedGenerator
3539
{
36-
[Pure]
37-
get => m_PoissonGenerator;
40+
get
41+
{
42+
if (m_sharedGenerator == null)
43+
{
44+
m_sharedGenerator = poissonGenerator;
45+
}
46+
47+
return m_sharedGenerator;
48+
}
3849
}
3950

4051
/// <summary>
@@ -44,7 +55,7 @@ public override IDiscreteGenerator<int> sharedGenerator
4455
public PoissonGenerator poissonGenerator
4556
{
4657
[Pure]
47-
get => new PoissonGenerator(m_PoissonGenerator);
58+
get => new PoissonGenerator(m_Lambda, m_StartPoint);
4859
}
4960

5061
/// <summary>
@@ -53,8 +64,52 @@ public PoissonGenerator poissonGenerator
5364
[NotNull]
5465
public PoissonGenerator sharedPoissonGenerator
5566
{
56-
[Pure]
57-
get => m_PoissonGenerator;
67+
get
68+
{
69+
if (m_sharedGenerator == null)
70+
{
71+
m_sharedGenerator = poissonGenerator;
72+
}
73+
74+
return m_sharedGenerator;
75+
}
76+
}
77+
78+
public float lambda
79+
{
80+
[MethodImpl(MethodImplOptions.AggressiveInlining), Pure]
81+
get => m_Lambda;
82+
set
83+
{
84+
if (m_Lambda == value)
85+
{
86+
return;
87+
}
88+
89+
m_Lambda = value;
90+
m_sharedGenerator = null;
91+
}
92+
}
93+
94+
public int startPoint
95+
{
96+
[MethodImpl(MethodImplOptions.AggressiveInlining), Pure]
97+
get => m_StartPoint;
98+
set
99+
{
100+
if (m_StartPoint == value)
101+
{
102+
return;
103+
}
104+
105+
m_StartPoint = value;
106+
m_sharedGenerator = null;
107+
}
108+
}
109+
110+
private void OnValidate()
111+
{
112+
m_sharedGenerator = null;
58113
}
59114
}
60115
}

Runtime/DiscreteDistributions/PoissonDistribution/GeneratorProviders/PoissonGeneratorSimpleProvider.cs

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright (c) 2020 Vladimir Popov zor1994@gmail.com https://github.com/ZorPastaman/Random-Generators
22

3+
using System.Runtime.CompilerServices;
34
using JetBrains.Annotations;
45
using UnityEngine;
56

@@ -16,25 +17,34 @@ namespace Zor.RandomGenerators.DiscreteDistributions
1617
public sealed class PoissonGeneratorSimpleProvider : DiscreteGeneratorProvider<int>
1718
{
1819
#pragma warning disable CS0649
19-
[SerializeField] private PoissonGeneratorSimple m_PoissonGenerator;
20+
[SerializeField] private float m_Lambda = PoissonDistribution.DefaultLambda;
2021
#pragma warning restore CS0649
2122

23+
private PoissonGeneratorSimple m_sharedGenerator;
24+
2225
/// <summary>
2326
/// Creates a new <see cref="PoissonGeneratorSimple"/> and returns it as <see cref="IDiscreteGenerator{Int32}"/>.
2427
/// </summary>
2528
public override IDiscreteGenerator<int> generator
2629
{
2730
[Pure]
28-
get => new PoissonGeneratorSimple(m_PoissonGenerator);
31+
get => new PoissonGeneratorSimple(m_Lambda);
2932
}
3033

3134
/// <summary>
3235
/// Returns a shared <see cref="PoissonGeneratorSimple"/> as <see cref="IDiscreteGenerator{Int32}"/>.
3336
/// </summary>
3437
public override IDiscreteGenerator<int> sharedGenerator
3538
{
36-
[Pure]
37-
get => m_PoissonGenerator;
39+
get
40+
{
41+
if (m_sharedGenerator == null)
42+
{
43+
m_sharedGenerator = poissonGenerator;
44+
}
45+
46+
return m_sharedGenerator;
47+
}
3848
}
3949

4050
/// <summary>
@@ -44,7 +54,7 @@ public override IDiscreteGenerator<int> sharedGenerator
4454
public PoissonGeneratorSimple poissonGenerator
4555
{
4656
[Pure]
47-
get => new PoissonGeneratorSimple(m_PoissonGenerator);
57+
get => new PoissonGeneratorSimple(m_Lambda);
4858
}
4959

5060
/// <summary>
@@ -53,8 +63,36 @@ public PoissonGeneratorSimple poissonGenerator
5363
[NotNull]
5464
public PoissonGeneratorSimple sharedPoissonGenerator
5565
{
56-
[Pure]
57-
get => m_PoissonGenerator;
66+
get
67+
{
68+
if (m_sharedGenerator == null)
69+
{
70+
m_sharedGenerator = poissonGenerator;
71+
}
72+
73+
return m_sharedGenerator;
74+
}
75+
}
76+
77+
public float lambda
78+
{
79+
[MethodImpl(MethodImplOptions.AggressiveInlining), Pure]
80+
get => m_Lambda;
81+
set
82+
{
83+
if (m_Lambda == value)
84+
{
85+
return;
86+
}
87+
88+
m_Lambda = value;
89+
m_sharedGenerator = null;
90+
}
91+
}
92+
93+
private void OnValidate()
94+
{
95+
m_sharedGenerator = null;
5896
}
5997
}
6098
}

0 commit comments

Comments
 (0)