Basic Polynomial Algebra Subprograms (BPAS)  v. 1.791
ExpressionTree.hpp
1 
2 #ifndef _EXPRESSION_TREE_HPP_
3 #define _EXPRESSION_TREE_HPP_
4 
5 #include <string>
6 #include <gmpxx.h>
7 #include <vector>
8 #include "ExprTreeNode.hpp"
9 
10 /**
11  * An ExpressionTree encompasses various forms of data that can be expressed generically
12  * as a binary tree with data elements connected by operators.
13  *
14  * Provides functions for converting this generic datatype into various external formats.
15  * For example, maple expression strings or latex mathmode strings.
16  */
18 private:
19  ExprTreeNode* root;
20 
21 public:
22 
23  /**
24  * Construct the default, empty tree.
25  */
27 
28  /**
29  * Construct a tree given the root node.
30  */
31  ExpressionTree(ExprTreeNode* theRoot);
32 
33  /**
34  * Copy constructor.
35  */
36  ExpressionTree(const ExpressionTree& otherTree);
37 
38  /**
39  * Construct a stree from a vector of trees as an array of trees.
40  */
41  ExpressionTree(const std::vector<ExpressionTree>& trees);
42 
43  /**
44  * Destructor.
45  */
47 
48  /**
49  * Copy assignment.
50  */
51  ExpressionTree& operator= (const ExpressionTree& otherTree);
52 
53 #if __GNUC__ >= 5
54 
55  /**
56  * Move constructor.
57  */
58  ExpressionTree(ExpressionTree&& otherTree);
59 
60  /**
61  * Move assignment.
62  */
64 
65 #endif
66 
67  /**
68  * Combine two expression trees using the supplied type and value as their common
69  * parent. This method will create copies of the input trees to use.
70  *
71  * returns the adjoined ExpressionTree.
72  */
73  static ExpressionTree combineExpressionTrees(const ExpressionTree& left, const ExpressionTree& right, ExprTreeType type, ExprTreeVal* val = NULL);
74 
75  /**
76  * Combine *this and the input rightTree into a new expression tree
77  * adjoining the two trees with addition.
78  *
79  * returns the new tree.
80  */
81  ExpressionTree operator+(const ExpressionTree& rightTree);
82 
83  /**
84  * Add the input rightTree to the right side of *this, adjoining the two
85  * trees with addition.
86  *
87  * returns a reference to *this, the updated tree.
88  */
89  ExpressionTree& operator+=(const ExpressionTree& rightTree);
90 
91  /**
92  * Combine *this and the input rightTree into a new expression tree
93  * adjoining the two trees with subtraction.
94  *
95  * returns the new tree.
96  */
97  ExpressionTree operator-(const ExpressionTree& rightTree);
98 
99  /**
100  * Add the input rightTree to the right side of *this, adjoining the two
101  * trees with subtraction.
102  *
103  * returns a reference to *this, the updated tree.
104  */
105  ExpressionTree& operator-=(const ExpressionTree& rightTree);
106 
107  /**
108  * Combine *this and the input rightTree into a new expression tree
109  * adjoining the two trees with multiplication.
110  *
111  * returns the new tree.
112  */
113  ExpressionTree operator*(const ExpressionTree& rightTree);
114 
115  /**
116  * Add the input rightTree to the right side of *this, adjoining the two
117  * trees with multiplication.
118  *
119  * returns a reference to *this, the updated tree.
120  */
121  ExpressionTree& operator*=(const ExpressionTree& rightTree);
122 
123  /**
124  * Combine *this and the input rightTree into a new expression tree
125  * adjoining the two trees with divison.
126  *
127  * returns the new tree.
128  */
129  ExpressionTree operator/(const ExpressionTree& rightTree);
130 
131  /**
132  * Add the input rightTree to the right side of *this, adjoining the two
133  * trees with division.
134  *
135  * returns a reference to *this, the updated tree.
136  */
137  ExpressionTree& operator/=(const ExpressionTree& rightTree);
138 
139  /**
140  * Combine *this and the input rightTree into a new expression tree
141  * adjoining the two trees with divison.
142  *
143  * returns the new tree.
144  */
145  ExpressionTree operator^(const ExpressionTree& rightTree);
146 
147  /**
148  * Add the input rightTree to the right side of *this, adjoining the two
149  * trees with division.
150  *
151  * returns a reference to *this, the updated tree.
152  */
153  ExpressionTree& operator^=(const ExpressionTree& rightTree);
154 
155  /**
156  * Convert *this to a generic string representation.
157  */
158  std::string toString() const;
159 
160  /**
161  * Convert *this to a string in the format expected of a maple expression.
162  *
163  * returns a string in maple format
164  */
165  std::string toMapleString() const;
166 
167  /**
168  * Convert *this to a string in the format expected of LaTeX.
169  *
170  * returns a string in latex format.
171  */
172  std::string toLaTeXString() const;
173 
174  /**
175  * Fill this expression tree with the vector of BPASRing elements specified.
176  * This clears this tree's current contents.
177  * ringVec: a vector of BPASRing elements.
178  */
179  template <class ExpTreeConvert>
180  void fromVector(const std::vector<ExpTreeConvert>& ringVec) {
181  delete root;
182  root = NULL;
183  for(size_t i = 0; i < ringVec.size(); ++i) {
184  ExpressionTree elem = ringVec[i].convertToExpressionTree();
185  root = ExprTreeNode::combineExprTreeNodes(root, elem.root, EXPR_ARRAY, NULL);
186  elem.root = NULL;
187  }
188  root = ExprTreeNode::combineExprTreeNodes(root, NULL, EXPR_ARRAY, NULL);
189  }
190 };
191 
192 /**
193  * An interface defining conversion of a class to an ExpressionTree.
194  */
196 
197 public:
198  /**
199  * Convert this to an expression tree.
200  *
201  * returns an expression tree describing *this.
202  */
203  virtual ExpressionTree convertToExpressionTree() const = 0;
204 };
205 
206 
207 #endif
static ExpressionTree combineExpressionTrees(const ExpressionTree &left, const ExpressionTree &right, ExprTreeType type, ExprTreeVal *val=NULL)
Combine two expression trees using the supplied type and value as their common parent.
ExprTreeVal is the data element of a ExprTreeNode.
Definition: ExprTreeNode.hpp:57
ExpressionTree operator+(const ExpressionTree &rightTree)
Combine *this and the input rightTree into a new expression tree adjoining the two trees with additio...
An ExpressionTree encompasses various forms of data that can be expressed generically as a binary tre...
Definition: ExpressionTree.hpp:17
std::string toLaTeXString() const
Convert *this to a string in the format expected of LaTeX.
ExpressionTree operator/(const ExpressionTree &rightTree)
Combine *this and the input rightTree into a new expression tree adjoining the two trees with divison...
ExpressionTree operator^(const ExpressionTree &rightTree)
Combine *this and the input rightTree into a new expression tree adjoining the two trees with divison...
std::string toString() const
Convert *this to a generic string representation.
static ExprTreeNode * combineExprTreeNodes(ExprTreeNode *lNode, ExprTreeNode *rNode, ExprTreeType tType, ExprTreeVal *vVal=NULL)
Creates a new ExprTreeNode and combines two sub-trees (lNode and rNode) by adjoining them to the newl...
ExpressionTree & operator^=(const ExpressionTree &rightTree)
Add the input rightTree to the right side of *this, adjoining the two trees with division.
ExpressionTree operator*(const ExpressionTree &rightTree)
Combine *this and the input rightTree into a new expression tree adjoining the two trees with multipl...
ExpressionTree & operator-=(const ExpressionTree &rightTree)
Add the input rightTree to the right side of *this, adjoining the two trees with subtraction.
ExpressionTree & operator+=(const ExpressionTree &rightTree)
Add the input rightTree to the right side of *this, adjoining the two trees with addition.
void fromVector(const std::vector< ExpTreeConvert > &ringVec)
Fill this expression tree with the vector of BPASRing elements specified.
Definition: ExpressionTree.hpp:180
ExpressionTree operator-(const ExpressionTree &rightTree)
Combine *this and the input rightTree into a new expression tree adjoining the two trees with subtrac...
ExpressionTree & operator/=(const ExpressionTree &rightTree)
Add the input rightTree to the right side of *this, adjoining the two trees with division.
std::string toMapleString() const
Convert *this to a string in the format expected of a maple expression.
ExprTreeNode is a single node in the bianry tree of an ExpressionTree.
Definition: ExprTreeNode.hpp:76
ExpressionTree & operator=(const ExpressionTree &otherTree)
Copy assignment.
An interface defining conversion of a class to an ExpressionTree.
Definition: ExpressionTree.hpp:195
ExpressionTree & operator*=(const ExpressionTree &rightTree)
Add the input rightTree to the right side of *this, adjoining the two trees with multiplication.
ExpressionTree()
Construct the default, empty tree.
~ExpressionTree()
Destructor.