Skip to content

Commit

Permalink
Thinking more...
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiteBlackGoose committed Apr 16, 2021
1 parent 87a86a8 commit 23649c3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 29 deletions.
4 changes: 2 additions & 2 deletions Sources/AngouriMath/Core/Exceptions/Interruptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace AngouriMath.Core.Exceptions
{
public sealed class OutOfQuotaException : Exception
public sealed class OutOfQuotaInterruption : Exception
{
[ConstantField] internal static OutOfQuotaException Instance = new();
[ConstantField] internal static OutOfQuotaInterruption Instance = new();
}
}
7 changes: 4 additions & 3 deletions Sources/AngouriMath/Core/QuotaGC/QuotaCounter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ public static QuotaLeft CreateInfinite()

public void Reset() => left = initial;

public void DecreaseAndCheck()
public bool DecreaseAndCheck()
{
if (infinite)
return;
return true;
left--;
if (left is 0)
throw OutOfQuotaException.Instance;
throw OutOfQuotaInterruption.Instance;
return true;
}
}

Expand Down
55 changes: 31 additions & 24 deletions Sources/Samples/Samples/Program.cs
Original file line number Diff line number Diff line change
@@ -1,55 +1,62 @@
using System;
using AngouriMath;
using AngouriMath.Extensions;
using static AngouriMath.MathS;
using static AngouriMath.Entity;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using AngouriMath.Core;
using AngouriMath.Core.Exceptions;
using System.Collections.Generic;
using System.Linq;


foreach (var res in ComputeMaxInt(40).Take(10))
Console.WriteLine(res);
Console.WriteLine(Simplify(expr: 0, quota: 40));


static Exception? Except(Action action)
static TOut? TryExecuteConstrained<TIn, TOut>(TIn input, int quota, Func<TIn, TOut> alg)
{
using var _ = QuotaCounter.QuotaLeft.Set(QuotaLeft.CreateFinite(quota));
try
{
action();
return alg(input);
}
catch (Exception e)
catch (OutOfQuotaInterruption)
{
return e;
return default;
}
return null;
}

static void AssignIfCan<TIn>(ref TIn toWhat, TIn? value)
{
if (value is { } valid)
toWhat = valid;
}

static IEnumerable<int> ComputeMaxInt(int quota)
static int Simplify(int expr, int quota)
{
static int SubAlgo(int curr)
var subQuota = 5;
while (quota > 0)
{
AssignIfCan(ref expr, TryExecuteConstrained(expr, subQuota, SubSim1));
AssignIfCan(ref expr, TryExecuteConstrained(expr, subQuota, SubSim2));
quota -= subQuota * 2;
subQuota += 2;
}
return expr;

static int SubSim1(int curr)
{
for (int i = 0; i < 15; i++)
for (int i = 0; i < 100; i++)
{
curr++;
QuotaCounter.QuotaLeft.Value.DecreaseAndCheck();
curr++;
}
return curr;
}
using var _ = QuotaCounter.QuotaLeft.Set(QuotaLeft.CreateFinite(quota));
var curr = 0;
while (true)

static int SubSim2(int curr)
{
if (Except(() => curr = SubAlgo(curr)) is OutOfQuotaException)
for (int i = 0; i < 70; i++)
{
yield return curr;
QuotaCounter.QuotaLeft.Value.Reset();
QuotaCounter.QuotaLeft.Value.DecreaseAndCheck();
curr += 2;
}
return curr;
}
}

0 comments on commit 23649c3

Please sign in to comment.