Skip to content

Commit

Permalink
Change ActionSet to use pragmas
Browse files Browse the repository at this point in the history
  • Loading branch information
WorkingRobot committed Oct 31, 2023
1 parent 76c30f4 commit c8e35f9
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions Solver/ActionSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,27 @@

namespace Craftimizer.Solver;

// #define IS_DETERMINISTIC

public struct ActionSet
{
private const bool IsDeterministic = false;

private uint bits;

[Pure]
[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]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ActionType ToAction(int index) => Simulator.AcceptedActions[index];
[Pure]
[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.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -57,25 +63,28 @@ public bool RemoveAction(ActionType action)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ActionType SelectRandom(Random random)
{
if (IsDeterministic)
return First();

#if IS_DETERMINISTIC
return First();
#else
return ElementAt(random.Next(Count));
#endif
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ActionType PopRandom(Random random)
{
if (IsDeterministic)
return PopFirst();

#if IS_DETERMINISTIC
return PopFirst();
#else
var action = ElementAt(random.Next(Count));
RemoveAction(action);
return action;
#endif
}

#if IS_DETERMINISTIC
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ActionType PopFirst()
private ActionType PopFirst()
{
var action = First();
RemoveAction(action);
Expand All @@ -84,5 +93,6 @@ public ActionType PopFirst()

[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ActionType First() => ElementAt(0);
private readonly ActionType First() => ElementAt(0);
#endif
}

0 comments on commit c8e35f9

Please sign in to comment.