refactor: Add sourcelink and Github Actions (#28)
* Quick pass * no locks * github actions * publish via github actions * pack
This commit is contained in:
+114
-81
@@ -1,110 +1,143 @@
|
||||
using Grasshopper.Kernel;
|
||||
using System;
|
||||
using System.Windows.Forms;
|
||||
using System.Windows.Forms;
|
||||
using Grasshopper.Kernel;
|
||||
using GrasshopperAsyncComponent;
|
||||
|
||||
namespace GrasshopperAsyncComponentDemo.SampleImplementations
|
||||
namespace GrasshopperAsyncComponentDemo.SampleImplementations;
|
||||
|
||||
public class Sample_PrimeCalculatorAsyncComponent : GH_AsyncComponent
|
||||
{
|
||||
public class Sample_PrimeCalculatorAsyncComponent : GH_AsyncComponent
|
||||
public override Guid ComponentGuid => new Guid("22C612B0-2C57-47CE-B9FE-E10621F18933");
|
||||
|
||||
protected override System.Drawing.Bitmap Icon => 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")
|
||||
{
|
||||
public override Guid ComponentGuid { get => new Guid("22C612B0-2C57-47CE-B9FE-E10621F18933"); }
|
||||
BaseWorker = new PrimeCalculatorWorker(this);
|
||||
}
|
||||
|
||||
protected override System.Drawing.Bitmap Icon { get => Properties.Resources.logo32; }
|
||||
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
|
||||
);
|
||||
}
|
||||
|
||||
public override GH_Exposure Exposure => GH_Exposure.primary;
|
||||
protected override void RegisterOutputParams(GH_OutputParamManager pManager)
|
||||
{
|
||||
pManager.AddNumberParameter("Output", "O", "The n-th prime number.", GH_ParamAccess.item);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public override void AppendAdditionalMenuItems(ToolStripDropDown menu)
|
||||
{
|
||||
base.AppendAdditionalMenuItems(menu);
|
||||
Menu_AppendItem(menu, "Cancel", (s, e) =>
|
||||
public override void AppendAdditionalMenuItems(ToolStripDropDown menu)
|
||||
{
|
||||
base.AppendAdditionalMenuItems(menu);
|
||||
Menu_AppendItem(
|
||||
menu,
|
||||
"Cancel",
|
||||
(s, e) =>
|
||||
{
|
||||
RequestCancellation();
|
||||
});
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private class PrimeCalculatorWorker : WorkerInstance
|
||||
private sealed class PrimeCalculatorWorker : WorkerInstance
|
||||
{
|
||||
private int TheNthPrime { get; set; } = 100;
|
||||
private long ThePrime { get; set; } = -1;
|
||||
|
||||
public PrimeCalculatorWorker(
|
||||
GH_Component? parent,
|
||||
string id = "baseworker",
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
: base(parent, id, cancellationToken) { }
|
||||
|
||||
public override void DoWork(Action<string, double> reportProgress, Action done)
|
||||
{
|
||||
int TheNthPrime { get; set; } = 100;
|
||||
long ThePrime { get; set; } = -1;
|
||||
// 👉 Checking for cancellation!
|
||||
if (CancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
public PrimeCalculatorWorker() : base(null) { }
|
||||
int count = 0;
|
||||
long a = 2;
|
||||
|
||||
public override void DoWork(Action<string, double> ReportProgress, Action Done)
|
||||
// Thanks Steak Overflow (TM) https://stackoverflow.com/a/13001749/
|
||||
while (count < TheNthPrime)
|
||||
{
|
||||
// 👉 Checking for cancellation!
|
||||
if (CancellationToken.IsCancellationRequested) { return; }
|
||||
|
||||
int count = 0;
|
||||
long a = 2;
|
||||
|
||||
// Thanks Steak Overflow (TM) https://stackoverflow.com/a/13001749/
|
||||
while (count < TheNthPrime)
|
||||
if (CancellationToken.IsCancellationRequested)
|
||||
{
|
||||
// 👉 Checking for cancellation!
|
||||
if (CancellationToken.IsCancellationRequested) { return; }
|
||||
|
||||
long b = 2;
|
||||
int prime = 1;// to check if found a prime
|
||||
while (b * b <= a)
|
||||
{
|
||||
// 👉 Checking for cancellation!
|
||||
if (CancellationToken.IsCancellationRequested) { return; }
|
||||
|
||||
if (a % b == 0)
|
||||
{
|
||||
prime = 0;
|
||||
break;
|
||||
}
|
||||
b++;
|
||||
}
|
||||
|
||||
ReportProgress(Id, ((double)count) / TheNthPrime);
|
||||
|
||||
if (prime > 0)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
a++;
|
||||
return;
|
||||
}
|
||||
|
||||
ThePrime = --a;
|
||||
Done();
|
||||
long b = 2;
|
||||
int prime = 1; // to check if found a prime
|
||||
while (b * b <= a)
|
||||
{
|
||||
// 👉 Checking for cancellation!
|
||||
if (CancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (a % b == 0)
|
||||
{
|
||||
prime = 0;
|
||||
break;
|
||||
}
|
||||
b++;
|
||||
}
|
||||
|
||||
reportProgress(Id, (double)count / TheNthPrime);
|
||||
|
||||
if (prime > 0)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
a++;
|
||||
}
|
||||
|
||||
public override WorkerInstance Duplicate() => new PrimeCalculatorWorker();
|
||||
ThePrime = --a;
|
||||
done();
|
||||
}
|
||||
|
||||
public override void GetData(IGH_DataAccess DA, GH_ComponentParamServer Params)
|
||||
public override WorkerInstance Duplicate(string id, CancellationToken cancellationToken) =>
|
||||
new PrimeCalculatorWorker(Parent, id, cancellationToken);
|
||||
|
||||
public override void GetData(IGH_DataAccess da, GH_ComponentParamServer parameters)
|
||||
{
|
||||
int nthPrime = 100;
|
||||
da.GetData(0, ref nthPrime);
|
||||
if (nthPrime > 1000000)
|
||||
{
|
||||
int _nthPrime = 100;
|
||||
DA.GetData(0, ref _nthPrime);
|
||||
if (_nthPrime > 1000000) _nthPrime = 1000000;
|
||||
if (_nthPrime < 1) _nthPrime = 1;
|
||||
|
||||
TheNthPrime = _nthPrime;
|
||||
nthPrime = 1000000;
|
||||
}
|
||||
|
||||
public override void SetData(IGH_DataAccess DA)
|
||||
if (nthPrime < 1)
|
||||
{
|
||||
// 👉 Checking for cancellation!
|
||||
if (CancellationToken.IsCancellationRequested) { return; }
|
||||
|
||||
DA.SetData(0, ThePrime);
|
||||
nthPrime = 1;
|
||||
}
|
||||
|
||||
TheNthPrime = nthPrime;
|
||||
}
|
||||
|
||||
public override void SetData(IGH_DataAccess da)
|
||||
{
|
||||
// 👉 Checking for cancellation!
|
||||
if (CancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
da.SetData(0, ThePrime);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user