-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvaluationTreeNode.cpp
More file actions
208 lines (173 loc) · 5.82 KB
/
Copy pathEvaluationTreeNode.cpp
File metadata and controls
208 lines (173 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "MathExpression.h"
#include "Exceptions.h"
#include "Globals.h"
#include "String.h"
#include <sstream>
#include <cmath>
namespace mpw {
namespace math_expression {
MathExpression::EvaluationTreeNode::EvaluationTreeNode(PtrToMathFunc ptrToMathFunc)
{
nodeState = STATE_FUNC;
func = ptrToMathFunc;
}
MathExpression::EvaluationTreeNode::EvaluationTreeNode(Number x)
: value(x), nodeState(STATE_CONST)
{
}
MathExpression::EvaluationTreeNode::EvaluationTreeNode(String str)
: varName(str), nodeState(STATE_VAR)
{
}
MathExpression::EvaluationTreeNode::~EvaluationTreeNode()
{
if (nodeState != STATE_FUNC)
return;
//Free the data in the node list, and free the data in all the children's
//node lists.
for (std::list<MathExpression::EvaluationTreeNode*>::iterator it = childNodes.begin();
it != childNodes.end(); ++it)
delete *it;
}
void MathExpression::EvaluationTreeNode::addNode(MathExpression::EvaluationTreeNode* newNode)
{
if (nodeState == STATE_FUNC)
childNodes.push_back(newNode);
else
assert("false"); //This should never happen.
}
String
MathExpression::EvaluationTreeNode::toString() //const
{
switch (nodeState)
{
case STATE_CONST:
{
//:_:TODO:_: Un-const this function; why doesn't it work when this is
//constant?
std::ostringstream oss;
oss << value;
return oss.str();
}
break;
case STATE_VAR:
return varName;
break;
case STATE_FUNC:
{
String str;
return str;
}
break;
}
//We should never reach here.
assert(false);
//Inaccessible code--this is to shut the compiler up when it warns that this
//function does not return anything.
return "";
}
MathExpression::EvaluationTreeNode*
MathExpression::EvaluationTreeNode::substitute(StrNumberMap const& varValues) const
{
EvaluationTreeNode* substituted = subSubstitute(varValues);
substituted->simplify();
return substituted;
}
MathExpression::EvaluationTreeNode*
MathExpression::EvaluationTreeNode::subSubstitute(StrNumberMap const& varValues) const
{
switch (nodeState)
{
case STATE_CONST:
return new MathExpression::EvaluationTreeNode(*this);
break;
case STATE_VAR:
{
if (varValues.find(varName) != varValues.end())
{
MathExpression::EvaluationTreeNode* temp = new MathExpression::EvaluationTreeNode(varValues.find(varName)->second);
return temp;
}
else
return new MathExpression::EvaluationTreeNode(*this);
}
break;
case STATE_FUNC:
{
std::list<MathExpression::EvaluationTreeNode*> substitutedChildNodes;
for (std::list<MathExpression::EvaluationTreeNode*>::const_iterator it = childNodes.begin();
it != childNodes.end(); ++it)
substitutedChildNodes.push_back((*it)->substitute(varValues));
MathExpression::EvaluationTreeNode* substitutedEvaluationTreeNode = new MathExpression::EvaluationTreeNode(func);
substitutedEvaluationTreeNode->childNodes = substitutedChildNodes;
return substitutedEvaluationTreeNode;
}
break;
}
//We should never reach here.
assert(false);
//Inaccessible code--this is to shut the compiler up when it warns that this
//function does not return anything.
return new MathExpression::EvaluationTreeNode(0.0);
}
void
MathExpression::EvaluationTreeNode::groupAssociativeOperation(PtrToMathFunc op)
{
if (nodeState == STATE_FUNC && func == op)
{
//Checks to see if any of the children nodes have that operation.
//If so, add that child's nodes to the current node's own children,
//and get rid of that child node. Repeat this until none of its child
//nodes' functions are op. See the documentation of this function for
//more information.
for (std::list<MathExpression::EvaluationTreeNode*>::iterator it = childNodes.begin();
it != childNodes.end(); ++it)
{
if ((*it)->nodeState == STATE_FUNC && (*it)->func == op)
{
childNodes.merge((*it)->childNodes);
childNodes.erase(it);
it = childNodes.begin();
}
}
}
}
void
MathExpression::EvaluationTreeNode::evaluateFuncsOfConsts()
{
if (nodeState != STATE_FUNC)
return;
//Check to see if all of the children are constants.
std::list<Number> args;
for (std::list<MathExpression::EvaluationTreeNode*>::iterator it = childNodes.begin();
it != childNodes.end(); ++it)
{
if ((*it)->nodeState != STATE_CONST)
break;
else
args.push_back((*it)->value);
}
//True iff every child is a constant.
if (args.size() == childNodes.size())
{
Number valueBuf = (*func)(args);
nodeState = STATE_CONST;
value = valueBuf;
return;
}
//Repeat this function for all the child nodes.
for (std::list<MathExpression::EvaluationTreeNode*>::iterator it = childNodes.begin();
it != childNodes.end(); ++it)
(*it)->evaluateFuncsOfConsts();
}
void
MathExpression::EvaluationTreeNode::simplify()
{
//First, group the associative operations together.
groupAssociativeOperation(&add);
groupAssociativeOperation(&multiply);
//Next, remove any functions of constants that we come across (e.g., sin[8]).
evaluateFuncsOfConsts();
}
} //mpw::math_expression
} //mpw