add state nodes for regex state machine

This commit is contained in:
Youmu Liu
2026-01-27 18:40:44 +08:00
parent a36e6cbab4
commit 3881078770
13 changed files with 510 additions and 820 deletions
+33 -19
View File
@@ -1,4 +1,4 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Counter"
// Project: StgSharp
@@ -30,26 +30,30 @@ using System.Numerics;
namespace StgSharp
{
public class Counter<T> where T: unmanaged, INumber<T>
public class Counter<T> where T : unmanaged, INumber<T>
{
internal dynamic _value;
internal readonly T _defaultDecrement;
internal readonly T _defaultIncrement;
internal readonly T _defaultValue;
internal readonly T _defualtDecreament;
internal readonly T _defualtIncreament;
public Counter(T defaultValue, T defaultIncrement, T defaultDecrement)
public Counter(
T defaultValue,
T defaultIncrement,
T defaultDecrement
)
{
_value = defaultValue;
_defaultValue = defaultValue;
_defualtIncreament = defaultIncrement;
_defualtDecreament = defaultDecrement;
_defaultIncrement = defaultIncrement;
_defaultDecrement = defaultDecrement;
}
public T DecrementSpan => _defualtDecreament;
public T DecrementSpan => _defaultDecrement;
public T IncrementSpan => _defualtIncreament;
public T IncrementSpan => _defaultIncrement;
public T CurrentSpan => _value - _defaultValue;
@@ -59,19 +63,21 @@ namespace StgSharp
set => _value = value;
}
public void Add(T value)
public void Add(
T value
)
{
_value += value;
}
public void QuickAdd()
{
_value += _defualtIncreament;
_value += _defaultIncrement;
}
public void QuickSub()
{
_value -= _defualtDecreament;
_value -= _defaultDecrement;
}
public void Reset()
@@ -79,26 +85,34 @@ namespace StgSharp
_value = _defaultValue;
}
public void Sub(T value)
public void Sub(
T value
)
{
_value -= value;
}
public override string ToString() => Value.ToString();
public static Counter<T> operator --(Counter<T> counter)
public static Counter<T> operator --(
Counter<T> counter
)
{
counter.QuickSub();
counter?.QuickSub();
return counter;
}
public static Counter<T> operator ++(Counter<T> counter)
public static Counter<T> operator ++(
Counter<T> counter
)
{
counter.QuickAdd();
counter?.QuickAdd();
return counter;
}
public static implicit operator T(Counter<T> counter)
public static implicit operator T(
Counter<T> counter
)
{
return counter switch
{
+1
View File
@@ -33,6 +33,7 @@ using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("StgSharp.Mathematics")]
[assembly: InternalsVisibleTo("StgSharp.RegularAnalysis")]
[assembly: InternalsVisibleTo("StgSharpDebug")]
[assembly: SuppressMessage(
+5 -5
View File
@@ -2,8 +2,8 @@
// -----------------------------------------------------------------------
// file="ISimpleStateMachine"
// 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
@@ -35,13 +35,13 @@ using System.Threading.Tasks;
namespace StgSharp.State
{
public interface ISimpleStateMachine<TStateNode, TStateLabel, TStateContext>
where TStateNode : ISimpleStateMachineNode<TStateLabel, TStateContext>
where TStateNode : SimpleStateMachineNode<TStateNode, TStateLabel, TStateContext>
where TStateLabel : unmanaged, IEquatable<TStateLabel>
where TStateContext : StateContext
{
ref readonly TStateNode this[
TStateLabel label
TStateNode this[
TStateLabel label
] { get; }
ref readonly TStateNode GetCurrentStateNode();
@@ -1,6 +1,6 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="ISimpleStateMachineNode"
// file="SimpleStateMachineNode"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
@@ -29,24 +29,29 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.State
{
public abstract class SimpleStateMachineNode<TLabel, TStateContext>
public abstract class SimpleStateMachineNode<TSelf, TLabel, TStateContext>
where TSelf : SimpleStateMachineNode<TSelf, TLabel, TStateContext>
where TLabel : unmanaged, IEquatable<TLabel>
where TStateContext : StateContext
{
private List<Func<SimpleStateMachineNode<TLabel, TStateContext>, SimpleStateMachineNode<TLabel, TStateContext>, TStateContext, bool>>
_jumpRule;
private List<(int, SimpleStateMachineNode<TLabel, TStateContext>)> _jumpTable;
private readonly List<int> _jumpTable = [];
public IReadOnlyCollection<(int, SimpleStateMachineNode<TLabel, TStateContext>)>
AvailableNodes => _jumpTable;
public ReadOnlySpan<int> AvailableNodes => CollectionsMarshal.AsSpan(_jumpTable);
public abstract bool IsValidateFrom(
TSelf former,
TStateContext context
);
public abstract bool TryTransaction(
ReadOnlySpan<TSelf> nodeSpan,
TStateContext context,
out TLabel label
);