Use mask for ActionPool
This commit is contained in:
+34
-55
@@ -1,5 +1,6 @@
|
||||
using Craftimizer.Simulator.Actions;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
@@ -39,77 +40,55 @@ public readonly struct ActionPool
|
||||
});
|
||||
|
||||
public const int MaskSize = 32;
|
||||
public const int EnumSize = 37;
|
||||
|
||||
private unsafe struct EnumBuffer
|
||||
{
|
||||
private fixed byte data[MaskSize];
|
||||
// Bitmask of accepted actions
|
||||
private readonly ulong acceptedActions;
|
||||
|
||||
public ActionType this[int index] => (ActionType)data[index];
|
||||
|
||||
public EnumBuffer(ReadOnlySpan<ActionType> actions)
|
||||
{
|
||||
fixed (byte* dataPtr = data)
|
||||
actions.CopyTo(new Span<ActionType>(dataPtr, MaskSize));
|
||||
}
|
||||
|
||||
public readonly ActionType[] ToArray(int size)
|
||||
{
|
||||
fixed (byte* dataPtr = data)
|
||||
return new Span<ActionType>(dataPtr, size).ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
private unsafe struct LUTBuffer
|
||||
{
|
||||
private fixed byte data[EnumSize];
|
||||
|
||||
public byte this[ActionType index] => data[(byte)index];
|
||||
|
||||
public LUTBuffer(ReadOnlySpan<ActionType> actions)
|
||||
{
|
||||
for (var i = 0; i < EnumSize; i++)
|
||||
data[i] = 0xFF;
|
||||
for (var i = 0; i < actions.Length; i++)
|
||||
data[(byte)actions[i]] = (byte)i;
|
||||
}
|
||||
}
|
||||
|
||||
// List of accepted actions (max 32)
|
||||
private readonly EnumBuffer acceptedActions;
|
||||
// Lookup table for accepted actions (ActionType as idx -> idx in acceptedActions)
|
||||
private readonly LUTBuffer acceptedActionsLUT;
|
||||
private readonly byte size;
|
||||
|
||||
internal ActionType[] AcceptedActions => acceptedActions.ToArray(size);
|
||||
internal ActionType[] AcceptedActions => GetActions();
|
||||
internal int Count => BitOperations.PopCount(acceptedActions);
|
||||
|
||||
public ActionPool(ReadOnlySpan<ActionType> actions)
|
||||
{
|
||||
if (actions.Length > MaskSize)
|
||||
throw new ArgumentOutOfRangeException(nameof(actions), actions.Length, $"ActionPool only supports up to {MaskSize} actions");
|
||||
acceptedActions = 0;
|
||||
foreach (var action in actions)
|
||||
acceptedActions |= 1ul << ((byte)action);
|
||||
|
||||
acceptedActions = new(actions);
|
||||
acceptedActionsLUT = new(actions);
|
||||
size = (byte)actions.Length;
|
||||
if (Count > MaskSize)
|
||||
throw new ArgumentOutOfRangeException(nameof(actions), actions.Length, $"ActionPool only supports up to {MaskSize} actions");
|
||||
}
|
||||
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal byte FromAction(ActionType action)
|
||||
private ActionType[] GetActions()
|
||||
{
|
||||
var ret = acceptedActionsLUT[action];
|
||||
if (ret == 0xFF)
|
||||
throw new ArgumentOutOfRangeException(nameof(action), action, $"Action {action} is unsupported in this pool.");
|
||||
var ret = new ActionType[Count];
|
||||
var i = 0;
|
||||
foreach (var v in (byte[])Enum.GetValuesAsUnderlyingType<ActionType>())
|
||||
{
|
||||
if ((acceptedActions & (1ul << v)) != 0)
|
||||
ret[i++] = (ActionType)v;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal ActionType ToAction(byte index)
|
||||
internal int FromAction(ActionType action)
|
||||
{
|
||||
if (index < 0 || index >= size)
|
||||
if ((acceptedActions & (1ul << (byte)action)) == 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(action), action, "Action is not accepted by this pool.");
|
||||
|
||||
// Get number of 1s before action
|
||||
return BitOperations.PopCount(acceptedActions & ((1ul << (byte)action) - 1));
|
||||
}
|
||||
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal ActionType ToAction(int index)
|
||||
{
|
||||
if (index < 0 || index >= Count)
|
||||
throw new ArgumentOutOfRangeException(nameof(index), index, $"Index {index} is out of range for this pool.");
|
||||
return acceptedActions[index];
|
||||
|
||||
// Get index of (index+1)th 1 in set
|
||||
return (ActionType)Intrinsics.NthBitSet(acceptedActions, index);
|
||||
}
|
||||
|
||||
[Pure]
|
||||
|
||||
Reference in New Issue
Block a user