Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Computed array values in C# - reactive programming
- class Foo {
- public int this[int index] {
- get {
- return index*2; //Your computation here
- }
- }
- }
- class Foo {
- public int this[int row,int col] {
- get {
- return row*col; //Your computation here
- }
- }
- }
- class Cell {
- private int _row;
- private int _col;
- public Cell(int row,int col) {
- _row = row;
- _col = col;
- }
- public int Value {
- get {
- return _row * _col;
- }
- }
- }
- class ComputedArray<T>
- {
- private Func<T>[] _array;
- public T this[int index] { get { return _array[index]( ); } }
- public void Set(int index, Func<T> func)
- {
- _array[index] = func;
- }
- public ComputedArray( int size )
- {
- _array = new Func<T>[size];
- }
- }
- ComputedArray<int> ar = new ComputedArray<int>( 2 );
- ar.Set( 0, ( ) => 2 );
- ar.Set( 1, ( ) => ar[0]*2 );
- Console.WriteLine( ar[0] );
- Console.WriteLine( ar[1] );
- public abstract class Expression
- {
- List<Expression> linkedExpressions;
- protected Expression lhs; // left hand side, right hand side
- protected Expression rhs;
- protected Expression(Expression x, Expression y)
- {
- List<Expression> linkedExpressions = new List<Expression>();
- lhs = x;
- rhs = y;
- // let the expressions know that they have a expression dependant on them
- lhs.NotifyExpressionLinked(this);
- rhs.NotifyExpressionLinked(this);
- }
- private void NotifyExpressionLinked(Expression e)
- {
- if (e != null)
- {
- linkedExpressions.Add(e);
- }
- }
- private void NotifyExpressionUnlinked(Expression e)
- {
- if (linkedExpressions.Contains(e)
- {
- linkedExpressions.Remove(e);
- }
- }
- // this method will notify all subscribed expressions that
- // one of the values they are dependant on has changed
- private void NotifyExpressionChanged()
- {
- if (linkedExpressions.Count != 0) // if we're not a leaf node
- {
- foreach (Expression e in linkedExpressions)
- {
- e.NotifyExpressionChanged();
- }
- }
- else Evaluate()
- // once we're at a point where there are no dependant expressions
- // to notify we can start evaluating
- }
- // if we only want to update the lhs, y will be null, and vice versa
- public sealed void UpdateValues(Expression x, Expression y)
- {
- if (x != null)
- {
- lhs.NotifyExpressionUnlinked(this);
- x.NotifyExpressionLinked(this);
- lhs = x;
- }
- if (y != null)
- {
- rhs.NotifyExpressionUnlinked(this);
- y.NotifyExpressionLinked(this);
- rhs = y;
- }
- NotifyExpressionChanged();
- }
- public virtual float Evaluate()
- {
- throw new NotImplementedException(); // we expect child classes to implement this
- }
- }
- public class LiteralExpression : Expression
- {
- private float value;
- public LiteralExpression(float x)
- : base(null, null) { } // should not have any linked expressions
- public override float Evaluate()
- {
- return value;
- }
- }
- public class AdditionExpression : Expression
- {
- public AdditionExpression(Expression x, Expression y)
- : base(x, y) { };
- public override float Evaluate()
- {
- return lhs.Evaluate() + rhs.Evaluate();
- }
- }
Add Comment
Please, Sign In to add comment