-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathSample_PrimeCalculatorAsyncComponent.cs
executable file
·125 lines (101 loc) · 3.47 KB
/
Sample_PrimeCalculatorAsyncComponent.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using Grasshopper.Kernel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using GrasshopperAsyncComponent;
using System.Windows.Forms;
namespace GrasshopperAsyncComponentDemo.SampleImplementations
{
public class Sample_PrimeCalculatorAsyncComponent : GH_AsyncComponent
{
public override Guid ComponentGuid { get => new Guid("22C612B0-2C57-47CE-B9FE-E10621F18933"); }
protected override System.Drawing.Bitmap Icon { get => Properties.Resources.logo32; }
public override GH_Exposure Exposure => GH_Exposure.primary;
public Sample_PrimeCalculatorAsyncComponent() : base("The N-th Prime Calculator", "PRIME", "Calculates the nth prime number.", "Samples", "Async")
{
BaseWorker = new PrimeCalculatorWorker();
}
protected override void RegisterInputParams(GH_InputParamManager pManager)
{
pManager.AddIntegerParameter("N", "N", "Which n-th prime number. Minimum 1, maximum one million. Take care, it can burn your CPU.", GH_ParamAccess.item);
}
protected override void RegisterOutputParams(GH_OutputParamManager pManager)
{
pManager.AddNumberParameter("Output", "O", "The n-th prime number.", GH_ParamAccess.item);
}
protected override void AppendAdditionalComponentMenuItems(ToolStripDropDown menu)
{
base.AppendAdditionalComponentMenuItems(menu);
Menu_AppendItem(menu, "Cancel", (s, e) =>
{
this.RequestCancellation();
});
}
public override void AppendAdditionalMenuItems(ToolStripDropDown menu)
{
base.AppendAdditionalMenuItems(menu);
Menu_AppendItem(menu, "Cancel", (s, e) =>
{
RequestCancellation();
});
}
}
public class PrimeCalculatorWorker : WorkerInstance
{
int TheNthPrime { get; set; } = 100;
long ThePrime { get; set; } = -1;
public PrimeCalculatorWorker() : base(null) { }
public override void DoWork(Action<string, double> ReportProgress, Action Done)
{
// 👉 Checking for cancellation!
if (CancellationToken.IsCancellationRequested) { Done(); return; }
int count = 0;
long a = 2;
// Thanks Steak Overflow (TM) https://stackoverflow.com/a/13001749/
while (count < TheNthPrime)
{
// 👉 Checking for cancellation!
if (CancellationToken.IsCancellationRequested) { Done(); return; }
long b = 2;
int prime = 1;// to check if found a prime
while (b * b <= a)
{
// 👉 Checking for cancellation!
if (CancellationToken.IsCancellationRequested) { Done(); return; }
if (a % b == 0)
{
prime = 0;
break;
}
b++;
}
ReportProgress(Id, ((double)count) / TheNthPrime);
if (prime > 0)
{
count++;
}
a++;
}
ThePrime = --a;
Done();
}
public override WorkerInstance Duplicate() => new PrimeCalculatorWorker();
public override void GetData(IGH_DataAccess DA, GH_ComponentParamServer Params)
{
int _nthPrime = 100;
DA.GetData(0, ref _nthPrime);
if (_nthPrime > 1000000) _nthPrime = 1000000;
if (_nthPrime < 1) _nthPrime = 1;
TheNthPrime = _nthPrime;
}
public override void SetData(IGH_DataAccess DA)
{
// 👉 Checking for cancellation!
if (CancellationToken.IsCancellationRequested) { return; }
DA.SetData(0, ThePrime);
}
}
}