Commit ead77415 authored by shrabbit's avatar shrabbit
Browse files

feat(Worker/Playgrounds): 新增 Worker AOT 控制台项目并纳入解决方案,新增 EEEnum/Factorial 游乐场,Combin 改用 BigInteger

parent 27482d3d
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -2,6 +2,8 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestV", "TestV\TestV.csproj", "{BEFAA4EC-9AE2-48BA-8687-6CE765BF3B50}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Worker", "Worker\Worker.csproj", "{4EFDB6A0-2E5D-4E1A-8484-A1C7FF535739}"
EndProject
Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
		Debug|Any CPU = Debug|Any CPU
@@ -12,5 +14,9 @@ Global
		{BEFAA4EC-9AE2-48BA-8687-6CE765BF3B50}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{BEFAA4EC-9AE2-48BA-8687-6CE765BF3B50}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{BEFAA4EC-9AE2-48BA-8687-6CE765BF3B50}.Release|Any CPU.Build.0 = Release|Any CPU
		{4EFDB6A0-2E5D-4E1A-8484-A1C7FF535739}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{4EFDB6A0-2E5D-4E1A-8484-A1C7FF535739}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{4EFDB6A0-2E5D-4E1A-8484-A1C7FF535739}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{4EFDB6A0-2E5D-4E1A-8484-A1C7FF535739}.Release|Any CPU.Build.0 = Release|Any CPU
	EndGlobalSection
EndGlobal
+6 −5
Original line number Diff line number Diff line
using Spectre.Console;
using System.Numerics;
using Spectre.Console;
using TestV.Tools;

namespace TestV.Playgrounds;
@@ -12,8 +13,8 @@ public class Combine : IPlayground
    {
        if (args.Length >= 2)
        {
            var r = int.Parse(args[0]);
            var n = int.Parse(args[1]);
            var r = BigInteger.Parse(args[0]);
            var n = BigInteger.Parse(args[1]);

            if (n == 0 || r == n)
            {
@@ -27,7 +28,7 @@ public class Combine : IPlayground
                return Task.CompletedTask;
            }
            
            AnsiConsole.WriteLine($"{Factorial(r, n) / Factorial(n)}\n");
            AnsiConsole.WriteLine($"{Factorial(r, n) / Factorial(n, 99999)}\n");
            
            return Task.CompletedTask;
        }
@@ -36,7 +37,7 @@ public class Combine : IPlayground
        return Task.CompletedTask;
    }

    public static int Factorial(int value, int deep = int.MaxValue)
    public static BigInteger Factorial(BigInteger value, BigInteger deep)
    {
        var result = value;
        for (var i = value - 1; i >= 1; i--)
+123 −0
Original line number Diff line number Diff line
using System.Collections;
using Spectre.Console;

namespace TestV.Playgrounds;

public class EEEnum : Playground<EEEnum>
{
    private const string R1 = "TestV";
    private const string R2 = "innnnd";
    private const string R3 = "found";
    private const string R4 = "release our moment";
    private const string R5 = "not at all";
    private const string R6 = "spectre.console";
    private string[] _pack = [R1, R2, R3, R4, R5, R6];
    
    private interface IInnerPlugin
    {
        IEnumerable<string> Rn();
    }

    private void Test(IInnerPlugin plugin)
    {
        var i = 0;
        AnsiConsole.WriteLine();
        foreach (var c in plugin.Rn())
        {
            Compare(c, _pack[i]);
            i++;
        }
        AnsiConsole.WriteLine();
    }

    private void Compare(string input, string reference) => AnsiConsole.MarkupLine($"[{(input == reference ? "green" : "red")}]{input}[/]");
    
    private class A : IInnerPlugin
    {
        public IEnumerable<string> Rn()
        {
            yield return R1;
            yield return R2;
            yield return R3;
            yield return R4;
            yield return R5;
            yield return R6;
        }
    }

    private class B : IInnerPlugin
    {
        public IEnumerable<string> Rn() => [R1, R2, R3, R4, R5, R6];
    }

    private class C : IInnerPlugin
    {
        public IEnumerable<string> Rn() => new Enum();
        
        private class Enum : IEnumerable<string>
        {
            public IEnumerator<string> GetEnumerator() => new Enumerator();

            IEnumerator IEnumerable.GetEnumerator()
            {
                return GetEnumerator();
            }
        }
        
        private class Enumerator : IEnumerator<string>
        {
            private string _current;
            private int _index;

            public bool MoveNext()
            {
                if (_index >= 6)
                {
                    return false;
                }

                _current = _index switch
                {
                    0 => R1,
                    1 => R2,
                    2 => R3,
                    3 => R4,
                    4 => R5,
                    5 => R6,
                    _ => throw new NotImplementedException()
                };

                _index++;

                return true;
            }

            public void Reset()
            {
                _index = 0;
            }

            string IEnumerator<string>.Current => _current;

            object? IEnumerator.Current => _current;

            public void Dispose()
            {
                
            }
        }
    }
    
    public override async Task Run(string[] args)
    {
        foreach (var i in new IInnerPlugin[]
                 {
                     new A(),
                     new B(),
                     new C()
                 })
        {
            Test(i);
        }
    }
}
 No newline at end of file
+43 −0
Original line number Diff line number Diff line
using System.Numerics;
using Spectre.Console;

namespace TestV.Playgrounds;

public class Factorial : Playground<Factorial>
{
    /// <summary>
    /// 计算大整数阶乘(针对 10万+ 位数场景优化)
    /// </summary>
    public static BigInteger Fact(BigInteger n)
    {
        if (n < 0) throw new ArgumentOutOfRangeException(nameof(n), "阶乘未定义负数");
        if (n <= 1) return BigInteger.One;

        // 使用分治法(乘积树)替代顺序累乘
        return MultiplyRange(BigInteger.One, n);
    }

    private static BigInteger MultiplyRange(BigInteger low, BigInteger high)
    {
        if (low > high) return BigInteger.One;
        if (low == high) return low;

        // 小区间直接循环,避免递归调用开销
        if (high - low < 16)
        {
            BigInteger res = low;
            for (var i = low + 1; i <= high; i++) res *= i;
            return res;
        }

        // 二分区间:medium × medium 比 huge × small 快得多
        BigInteger mid = (low + high) / 2;
        return MultiplyRange(low, mid) * MultiplyRange(mid + 1, high);
    }

    public override async Task Run(string[] args)
    {
        var value = BigInteger.Parse(args[0]);
        AnsiConsole.WriteLine($"{Fact(value)}\n");
    }
}
 No newline at end of file

Worker/Program.cs

0 → 100644
+13 −0
Original line number Diff line number Diff line
namespace Worker;

class Program
{
    static void Main(string[] args)
    {
        while (true)
        {
            Console.WriteLine(DateTime.Now);
            Thread.Sleep(1000);
        }
    }
}
 No newline at end of file
Loading