Guest User

Untitled

a guest
Aug 18th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. Computed array values in C# - reactive programming
  2. class Foo {
  3. public int this[int index] {
  4. get {
  5. return index*2; //Your computation here
  6. }
  7. }
  8. }
  9.  
  10. class Foo {
  11. public int this[int row,int col] {
  12. get {
  13. return row*col; //Your computation here
  14. }
  15. }
  16. }
  17.  
  18. class Cell {
  19. private int _row;
  20. private int _col;
  21.  
  22. public Cell(int row,int col) {
  23. _row = row;
  24. _col = col;
  25. }
  26.  
  27. public int Value {
  28. get {
  29. return _row * _col;
  30. }
  31. }
  32. }
  33.  
  34. class ComputedArray<T>
  35. {
  36. private Func<T>[] _array;
  37.  
  38. public T this[int index] { get { return _array[index]( ); } }
  39.  
  40. public void Set(int index, Func<T> func)
  41. {
  42. _array[index] = func;
  43. }
  44.  
  45. public ComputedArray( int size )
  46. {
  47. _array = new Func<T>[size];
  48. }
  49. }
  50.  
  51. ComputedArray<int> ar = new ComputedArray<int>( 2 );
  52. ar.Set( 0, ( ) => 2 );
  53. ar.Set( 1, ( ) => ar[0]*2 );
  54. Console.WriteLine( ar[0] );
  55. Console.WriteLine( ar[1] );
  56.  
  57. public abstract class Expression
  58. {
  59. List<Expression> linkedExpressions;
  60. protected Expression lhs; // left hand side, right hand side
  61. protected Expression rhs;
  62.  
  63. protected Expression(Expression x, Expression y)
  64. {
  65. List<Expression> linkedExpressions = new List<Expression>();
  66. lhs = x;
  67. rhs = y;
  68. // let the expressions know that they have a expression dependant on them
  69. lhs.NotifyExpressionLinked(this);
  70. rhs.NotifyExpressionLinked(this);
  71. }
  72.  
  73. private void NotifyExpressionLinked(Expression e)
  74. {
  75. if (e != null)
  76. {
  77. linkedExpressions.Add(e);
  78. }
  79. }
  80.  
  81. private void NotifyExpressionUnlinked(Expression e)
  82. {
  83. if (linkedExpressions.Contains(e)
  84. {
  85. linkedExpressions.Remove(e);
  86. }
  87. }
  88.  
  89. // this method will notify all subscribed expressions that
  90. // one of the values they are dependant on has changed
  91. private void NotifyExpressionChanged()
  92. {
  93. if (linkedExpressions.Count != 0) // if we're not a leaf node
  94. {
  95. foreach (Expression e in linkedExpressions)
  96. {
  97. e.NotifyExpressionChanged();
  98. }
  99. }
  100. else Evaluate()
  101. // once we're at a point where there are no dependant expressions
  102. // to notify we can start evaluating
  103. }
  104.  
  105. // if we only want to update the lhs, y will be null, and vice versa
  106. public sealed void UpdateValues(Expression x, Expression y)
  107. {
  108. if (x != null)
  109. {
  110. lhs.NotifyExpressionUnlinked(this);
  111. x.NotifyExpressionLinked(this);
  112. lhs = x;
  113. }
  114.  
  115. if (y != null)
  116. {
  117. rhs.NotifyExpressionUnlinked(this);
  118. y.NotifyExpressionLinked(this);
  119. rhs = y;
  120. }
  121.  
  122. NotifyExpressionChanged();
  123. }
  124.  
  125. public virtual float Evaluate()
  126. {
  127. throw new NotImplementedException(); // we expect child classes to implement this
  128. }
  129. }
  130.  
  131. public class LiteralExpression : Expression
  132. {
  133. private float value;
  134.  
  135. public LiteralExpression(float x)
  136. : base(null, null) { } // should not have any linked expressions
  137.  
  138. public override float Evaluate()
  139. {
  140. return value;
  141. }
  142. }
  143.  
  144. public class AdditionExpression : Expression
  145. {
  146. public AdditionExpression(Expression x, Expression y)
  147. : base(x, y) { };
  148.  
  149. public override float Evaluate()
  150. {
  151. return lhs.Evaluate() + rhs.Evaluate();
  152. }
  153. }
Add Comment
Please, Sign In to add comment