Remove threadlocal dependence
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Craftimizer.Simulator.Actions;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.Intrinsics.X86;
|
||||
@@ -42,23 +43,29 @@ public struct ActionSet
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static int FromAction(ActionType action) => Array.IndexOf(Simulator.AcceptedActions, action);
|
||||
private static int FromAction(ActionType action) => Simulator.AcceptedActionsLUT[(byte)action];
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static ActionType ToAction(int index) => Simulator.AcceptedActions[index];
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly bool HasAction(ActionType action) => (bits & (1u << (FromAction(action) + 1))) != 0;
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void AddAction(ActionType action) => bits |= 1u << (FromAction(action) + 1);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void RemoveAction(ActionType action) => bits &= ~(1u << (FromAction(action) + 1));
|
||||
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly bool HasAction(ActionType action) => (bits & (1u << (FromAction(action) + 1))) != 0;
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly ActionType ElementAt(int index) => ToAction(NthBitSet(bits, index) - 1);
|
||||
|
||||
[Pure]
|
||||
public readonly int Count => BitOperations.PopCount(bits);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly ActionType SelectRandom(Random random) => ElementAt(random.Next(Count));
|
||||
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly ActionType First() => ElementAt(0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user