amath  1.8.5 Simple command line calculator
Optimizer Class Reference

`#include <optimizer.h>`

Collaboration diagram for Optimizer:

## Public Member Functions

Optimizer (SyntaxNode *root)

~Optimizer ()

void Optimize () const

SyntaxNodeGetRoot () const

## Static Public Member Functions

static int GetTreeDepth (SyntaxNode *node, int depth)

## Static Private Member Functions

static void TagChildren (SyntaxNode *node)

static void BalanceTree (SyntaxNode *node)

static void ReduceUnaryNodes (SyntaxNode *node)

static void ReduceValueNodes (SyntaxNode *node)

static void TagStartNode (SyntaxNode *node)

SyntaxNoderoot

## Detailed Description

Definition at line 39 of file optimizer.h.

## ◆ Optimizer()

 Optimizer::Optimizer ( SyntaxNode * root )
explicit

Definition at line 35 of file optimizer.cpp.

References root.

Referenced by Evaluator::Evaluate().

36 {
37  this->root = root;
38 }
SyntaxNode * root
Definition: optimizer.h:50
Here is the caller graph for this function:

## ◆ ~Optimizer()

 Optimizer::~Optimizer ( )

Definition at line 40 of file optimizer.cpp.

41 {
42 }

## ◆ BalanceTree()

 void Optimizer::BalanceTree ( SyntaxNode * node )
staticprivate

Definition at line 69 of file optimizer.cpp.

Referenced by BalanceTree(), and Optimize().

70 {
71  if (node == nullptr)
72  return;
73
74  node->ResetIterator();
75
76  SyntaxNode* current = node->GetNext();
77
78  while (current != nullptr)
79  {
80  if (current->GetNodeType() == expression)
81  {
82  ExpressionNode* troot = static_cast<ExpressionNode*>(current);
83  troot->ResetIterator();
84  ExpressionNode* pivot = static_cast<ExpressionNode*>(troot->GetNext());
85  if (pivot != nullptr && troot->GetPrecedence() == pivot->GetPrecedence())
86  {
87  int rdepth = GetTreeDepth(troot, 1);
88  int pdepth = GetTreeDepth(pivot, 0);
89  if (rdepth - pdepth > 1 || rdepth - pdepth < -1)
90  {
91  pivot->ResetIterator();
92  pivot->GetNext();
93  ExpressionNode* child = static_cast<ExpressionNode*>(pivot->GetNext());
94  if (child != nullptr)
95  {
96  SyntaxNode* parent = troot->GetParent();
97  parent->Detach(troot);
98  troot->Detach(pivot);
99  pivot->Detach(child);
100  troot->Attach(child);
101  pivot->Attach(troot);
102  parent->Attach(pivot);
103  current = pivot;
104  current->ResetIterator();
105  }
106  }
107  }
108  }
109
110  current = node->GetNext();
111  BalanceTree(current);
112  }
113 }
virtual void Detach(SyntaxNode *node)=0
virtual void Attach(SyntaxNode *node)=0
Base class for all nodes in a syntax tree.
Definition: nodes.h:65
virtual void ResetIterator()
Definition: nodes.cpp:80
virtual NodeType GetNodeType()=0
static void BalanceTree(SyntaxNode *node)
Definition: optimizer.cpp:69
SyntaxNode * GetParent() const
Definition: nodes.cpp:70
virtual int GetPrecedence()=0
virtual SyntaxNode * GetNext()=0
Base class for all nodes related to mathematical expressions.
Definition: nodes.h:99
static int GetTreeDepth(SyntaxNode *node, int depth)
Definition: optimizer.cpp:115
Here is the call graph for this function:
Here is the caller graph for this function:

## ◆ GetRoot()

 SyntaxNode * Optimizer::GetRoot ( ) const

Definition at line 44 of file optimizer.cpp.

References root.

Referenced by Evaluator::Evaluate().

45 {
46  return root;
47 }
SyntaxNode * root
Definition: optimizer.h:50
Here is the caller graph for this function:

## ◆ GetTreeDepth()

 int Optimizer::GetTreeDepth ( SyntaxNode * node, int depth )
static

Definition at line 115 of file optimizer.cpp.

References SyntaxNode::GetNext(), GetTreeDepth(), and SyntaxNode::ResetIterator().

Referenced by BalanceTree(), and GetTreeDepth().

116 {
117  int max = depth;
118  SyntaxNode* current;
119  node->ResetIterator();
120
121  while ((current = node->GetNext()) != nullptr)
122  {
123  int a = GetTreeDepth(current, depth + 1);
124  if (a > max)
125  {
126  max = a;
127  }
128  }
129
130  return max;
131 }
Base class for all nodes in a syntax tree.
Definition: nodes.h:65
virtual void ResetIterator()
Definition: nodes.cpp:80
virtual SyntaxNode * GetNext()=0
static int GetTreeDepth(SyntaxNode *node, int depth)
Definition: optimizer.cpp:115
Here is the call graph for this function:
Here is the caller graph for this function:

## ◆ Optimize()

 void Optimizer::Optimize ( ) const

Definition at line 49 of file optimizer.cpp.

References BalanceTree(), ReduceUnaryNodes(), ReduceValueNodes(), root, TagChildren(), and TagStartNode().

Referenced by Evaluator::Evaluate().

50 {
56 }
SyntaxNode * root
Definition: optimizer.h:50
static void ReduceUnaryNodes(SyntaxNode *node)
Definition: optimizer.cpp:133
static void BalanceTree(SyntaxNode *node)
Definition: optimizer.cpp:69
static void TagStartNode(SyntaxNode *node)
Definition: optimizer.cpp:214
static void ReduceValueNodes(SyntaxNode *node)
Definition: optimizer.cpp:175
static void TagChildren(SyntaxNode *node)
Definition: optimizer.cpp:58
Here is the call graph for this function:
Here is the caller graph for this function:

## ◆ ReduceUnaryNodes()

 void Optimizer::ReduceUnaryNodes ( SyntaxNode * node )
staticprivate

Definition at line 133 of file optimizer.cpp.

Referenced by Optimize(), and ReduceUnaryNodes().

134 {
135  SyntaxNode* current;
136  node->ResetIterator();
137
138  while ((current = node->GetNext()) != nullptr)
139  {
140  ReduceUnaryNodes(current);
141
142  if (current->GetReductionType() == unaryreduc)
143  {
144  ExpressionNode* expression = static_cast<ExpressionNode*>(current);
145  expression->ResetIterator();
146  ExpressionNode* next = static_cast<ExpressionNode*>(expression->GetNext());
147
148  if (next->GetReductionType() == unaryreduc)
149  {
150  next->ResetIterator();
151  SyntaxNode* temp = next->GetNext();
152  next->Detach(temp);
153  SyntaxNode* parent = expression->GetParent();
154  parent->Replace(expression, temp);
155  current = parent;
156  current->ResetIterator();
157  }
158  else if (next->GetReductionType() == valuereduc)
159  {
160  NumericValueNode* valueNode = static_cast<NumericValueNode*>(next);
161  Number* number = valueNode->Evaluate();
162  Number* modified = number->Unary();
163  valueNode->ReplaceWith(modified);
164
165  current->Detach(valueNode);
166  SyntaxNode* parent = current->GetParent();
167  parent->Replace(current, valueNode);
168  current = parent;
169  current->ResetIterator();
170  }
171  }
172  }
173 }
virtual ReductionType GetReductionType()
Definition: nodes.cpp:55
virtual void Detach(SyntaxNode *node)=0
Base class for all nodes in a syntax tree.
Definition: nodes.h:65
virtual void Replace(SyntaxNode *n, SyntaxNode *x)=0
Definition: numb.h:66
virtual Number * Unary()=0
static void ReduceUnaryNodes(SyntaxNode *node)
Definition: optimizer.cpp:133
virtual void ResetIterator()
Definition: nodes.cpp:80
void ReplaceWith(Number *value)
Definition: values.cpp:427
Number * Evaluate()
Definition: values.cpp:393
SyntaxNode * GetParent() const
Definition: nodes.cpp:70
Use of a numeric value in a syntax tree.
Definition: values.h:150
virtual SyntaxNode * GetNext()=0
Base class for all nodes related to mathematical expressions.
Definition: nodes.h:99
Here is the call graph for this function:
Here is the caller graph for this function:

## ◆ ReduceValueNodes()

 void Optimizer::ReduceValueNodes ( SyntaxNode * node )
staticprivate

Definition at line 175 of file optimizer.cpp.

Referenced by Optimize(), and ReduceValueNodes().

176 {
177  SyntaxNode* current;
178  node->ResetIterator();
179
180  while ((current = node->GetNext()) != nullptr)
181  {
182  if (current->GetNodeType() == expression)
183  {
184  ExpressionNode* expression = static_cast<ExpressionNode*>(current);
185  ReductionType reduction = expression->GetReductionType();
186  if (reduction == compladdreduc || reduction == complsubreduc)
187  {
188  expression->ResetIterator();
189  NumericValueNode* first = static_cast<NumericValueNode*>(expression->GetNext());
190  NumericValueNode* second = static_cast<NumericValueNode*>(expression->GetNext());
191  if (
192  first->GetReductionType() == valuereduc && second->GetReductionType() == valuereduc &&
193  ((first->Evaluate()->PureComplexValue() && !second->Evaluate()->PureComplexValue()) ||
194  (!first->Evaluate()->PureComplexValue() && second->Evaluate()->PureComplexValue()))
195  )
196  {
197  Number* number =
200  first->Evaluate()->Sub(second->Evaluate());
201  NumericValueNode* reducedNode = new NumericValueNode(number);
202  SyntaxNode* parent = current->GetParent();
203  parent->Replace(current, reducedNode);
204  current = parent;
205  current->ResetIterator();
206  }
207  }
208  }
209
210  ReduceValueNodes(current);
211  }
212 }
virtual ReductionType GetReductionType()
Definition: values.cpp:381
virtual ReductionType GetReductionType()
Definition: nodes.cpp:55
Base class for all nodes in a syntax tree.
Definition: nodes.h:65
virtual void Replace(SyntaxNode *n, SyntaxNode *x)=0
Definition: numb.h:66
virtual void ResetIterator()
Definition: nodes.cpp:80
ReductionType
Definition: nodes.h:49
virtual NodeType GetNodeType()=0
Number * Evaluate()
Definition: values.cpp:393
static void ReduceValueNodes(SyntaxNode *node)
Definition: optimizer.cpp:175
SyntaxNode * GetParent() const
Definition: nodes.cpp:70
virtual Number * Sub(Number *other)=0
Use of a numeric value in a syntax tree.
Definition: values.h:150
virtual SyntaxNode * GetNext()=0
Base class for all nodes related to mathematical expressions.
Definition: nodes.h:99
virtual bool PureComplexValue()=0
Here is the call graph for this function:
Here is the caller graph for this function:

## ◆ TagChildren()

 void Optimizer::TagChildren ( SyntaxNode * node )
staticprivate

Definition at line 58 of file optimizer.cpp.

Referenced by Optimize(), and TagChildren().

59 {
60  SyntaxNode* current;
61  node->ResetIterator();
62  while ((current = node->GetNext()) != nullptr)
63  {
64  current->SetParent(node);
65  TagChildren(current);
66  }
67 }
Base class for all nodes in a syntax tree.
Definition: nodes.h:65
void SetParent(SyntaxNode *node)
Definition: nodes.cpp:75
virtual void ResetIterator()
Definition: nodes.cpp:80
virtual SyntaxNode * GetNext()=0
static void TagChildren(SyntaxNode *node)
Definition: optimizer.cpp:58
Here is the call graph for this function:
Here is the caller graph for this function:

## ◆ TagStartNode()

 void Optimizer::TagStartNode ( SyntaxNode * node )
staticprivate

Definition at line 214 of file optimizer.cpp.

Referenced by Optimize(), and TagStartNode().

215 {
216  node->ResetIterator();
217  SyntaxNode* next = node->GetNext();
218
219  if (next != nullptr)
220  {
221  TagStartNode(next);
222  }
223  else
224  {
225  node->SetFirstNode();
226  }
227 }
Base class for all nodes in a syntax tree.
Definition: nodes.h:65
void SetFirstNode()
Definition: nodes.cpp:65
virtual void ResetIterator()
Definition: nodes.cpp:80
static void TagStartNode(SyntaxNode *node)
Definition: optimizer.cpp:214
virtual SyntaxNode * GetNext()=0
Here is the call graph for this function:
Here is the caller graph for this function:

## ◆ root

 SyntaxNode* Optimizer::root
private

Definition at line 50 of file optimizer.h.

Referenced by GetRoot(), Optimize(), and Optimizer().

The documentation for this class was generated from the following files: