Index 扉へ 首頁
人類の品種・宇宙人・系外銀河など
simulation library - "StateX" (C#&F#)
0   商湯      2026年2月4日 水 11:09
simulation library - "StateX"
(C# & F#)

Download source files:
http://www.jkofsp.net/files/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Takaku;

namespace Simulation.StateX
{
   class CachedVarOfState
   {  
       private Func<object> FuncValue;
       private object Value;
       private bool Calculated;
       public CachedVarOfState(Func<object> funcValue)
       {
           FuncValue = funcValue;
           Calculated = false;
       }
       public object v()
       {
           if (Calculated)
               return Value;
           else
           {
               Calculated = true;
               return Value = FuncValue();
           }
       }
       public void Flush()
       {
           Calculated = false;
       }
   }

   interface IStateX
   {
       void Update();
       void Flush();
   }

   class ManagerX
   {
       public static ManagerX TheManagerX = new ManagerX();
       private LinkedList<IStateX> AllStateX = new LinkedList<IStateX>();
       public void Add(IStateX stateX)
       {
           AllStateX.AddLast(stateX);
       }
       public void Update()
       {
           foreach (IStateX i in AllStateX)
               i.Update();
       }
       public void Flush()
       {
           foreach (IStateX i in AllStateX)
               i.Flush();
       }
   }

   class StateX<S> : IStateX
   {
       public Dictionary<S, Dictionary<string, CachedVarOfState>> CachedVarsOfState = new Dictionary<S, Dictionary<string, CachedVarOfState>>();
       public Dictionary<S, List<Takaku.Tuple<Func<bool>, S>>> Jumps = new Dictionary<S, List<Takaku.Tuple<Func<bool>, S>>>();
       public S Current, Next;
       public StateX(S defaultValue)
       {
           Current = defaultValue;
           ManagerX.TheManagerX.Add(this);
       }
       public void Update()
       {
           foreach (Takaku.Tuple<Func<bool>, S> i in Jumps[Current])
           {
               if (i._1())
               {
                   Next = i._2;
                   return;
               }
           }

           Next = Current;
       }
       public void Flush()
       {
           Current = Next;

           foreach (KeyValuePair<S, Dictionary<string, CachedVarOfState>> i in CachedVarsOfState)
           {
               foreach (KeyValuePair<string, CachedVarOfState> j in i.Value)
               {
                   j.Value.Flush();
               }
           }
       }
       public object GetCachedVar(string nameCachedVar)
       {
           return CachedVarsOfState[Current][nameCachedVar].v();
       }
   }
}
この記事は 2026年2月4日 水 11:11 に作者によって編集されました
1   商湯      2026年2月4日 水 11:10
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Simulation.StateX;

namespace SimulationStateXCSharp
{
   public partial class Form1 : Form
   {
       bool Exit = false;

       StateX<byte> stateX = new StateX<byte>(1);
       int x1 = 1;
       double x2 = 2;
       bool a = false, b = false, c = false;

       public Form1()
       {
           InitializeComponent();
       }

       private void Form1_Load(object sender, EventArgs e)
       {
           stateX.CachedVarsOfState[1] = new Dictionary<string, CachedVarOfState>();
           stateX.CachedVarsOfState[1]["y1"] = new CachedVarOfState(() => x1 + 1);
           stateX.CachedVarsOfState[1]["y2"] = new CachedVarOfState(() => x2 * 2);

           stateX.CachedVarsOfState[2] = new Dictionary<string, CachedVarOfState>();
           stateX.CachedVarsOfState[2]["y1"] = new CachedVarOfState(() => x1 + 2);
           stateX.CachedVarsOfState[2]["y2"] = new CachedVarOfState(() => x2 * 3);

           stateX.Jumps[1] = new List<Takaku.Tuple<Func<bool>, byte>>();
           stateX.Jumps[1].Add(new Takaku.Tuple<Func<bool>, byte>(() => a, 2));
           stateX.Jumps[1].Add(new Takaku.Tuple<Func<bool>, byte>(() => b, 1));
           
           stateX.Jumps[2] = new List<Takaku.Tuple<Func<bool>, byte>>();
           stateX.Jumps[2].Add(new Takaku.Tuple<Func<bool>, byte>(() => a, 1));
           stateX.Jumps[2].Add(new Takaku.Tuple<Func<bool>, byte>(() => c, 2));
       }

       private void Form1_FormClosed(object sender, FormClosedEventArgs e)
       {
           Exit = true;
       }

       private void Form1_Shown(object sender, EventArgs e)
       {
           while (Exit == false)
           {
               ManagerX.TheManagerX.Update();

               ManagerX.TheManagerX.Flush();

               a = false;
               b = false;
               c = false;

               textBox1.Text = stateX.GetCachedVar("y1").ToString();
               textBox2.Text = stateX.GetCachedVar("y2").ToString();

               Application.DoEvents();
           }
       }

       private void button1_Click(object sender, EventArgs e)
       {
           a = true;
       }

       private void button2_Click(object sender, EventArgs e)
       {
           b = true;
       }

       private void button3_Click(object sender, EventArgs e)
       {
           c = true;
       }
   }
}
2   商湯      2026年2月4日 水 11:10
//F# の詳細 (http://fsharp.net)

namespace Simulation.StateX

   open System
   open System.Collections.Generic

   type CachedVarOfState = class
       val mutable FuncValue : unit -> Object
       [<DefaultValue>]
       val mutable Value : Object
       val mutable Calculated : bool
       new (funcValue : unit -> Object) =
           {
               FuncValue = funcValue;
               Calculated = false;
           }
       member x.v () =
           if x.Calculated then
               x.Value
           else
               x.Calculated <- true
               x.Value <- x.FuncValue ()
               x.Value
       member x.Flush () = x.Calculated <- false
   end

   type IStateX = interface
       abstract Update : unit -> unit
       abstract Flush : unit -> unit
   end

   module ManagerX =
       let AllStateX = new LinkedList<IStateX>()
       let Add (stateX : IStateX) = ignore (AllStateX.AddLast stateX)
       let Update () = for i in AllStateX do i.Update ()
       let Flush () = for i in AllStateX do i.Flush ()

   type StateX<'S when 'S : equality> = class
       interface IStateX with
           member x.Update () =
               for i in x.Jumps.[x.Current] do
                   if (fst i) () then
                       x.Next <- snd i
                       ()
               x.Next <- x.Current
           member x.Flush () =
               x.Current <- x.Next
               for i in x.CachedVarsOfState do
                   for j in i.Value do
                       j.Value.Flush ()
       end
       val mutable CachedVarsOfState : Dictionary<'S, Dictionary<string, CachedVarOfState>>
       val mutable Jumps : Dictionary<'S, List<(unit -> bool) * 'S>> // Dictionary<'S, List<Tuple<unit -> bool, 'S>>>
       val mutable Current : 'S
       [<DefaultValue>]
       val mutable Next : 'S
       new (defaultValue : 'S) as x =
           {
               Current = defaultValue;
               CachedVarsOfState = new Dictionary<'S, Dictionary<string, CachedVarOfState>>();
               Jumps = new Dictionary<'S, List<(unit -> bool) * 'S>>() // Dictionary<'S, List<Tuple<unit -> bool, 'S>>>()
           }
           then
               ManagerX.Add x
       member x.GetCachedVar(nameCachedVar : string) = x.CachedVarsOfState.[x.Current].[nameCachedVar].v ()
   end

   module Main =
       let main =
           let stateX = new StateX<byte>(byte 1)
           let x1 = 1;
           let x2 = 2.0
           let a = false
           let b = false
           let c = false;

           stateX.CachedVarsOfState.[byte 1] <- new Dictionary<string, CachedVarOfState>()
           stateX.CachedVarsOfState.[byte 1].["y1"] <- new CachedVarOfState(fun () -> (x1 + 1) :> Object)
           stateX.CachedVarsOfState.[byte 1].["y2"] <- new CachedVarOfState(fun () -> (x2 * 2.0) :> Object)

           stateX.CachedVarsOfState.[byte 2] <- new Dictionary<string, CachedVarOfState>()
           stateX.CachedVarsOfState.[byte 2].["y1"] <- new CachedVarOfState(fun () -> (x1 + 1) :> Object)
           stateX.CachedVarsOfState.[byte 2].["y2"] <- new CachedVarOfState(fun () -> (x2 * 3.0) :> Object)

           stateX.Jumps.[byte 1] <- new List<(unit -> bool) * byte>()
           stateX.Jumps.[byte 1].Add ((fun () -> a), byte 2)
           stateX.Jumps.[byte 1].Add ((fun () -> b), byte 1)

           stateX.Jumps.[byte 2] <- new List<(unit -> bool) * byte>()
           stateX.Jumps.[byte 2].Add ((fun () -> a), byte 1)
           stateX.Jumps.[byte 2].Add ((fun () -> c), byte 2)
           
           ()
           
名前      編集用パスワード(省略可)    パスワードの記憶
数式 αβγ IPA http:// 画像 B I U 上 下
(添付ファイルは http://www.jkofsp.net/up/ にアップロードしてください)