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 m_treeRoot = NULL;
00042 }
00043 if(mapVarIdx != NULL)
00044 {
00045 delete mapVarIdx;
00046 mapVarIdx = NULL;
00047 }
00048 }
00049
00050
00051 std::vector<OSnLNode*> OSExpressionTree::getPostfixFromExpressionTree()
00052 {
00053 return m_treeRoot->getPostfixFromExpressionTree();
00054 }
00055
00056
00057 std::vector<OSnLNode*> OSExpressionTree::getPrefixFromExpressionTree()
00058 {
00059 return m_treeRoot->getPrefixFromExpressionTree();
00060 }
00061
00062
00063
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
00078 std::map<int, int> *OSExpressionTree::getVariableIndiciesMap()
00079 {
00080 return getVariableIndicesMap();
00081 }
00082
00083 std::map<int, int> *OSExpressionTree::getVariableIndicesMap()
00084 {
00085 if( m_bIndexMapGenerated == true) return mapVarIdx;
00086 mapVarIdx = new std::map<int, int>();
00087 std::map<int, int>::iterator m_mPosVarIdx;
00088 m_treeRoot->getVariableIndexMap( mapVarIdx);
00089
00090
00091
00092
00093
00094
00095 m_bIndexMapGenerated = true;
00096 return mapVarIdx;
00097 }
00098
00099 bool OSExpressionTree::IsEqual(OSExpressionTree *that)
00100 {
00101 #ifdef DEBUG_ISEQUAL_ROUTINES
00102 cout << "Start comparing in OSExpressionTree" << endl;
00103 #endif
00104 if (this == NULL)
00105 {
00106 if (that == NULL)
00107 return true;
00108 else
00109 {
00110 #ifdef DEBUG_ISEQUAL_ROUTINES
00111 cout << "First object is NULL, second is not" << endl;
00112 #endif
00113 return false;
00114 }
00115 }
00116 else
00117 {
00118 if (that == NULL)
00119 {
00120 #ifdef DEBUG_ISEQUAL_ROUTINES
00121 cout << "Second object is NULL, first is not" << endl;
00122 #endif
00123 return false;
00124 }
00125 else
00126 {
00127 if (!this->m_treeRoot->IsEqual(that->m_treeRoot))
00128 return false;
00129
00130 return true;
00131 }
00132 }
00133 }
00134
00135