Get rid of ActionPool, use ulong for ActionSet

This commit is contained in:
Asriel Camora
2024-02-29 02:47:59 -08:00
parent 85922b225c
commit c8d3161eb1
7 changed files with 112 additions and 226 deletions
+30 -18
View File
@@ -7,13 +7,25 @@ namespace Craftimizer.Solver;
public struct ActionSet
{
internal uint bits;
internal ulong bits;
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static int FromAction(ActionType action) => (byte)action;
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static ActionType ToAction(int index) => (ActionType)index;
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static ulong ToMask(ActionType action) => 1ul << FromAction(action);
// Return true if action was newly added and not there before.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool AddAction(in ActionPool pool, ActionType action)
public bool AddAction(ActionType action)
{
var mask = pool.ToMask(action);
var mask = ToMask(action);
var old = bits;
bits |= mask;
return (old & mask) == 0;
@@ -21,9 +33,9 @@ public struct ActionSet
// Return true if action was newly removed and not already gone.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool RemoveAction(in ActionPool pool, ActionType action)
public bool RemoveAction(ActionType action)
{
var mask = pool.ToMask(action);
var mask = ToMask(action);
var old = bits;
bits &= ~mask;
return (old & mask) != 0;
@@ -31,10 +43,10 @@ public struct ActionSet
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool HasAction(in ActionPool pool, ActionType action) => (bits & pool.ToMask(action)) != 0;
public readonly bool HasAction(ActionType action) => (bits & ToMask(action)) != 0;
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ActionType ElementAt(in ActionPool pool, int index) => pool.ToAction(Intrinsics.NthBitSet(bits, index) - 1);
public readonly ActionType ElementAt(int index) => ToAction(Intrinsics.NthBitSet(bits, index));
[Pure]
public readonly int Count => BitOperations.PopCount(bits);
@@ -43,38 +55,38 @@ public struct ActionSet
public readonly bool IsEmpty => bits == 0;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ActionType SelectRandom(in ActionPool pool, Random random)
public readonly ActionType SelectRandom(Random random)
{
#if IS_DETERMINISTIC
return First(in pool);
return First();
#else
return ElementAt(in pool, random.Next(Count));
return ElementAt(random.Next(Count));
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ActionType PopRandom(in ActionPool pool, Random random)
public ActionType PopRandom(Random random)
{
#if IS_DETERMINISTIC
return PopFirst(in pool);
return PopFirst();
#else
var action = ElementAt(in pool, random.Next(Count));
RemoveAction(in pool, action);
var action = ElementAt(random.Next(Count));
RemoveAction(action);
return action;
#endif
}
#if IS_DETERMINISTIC
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private ActionType PopFirst(in pool)
private ActionType PopFirst()
{
var action = First(in pool);
RemoveAction(in pool, action);
var action = First();
RemoveAction(action);
return action;
}
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private readonly ActionType First(in pool) => ElementAt(in pool, 0);
private readonly ActionType First() => ElementAt(0);
#endif
}