diff --git a/fsm/FSM.cs b/fsm/FSM.cs index afe3cec..15fc7ce 100644 --- a/fsm/FSM.cs +++ b/fsm/FSM.cs @@ -37,10 +37,15 @@ public class FSM { Context = context; State = state; + + State.onEnter( Context, state ); } public void Transition(ST newState) { + State.onExit( Context, newState ); + newState.onEnter( Context, State ); + State = newState; } diff --git a/imm/Imm.cs b/imm/Imm.cs index 43c5f57..70a9ecb 100644 --- a/imm/Imm.cs +++ b/imm/Imm.cs @@ -1,22 +1,27 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using System.Linq; +using System; +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Linq; using System.Runtime.CompilerServices; -using System.Text; -using System.Threading; +using System.Text; +using System.Threading; using System.Threading.Tasks; // A spot for immutable helpers namespace imm { + public record class Versioned where T : Versioned { + public delegate void ChangeDelegate( T old, T next ); + public uint Version { get; protected set; } = 0; public string Reason { get; protected set; } = ""; + public ChangeDelegate OnChange; + public T Process( Func fn, string reason = "" ) { var newT = fn( ( T )this ); @@ -45,15 +50,21 @@ namespace imm { var orig = ( T )this; - var newT = base.Process( ( old ) => old with + var next = ( T ) this with { + //Do the Versioned code here + Version = orig.Version + 1, + Reason = reason, + Old = orig, MemberName = memberName, FilePath = filePath, LineNumber = lineNumber, - }, reason ); + }; - return newT; + OnChange( orig, next ); + + return next; } public T Process( Func fn, string reason = "", @@ -65,10 +76,13 @@ namespace imm { var orig = ( T )this; - return ( T )this with + var next = ( T )fn( orig ); + + var ret = ( T )next with { //Do the Versioned code here Version = orig.Version + 1, + Reason = reason, //Recorded Old = orig, @@ -77,6 +91,10 @@ namespace imm FilePath = filePath, LineNumber = lineNumber, }; + + OnChange( orig, ret ); + + return ret; } } @@ -87,7 +105,19 @@ namespace imm public readonly DateTime CreatedAt = DateTime.Now; public DateTime TouchedAt { get; set; } = DateTime.Now; - public T Process( Func fn, string reason = "", + + public T Process( T next, string reason = "", + [CallerMemberName] string memberName = "", + [CallerFilePath] string filePath = "", + [CallerLineNumber] int lineNumber = 0, + [CallerArgumentExpression("next")] + string expression = default ) + { + return Process( ( old ) => next, reason, memberName, filePath, lineNumber, expression ); + } + + + new public T Process( Func fn, string reason = "", [CallerMemberName] string memberName = "", [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0, @@ -96,10 +126,13 @@ namespace imm { var orig = ( T )this; - return ( T )this with + var next = ( T )fn( orig ); + + var ret = ( T )next with { //Versioned Version = orig.Version + 1, + Reason = reason, //Recorded Old = orig, @@ -108,8 +141,13 @@ namespace imm FilePath = filePath, LineNumber = lineNumber, + //Timed TouchedAt = DateTime.Now, }; + + OnChange( orig, ret ); + + return ret; } } @@ -121,5 +159,5 @@ namespace imm } -} - +} + diff --git a/imm/List.cs b/imm/List.cs new file mode 100644 index 0000000..d789711 --- /dev/null +++ b/imm/List.cs @@ -0,0 +1,158 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Collections.Immutable; + +namespace imm; + +public record class List : Timed>, IImmutableList +{ + + ImmutableList Values = ImmutableList.Empty; + + + + public T this[int index] => (( IReadOnlyList )Values)[index]; + + public int Count => Values.Count; + + public List Add( T value ) + { + return this with { Values = Values.Add( value ) }; + } + + public List AddRange( IEnumerable items ) + { + return this with { Values = Values.AddRange( items ) }; + } + + public List Clear() + { + return this with { Values = Values.Clear() }; + } + + public IEnumerator GetEnumerator() + { + return Values.GetEnumerator(); + } + + public int IndexOf( T item, int index, int count, IEqualityComparer equalityComparer ) + { + return Values.IndexOf( item, index, count, equalityComparer ); + } + + public int IndexOf( T item ) + { + return Values.IndexOf( item, 0, Count, EqualityComparer.Default ); + } + + public List Insert( int index, T element ) + { + return this with { Values = Values.Insert( index, element ) }; + } + + public List InsertRange( int index, IEnumerable items ) + { + return this with { Values = Values.InsertRange( index, items ) }; + } + + public int LastIndexOf( T item, int index, int count, IEqualityComparer equalityComparer ) + { + return Values.LastIndexOf( item, index, count, equalityComparer ); + } + + + + public List Remove( T value ) + { + return Remove( value, EqualityComparer.Default ); + } + + public List Remove( T value, IEqualityComparer equalityComparer ) + { + return this with { Values = Values.Remove( value, equalityComparer ) }; + } + + public List RemoveAll( Predicate match ) + { + return this with { Values = Values.RemoveAll( match ) }; + } + + public List RemoveAt( int index ) + { + return this with { Values = Values.RemoveAt( index ) }; + } + + public List RemoveRange( IEnumerable items, IEqualityComparer equalityComparer ) + { + return this with { Values = Values.RemoveRange( items, equalityComparer ) }; + } + + public List RemoveRange( int index, int count ) + { + return this with { Values = Values.RemoveRange( index, count ) }; + } + + public List Replace( T oldValue, T newValue, IEqualityComparer equalityComparer ) + { + return this with { Values = Values.Replace( oldValue, newValue, equalityComparer ) }; + } + + public List SetItem( int index, T value ) + { + return this with { Values = Values.SetItem( index, value ) }; + } + + IEnumerator IEnumerable.GetEnumerator() + { + return (( IEnumerable )Values).GetEnumerator(); + } + + IImmutableList IImmutableList.Clear() + { + return Clear(); + } + + IImmutableList IImmutableList.Add( T value ) + { + return Add( value ); + } + + IImmutableList IImmutableList.AddRange( IEnumerable items ) + { + return AddRange( items ); + } + + IImmutableList IImmutableList.Insert( int index, T element ) + { + return Insert( index, element ); + } + + IImmutableList IImmutableList.InsertRange( int index, IEnumerable items ) + { + return InsertRange( index, items ); + } + + IImmutableList IImmutableList.Remove( T value, IEqualityComparer equalityComparer ) + { + return Remove( value, equalityComparer ); + } + + IImmutableList IImmutableList.RemoveAll( Predicate match ) + { + return RemoveAll( match ); + } + + IImmutableList IImmutableList.RemoveAt( int index ) => RemoveAt( index ); + + IImmutableList IImmutableList.RemoveRange( IEnumerable items, IEqualityComparer equalityComparer ) + => RemoveRange( items, equalityComparer ); + + IImmutableList IImmutableList.RemoveRange( int index, int count ) => RemoveRange( index, count ); + + IImmutableList IImmutableList.Replace( T oldValue, T newValue, IEqualityComparer equalityComparer ) + => Replace( oldValue, newValue, equalityComparer ); + + IImmutableList IImmutableList.SetItem( int index, T value ) => SetItem( index, value ); +} + diff --git a/imm/Util.cs b/imm/Util.cs new file mode 100644 index 0000000..d77d58e --- /dev/null +++ b/imm/Util.cs @@ -0,0 +1,4 @@ +using System; +using System.Collections.Generic; + +namespace imm; diff --git a/math/fn.cs b/math/fn.cs index 55e96e7..32f5e21 100644 --- a/math/fn.cs +++ b/math/fn.cs @@ -14,6 +14,9 @@ namespace math static public class fn { + static public float ToDeg( float rad ) => ( float )(180.0 / Math.PI) * rad; + static public float ToRad( float deg ) => ( float )(Math.PI / 180.0) * deg; + static public float Clamp( float v, float min, float max ) { return v < min ? min : v > max ? max : v; diff --git a/ser/SerializableDictionary.cs b/ser/SerializableDictionary.cs index 6d5e99b..90de8db 100644 --- a/ser/SerializableDictionary.cs +++ b/ser/SerializableDictionary.cs @@ -62,7 +62,8 @@ namespace lib } } - [SecurityPermission( SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter )] + + //[SecurityPermission( SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter )] void ISerializable.GetObjectData( SerializationInfo info, StreamingContext context ) { info.AddValue( "ItemCount", this.Count );