00001
00016
00017 #include "OSExpressionTree.h"
00018 #include<vector>
00019
00020 using std::cout;
00021 using std::endl;
00022
00023 OSExpressionTree::OSExpressionTree():
00024 m_treeRoot( NULL),
00025 mapVarIdx( NULL),
00026 m_bIndexMapGenerated( false),
00027 bADMustReTape( false),
00028 bDestroyNlNodes( true)
00029 {
00030 }
00031
00032
00033 OSExpressionTree::~OSExpressionTree()
00034 {
00035 #ifdef DEBUG
00036 cout << "Inside the OSExpressionTree Destructor" << endl;
00037 #endif
00038 if( bDestroyNlNodes == true)
00039 {
00040 if(m_treeRoot != NULL) delete m_treeRoot;
00041 }
00042 if(mapVarIdx != NULL)
00043 {
00044 delete mapVarIdx;
00045 mapVarIdx = NULL;
00046 }
00047 }
00048
00049
00050 std::vector<OSnLNode*> OSExpressionTree::getPostfixFromExpressionTree()
00051 {
00052 return m_treeRoot->getPostfixFromExpressionTree();
00053 }
00054
00055
00056 std::vector<OSnLNode*> OSExpressionTree::getPrefixFromExpressionTree()
00057 {
00058 return m_treeRoot->getPrefixFromExpressionTree();
00059 }
00060
00061
00062
00063 double OSExpressionTree::calculateFunction( double *x, bool new_x)
00064 {
00065
00066 if( new_x == false)
00067 {
00068 return m_dTreeRootValue;
00069 }
00070 else
00071 {
00072 m_dTreeRootValue = m_treeRoot->calculateFunction( x);
00073 return m_dTreeRootValue;
00074 }
00075 }
00076
00077 std::map<int, int> *OSExpressionTree::getVariableIndiciesMap()
00078 {
00079 if( m_bIndexMapGenerated == true) return mapVarIdx;
00080 mapVarIdx = new std::map<int, int>();
00081 std::map<int, int>::iterator m_mPosVarIdx;
00082 m_treeRoot->getVariableIndexMap( mapVarIdx);
00083
00084
00085
00086
00087
00088
00089 m_bIndexMapGenerated = true;
00090 return mapVarIdx;
00091 }
00092
00093 bool OSExpressionTree::IsEqual(OSExpressionTree *that)
00094 {
00095 #ifdef DEBUG_ISEQUAL_ROUTINES
00096 cout << "Start comparing in OSExpressionTree" << endl;
00097 #endif
00098 if (this == NULL)
00099 {
00100 if (that == NULL)
00101 return true;
00102 else
00103 {
00104 #ifdef DEBUG_ISEQUAL_ROUTINES
00105 cout << "First object is NULL, second is not" << endl;
00106 #endif
00107 return false;
00108 }
00109 }
00110 else
00111 {
00112 if (that == NULL)
00113 {
00114 #ifdef DEBUG_ISEQUAL_ROUTINES
00115 cout << "Second object is NULL, first is not" << endl;
00116 #endif
00117 return false;
00118 }
00119 else
00120 {
00121 if (!this->m_treeRoot->IsEqual(that->m_treeRoot))
00122 return false;
00123
00124 return true;
00125 }
00126 }
00127 }
00128
00129