Change ActionSet to use pragmas

This commit is contained in:
Asriel Camora
2023-10-31 12:47:30 -07:00
parent a382da066e
commit 439a174d34
+20 -10
View File
@@ -5,21 +5,27 @@ using System.Runtime.CompilerServices;
namespace Craftimizer.Solver; namespace Craftimizer.Solver;
// #define IS_DETERMINISTIC
public struct ActionSet public struct ActionSet
{ {
private const bool IsDeterministic = false;
private uint bits; private uint bits;
[Pure] [Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int FromAction(ActionType action) => Simulator.AcceptedActionsLUT[(byte)action]; private static int FromAction(ActionType action)
{
var ret = Simulator.AcceptedActionsLUT[(byte)action];
if (ret == 0)
throw new ArgumentOutOfRangeException(nameof(action), action, $"Action {action} is unsupported in {nameof(ActionSet)}.");
return ret;
}
[Pure] [Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ActionType ToAction(int index) => Simulator.AcceptedActions[index]; private static ActionType ToAction(int index) => Simulator.AcceptedActions[index];
[Pure] [Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint ToMask(ActionType action) => 1u << FromAction(action) + 1; private static uint ToMask(ActionType action) => 1u << (FromAction(action) + 1);
// Return true if action was newly added and not there before. // Return true if action was newly added and not there before.
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -57,25 +63,28 @@ public struct ActionSet
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ActionType SelectRandom(Random random) public readonly ActionType SelectRandom(Random random)
{ {
if (IsDeterministic) #if IS_DETERMINISTIC
return First(); return First();
#else
return ElementAt(random.Next(Count)); return ElementAt(random.Next(Count));
#endif
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public ActionType PopRandom(Random random) public ActionType PopRandom(Random random)
{ {
if (IsDeterministic) #if IS_DETERMINISTIC
return PopFirst(); return PopFirst();
#else
var action = ElementAt(random.Next(Count)); var action = ElementAt(random.Next(Count));
RemoveAction(action); RemoveAction(action);
return action; return action;
#endif
} }
#if IS_DETERMINISTIC
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public ActionType PopFirst() private ActionType PopFirst()
{ {
var action = First(); var action = First();
RemoveAction(action); RemoveAction(action);
@@ -84,5 +93,6 @@ public struct ActionSet
[Pure] [Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ActionType First() => ElementAt(0); private readonly ActionType First() => ElementAt(0);
#endif
} }