1
- /**
2
- * Created by mahirshah on 12/11/15.
3
- */
4
- "use strict" ;
1
+ 'use strict' ;
5
2
6
- define ( [ ] , function ( ) { } ) ;
3
+ define ( [ 'exports' , 'ConstantNode' , 'UnaryNode' , 'Operators' ] , function ( exports , _ConstantNode , _UnaryNode , _Operators ) {
4
+ Object . defineProperty ( exports , "__esModule" , {
5
+ value : true
6
+ } ) ;
7
+
8
+ var _ConstantNode2 = _interopRequireDefault ( _ConstantNode ) ;
9
+
10
+ var _UnaryNode2 = _interopRequireDefault ( _UnaryNode ) ;
11
+
12
+ function _interopRequireDefault ( obj ) {
13
+ return obj && obj . __esModule ? obj : {
14
+ default : obj
15
+ } ;
16
+ }
17
+
18
+ function _classCallCheck ( instance , Constructor ) {
19
+ if ( ! ( instance instanceof Constructor ) ) {
20
+ throw new TypeError ( "Cannot call a class as a function" ) ;
21
+ }
22
+ }
23
+
24
+ var _createClass = ( function ( ) {
25
+ function defineProperties ( target , props ) {
26
+ for ( var i = 0 ; i < props . length ; i ++ ) {
27
+ var descriptor = props [ i ] ;
28
+ descriptor . enumerable = descriptor . enumerable || false ;
29
+ descriptor . configurable = true ;
30
+ if ( "value" in descriptor ) descriptor . writable = true ;
31
+ Object . defineProperty ( target , descriptor . key , descriptor ) ;
32
+ }
33
+ }
34
+
35
+ return function ( Constructor , protoProps , staticProps ) {
36
+ if ( protoProps ) defineProperties ( Constructor . prototype , protoProps ) ;
37
+ if ( staticProps ) defineProperties ( Constructor , staticProps ) ;
38
+ return Constructor ;
39
+ } ;
40
+ } ) ( ) ;
41
+
42
+ var EquationSolver = ( function ( ) {
43
+ function EquationSolver ( expressionTree1 , expressionTree2 ) {
44
+ var _this = this ;
45
+
46
+ _classCallCheck ( this , EquationSolver ) ;
47
+
48
+ var context = arguments . length <= 2 || arguments [ 2 ] === undefined ? { } : arguments [ 2 ] ;
49
+ this . expressionTree1 = expressionTree1 ;
50
+ this . expressionTree2 = expressionTree2 ;
51
+ this . context = context ;
52
+ this . numUnknowns = 0 ;
53
+ this . unknownTree = expressionTree1 . getExpressionTree ( ) ;
54
+ this . knownTree = expressionTree2 . getExpressionTree ( ) ;
55
+ this . knownTreeStack = [ ] ;
56
+ this . unknownTreeStack = [ ] ;
57
+ expressionTree1 . getExpressionTree ( ) . iterate ( function ( constant ) {
58
+ if ( isNaN ( constant ) && ! context [ constant ] ) {
59
+ _this . numUnknowns += 1 ;
60
+ }
61
+ } ) ;
62
+ expressionTree2 . getExpressionTree ( ) . iterate ( function ( constant ) {
63
+ if ( isNaN ( constant ) && ! context [ constant ] ) {
64
+ _this . numUnknowns += 1 ;
65
+ _this . unknownTree = _this . expressionTree2 . getExpressionTree ( ) ;
66
+ _this . knownTree = _this . expressionTree2 . getExpressionTree ( ) ;
67
+ }
68
+ } ) ;
69
+ }
70
+
71
+ _createClass ( EquationSolver , [ {
72
+ key : 'evaluate' ,
73
+ value : function evaluate ( ) {
74
+ if ( this . numUnknowns === 0 ) {
75
+ return this . expressionTree1 . evaluate ( this . context ) === this . expressionTree2 . evaluate ( this . context ) ;
76
+ } else if ( this . numUnknowns > 1 ) {
77
+ throw new Error ( 'Too many unknowns' ) ;
78
+ }
79
+
80
+ while ( ! this . _doStepOfAlegbra ( ) ) {
81
+ this . unknownTreeStack . push ( this . unknownTree . toInfix ( ) ) ;
82
+ this . knownTreeStack . push ( this . knownTree . toInfix ( ) ) ;
83
+ }
84
+
85
+ return this . unknownTree . toInfix ( ) + ' = ' + this . knownTree . toInfix ( ) ;
86
+ }
87
+ } , {
88
+ key : '_doStepOfAlegbra' ,
89
+ value : function _doStepOfAlegbra ( ) {
90
+ if ( this . unknownTree instanceof _ConstantNode2 . default ) {
91
+ return true ;
92
+ } else if ( this . unknownTree instanceof _UnaryNode2 . default ) {
93
+ this . knownTree = _Operators . OPERATOR_INVERSE_MAP . get ( this . unknownTree . operator ) ( this . knownTree ) ;
94
+ this . unknownTree = this . unknownTree . operand ;
95
+ } else if ( this . unknownTree . left . isUnknown ( this . context ) ) {
96
+ this . knownTree = _Operators . OPERATOR_INVERSE_MAP . get ( this . unknownTree . operator ) ( this . knownTree , this . unknownTree . right ) ;
97
+ this . unknownTree = this . unknownTree . left ;
98
+ } else {
99
+ this . knownTree = _Operators . OPERATOR_INVERSE_MAP . get ( this . unknownTree . operator ) ( this . knownTree , this . unknownTree . left ) ;
100
+ this . unknownTree = this . unknownTree . right ;
101
+ }
102
+
103
+ return false ;
104
+ }
105
+ } ] ) ;
106
+
107
+ return EquationSolver ;
108
+ } ) ( ) ;
109
+
110
+ exports . default = EquationSolver ;
111
+ } ) ;
0 commit comments