00001
00015 #include "OSOutput.h"
00016 #include "OSExpressionTree.h"
00017 #include <vector>
00018
00019 using std::endl;
00020
00021 OSExpressionTree::OSExpressionTree():
00022
00023
00024 mapVarIdx( NULL),
00025 m_bIndexMapGenerated( false),
00026 bADMustReTape( false),
00027 bDestroyNlNodes( true)
00028 {
00029 }
00030
00031
00032 OSExpressionTree::~OSExpressionTree()
00033 {
00034 #ifndef NDEBUG
00035 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
00036 "Inside the OSExpressionTree Destructor");
00037 #endif
00038
00039
00040
00041
00042
00043 if(mapVarIdx != NULL)
00044 {
00045 delete mapVarIdx;
00046 mapVarIdx = NULL;
00047 }
00048 }
00049
00050 #if 0
00051 std::vector<ExprNode*> OSExpressionTree::getPrefixFromExpressionTree()
00052 {
00053 return m_treeRoot->getPrefixFromExpressionTree();
00054 }
00055
00056
00057 std::vector<ExprNode*> OSExpressionTree::getPostfixFromExpressionTree()
00058 {
00059 return m_treeRoot->getPostfixFromExpressionTree();
00060 }
00061 #endif
00062
00063 #if 0
00064 double OSExpressionTree::calculateFunction( double *x, bool new_x)
00065 {
00066
00067 if( new_x == false)
00068 {
00069 return m_dTreeRootValue;
00070 }
00071 else
00072 {
00073 m_dTreeRootValue = m_treeRoot->calculateFunction( x);
00074 return m_dTreeRootValue;
00075 }
00076 }
00077 #endif
00078
00079 bool OSExpressionTree::IsEqual(OSExpressionTree *that)
00080 {
00081 #ifndef NDEBUG
00082 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_trace, "Start comparing in OSExpressionTree");
00083 #endif
00084 if (this == NULL)
00085 {
00086 if (that == NULL)
00087 return true;
00088 else
00089 {
00090 #ifndef NDEBUG
00091 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_trace, "First object is NULL, second is not");
00092 #endif
00093 return false;
00094 }
00095 }
00096 else
00097 {
00098 if (that == NULL)
00099 {
00100 #ifndef NDEBUG
00101 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_trace, "Second object is NULL, first is not");
00102 #endif
00103 return false;
00104 }
00105 else
00106 {
00107 if (this->m_bIndexMapGenerated != that->m_bIndexMapGenerated)
00108 return false;
00109 if (this->bADMustReTape != that->bADMustReTape)
00110 return false;
00111 if (this->bDestroyNlNodes != that->bDestroyNlNodes)
00112 return false;
00113 return true;
00114 }
00115 }
00116 }
00117
00118
00119 ScalarExpressionTree::ScalarExpressionTree():
00120 m_treeRoot( NULL)
00121 {
00122 #ifndef NDEBUG
00123 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
00124 "Inside the ScalarExpressionTree Constructor");
00125 #endif
00126 }
00127
00128
00129 ScalarExpressionTree::~ScalarExpressionTree()
00130 {
00131 #ifndef NDEBUG
00132 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
00133 "Inside the ScalarExpressionTree Destructor");
00134 #endif
00135 if( bDestroyNlNodes == true)
00136 {
00137 if(m_treeRoot != NULL) delete m_treeRoot;
00138 m_treeRoot = NULL;
00139 }
00140 }
00141
00142 std::vector<ExprNode*> ScalarExpressionTree::getPrefixFromExpressionTree()
00143 {
00144 return m_treeRoot->getPrefixFromExpressionTree();
00145 }
00146
00147
00148 std::vector<ExprNode*> ScalarExpressionTree::getPostfixFromExpressionTree()
00149 {
00150 return m_treeRoot->getPostfixFromExpressionTree();
00151 }
00152
00153
00154 std::map<int, int> *ScalarExpressionTree::getVariableIndicesMap()
00155 {
00156 if( m_bIndexMapGenerated == true) return mapVarIdx;
00157 mapVarIdx = new std::map<int, int>();
00158 std::map<int, int>::iterator m_mPosVarIdx;
00159 m_treeRoot->getVariableIndexMap( mapVarIdx);
00160 m_bIndexMapGenerated = true;
00161 return mapVarIdx;
00162 }
00163
00164
00165 double ScalarExpressionTree::calculateFunction( double *x, bool new_x)
00166 {
00167
00168 if( new_x == false)
00169 {
00170 return m_dTreeRootValue;
00171 }
00172 else
00173 {
00174 m_dTreeRootValue = m_treeRoot->calculateFunction( x);
00175 return m_dTreeRootValue;
00176 }
00177 }
00178
00179
00180 bool ScalarExpressionTree::IsEqual(ScalarExpressionTree *that)
00181 {
00182 #ifndef NDEBUG
00183 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_trace, "Start comparing in ScalarExpressionTree");
00184 #endif
00185 if (this == NULL)
00186 {
00187 if (that == NULL)
00188 return true;
00189 else
00190 {
00191 #ifndef NDEBUG
00192 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_trace, "First object is NULL, second is not");
00193 #endif
00194 return false;
00195 }
00196 }
00197 else
00198 {
00199 if (that == NULL)
00200 {
00201 #ifndef NDEBUG
00202 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_trace, "Second object is NULL, first is not");
00203 #endif
00204 return false;
00205 }
00206 else
00207 {
00208 if (!this->m_treeRoot->IsEqual(that->m_treeRoot))
00209 return false;
00210
00211 return this->OSExpressionTree::IsEqual(that);
00212 }
00213 }
00214 }
00215
00216
00217 MatrixExpressionTree::MatrixExpressionTree():
00218 m_treeRoot( NULL)
00219 {
00220 #ifndef NDEBUG
00221 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
00222 "Inside the MatrixExpressionTree Constructor");
00223 #endif
00224 }
00225
00226
00227 MatrixExpressionTree::~MatrixExpressionTree()
00228 {
00229 #ifndef NDEBUG
00230 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
00231 "Inside the MatrixExpressionTree Destructor");
00232 #endif
00233 if( bDestroyNlNodes == true)
00234 {
00235 if(m_treeRoot != NULL) delete m_treeRoot;
00236 m_treeRoot = NULL;
00237 }
00238 }
00239
00240
00241 std::vector<ExprNode*> MatrixExpressionTree::getPrefixFromExpressionTree()
00242 {
00243 return m_treeRoot->getPrefixFromExpressionTree();
00244 }
00245
00246
00247 std::vector<ExprNode*> MatrixExpressionTree::getPostfixFromExpressionTree()
00248 {
00249 return m_treeRoot->getPostfixFromExpressionTree();
00250 }
00251
00252
00253 bool MatrixExpressionTree::IsEqual(MatrixExpressionTree *that)
00254 {
00255 #ifndef NDEBUG
00256 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_trace, "Start comparing in MatrixExpressionTree");
00257 #endif
00258 if (this == NULL)
00259 {
00260 if (that == NULL)
00261 return true;
00262 else
00263 {
00264 #ifndef NDEBUG
00265 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_trace, "First object is NULL, second is not");
00266 #endif
00267 return false;
00268 }
00269 }
00270 else
00271 {
00272 if (that == NULL)
00273 {
00274 #ifndef NDEBUG
00275 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_trace, "Second object is NULL, first is not");
00276 #endif
00277 return false;
00278 }
00279 else
00280 {
00281 if (!this->m_treeRoot->IsEqual(that->m_treeRoot))
00282 return false;
00283
00284 return this->OSExpressionTree::IsEqual(that);
00285 }
00286 }
00287 }
00288