00001
00016 #include "OSOutput.h"
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 #ifndef NDEBUG
00036 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSExpressionTree, ENUM_OUTPUT_LEVEL_debug,
00037 "Inside the OSExpressionTree Destructor");
00038 #endif
00039 if( bDestroyNlNodes == true)
00040 {
00041 if(m_treeRoot != NULL) delete m_treeRoot;
00042 m_treeRoot = NULL;
00043 }
00044 if(mapVarIdx != NULL)
00045 {
00046 delete mapVarIdx;
00047 mapVarIdx = NULL;
00048 }
00049 }
00050
00051
00052 std::vector<OSnLNode*> OSExpressionTree::getPostfixFromExpressionTree()
00053 {
00054 return m_treeRoot->getPostfixFromExpressionTree();
00055 }
00056
00057
00058 std::vector<OSnLNode*> OSExpressionTree::getPrefixFromExpressionTree()
00059 {
00060 return m_treeRoot->getPrefixFromExpressionTree();
00061 }
00062
00063
00064
00065 double OSExpressionTree::calculateFunction( double *x, bool new_x)
00066 {
00067
00068 if( new_x == false)
00069 {
00070 return m_dTreeRootValue;
00071 }
00072 else
00073 {
00074 m_dTreeRootValue = m_treeRoot->calculateFunction( x);
00075 return m_dTreeRootValue;
00076 }
00077 }
00078
00079 std::map<int, int> *OSExpressionTree::getVariableIndiciesMap()
00080 {
00081 return getVariableIndicesMap();
00082 }
00083
00084 std::map<int, int> *OSExpressionTree::getVariableIndicesMap()
00085 {
00086 if( m_bIndexMapGenerated == true) return mapVarIdx;
00087 mapVarIdx = new std::map<int, int>();
00088 std::map<int, int>::iterator m_mPosVarIdx;
00089 m_treeRoot->getVariableIndexMap( mapVarIdx);
00090
00091
00092
00093
00094
00095
00096 m_bIndexMapGenerated = true;
00097 return mapVarIdx;
00098 }
00099
00100 bool OSExpressionTree::IsEqual(OSExpressionTree *that)
00101 {
00102 #ifndef NDEBUG
00103 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSExpressionTree, ENUM_OUTPUT_LEVEL_trace, "Start comparing in OSExpressionTree");
00104 #endif
00105 if (this == NULL)
00106 {
00107 if (that == NULL)
00108 return true;
00109 else
00110 {
00111 #ifndef NDEBUG
00112 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSExpressionTree, ENUM_OUTPUT_LEVEL_trace, "First object is NULL, second is not");
00113 #endif
00114 return false;
00115 }
00116 }
00117 else
00118 {
00119 if (that == NULL)
00120 {
00121 #ifndef NDEBUG
00122 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSExpressionTree, ENUM_OUTPUT_LEVEL_trace, "Second object is NULL, first is not");
00123 #endif
00124 return false;
00125 }
00126 else
00127 {
00128 if (!this->m_treeRoot->IsEqual(that->m_treeRoot))
00129 return false;
00130
00131 return true;
00132 }
00133 }
00134 }
00135
00136