use rule table instead a single jump funcfor state machine

This commit is contained in:
Youmu Liu
2026-01-26 18:05:23 +08:00
parent ce34e8f7e0
commit a36e6cbab4
3 changed files with 73 additions and 18 deletions
+11 -6
View File
@@ -1,4 +1,4 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="ISimpleStateMachine"
// Project: StgSharp
@@ -28,24 +28,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.State
{
public interface ISimpleStateMachine<TStateNode, TStateLabel, TStateContext>
where TStateNode: ISimpleStateMachineNode<TStateLabel, TStateContext>
where TStateLabel: unmanaged, Enum
where TStateContext: StateContext
where TStateNode : ISimpleStateMachineNode<TStateLabel, TStateContext>
where TStateLabel : unmanaged, IEquatable<TStateLabel>
where TStateContext : StateContext
{
ref readonly TStateNode this[TStateLabel label] { get; }
ref readonly TStateNode this[
TStateLabel label
] { get; }
ref readonly TStateNode GetCurrentStateNode();
void Reset();
bool TryMoveNext(TStateContext ctx);
bool TryMoveNext(
TStateContext ctx
);
}
}
@@ -1,9 +1,9 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="ISimpleStateMachineNode"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@@ -28,18 +28,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.State
{
public interface ISimpleStateMachineNode<TLabel, TStateContext> where TLabel: unmanaged, Enum
where TStateContext: StateContext
public abstract class SimpleStateMachineNode<TLabel, TStateContext>
where TLabel : unmanaged, IEquatable<TLabel>
where TStateContext : StateContext
{
public ICollection<TLabel> AvailableStateLabels { get; }
private List<Func<SimpleStateMachineNode<TLabel, TStateContext>, SimpleStateMachineNode<TLabel, TStateContext>, TStateContext, bool>>
_jumpRule;
private List<(int, SimpleStateMachineNode<TLabel, TStateContext>)> _jumpTable;
bool TryGetNextStateLabel(TLabel label, TStateContext context, out TLabel nextLabel);
public IReadOnlyCollection<(int, SimpleStateMachineNode<TLabel, TStateContext>)>
AvailableNodes => _jumpTable;
public abstract bool TryTransaction(
TStateContext context,
out TLabel label
);
}
}