/////////////////////////////////////////////////////////////////////// // Math Type Library // $Id: nodes.tcc,v 1.9 2002/07/02 00:56:12 cparpart Exp $ // (This file contains the expression tree specific template members) // // Copyright (c) 2002 by Christian Parpart // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Library General Public // License as published by the Free Software Foundation; either // version 2 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Library General Public License for more details. // // You should have received a copy of the GNU Library General Public License // along with this library; see the file COPYING.LIB. If not, write to // the Free Software Foundation, Inc., 59 Temple Place - Suite 330, // Boston, MA 02111-1307, USA. /////////////////////////////////////////////////////////////////////// #ifndef libmath_nodes_h #error You may not include math++/nodes.tcc directly; include math++/nodes.h instead. #endif #include #include #include namespace math { // TNode template TNode::TNode(TNodeType ANodeType, short APriority, TNode *AParent) : FNodeType(ANodeType), FPriority(APriority), FParent(AParent) { } template TNode::TNode(const TNode& ANode) : FNodeType(ANode.FNodeType), FPriority(ANode.FPriority), FParent(ANode.FParent) { } template void TNode::parent(TNode *AParent) { FParent = AParent; } template TNode::~TNode() { } template typename TNode::TNodeType TNode::nodeType() const { return FNodeType; } template short TNode::priority() const { return FPriority; } template TNode *TNode::parent() const { return const_cast *>(FParent); } template TNode *TNode::left() const { // the node doesn't support children by default return 0; } template TNode *TNode::right() const { // the node doesn't support children by default return 0; } template bool operator==(const TNode& a, const TNode& b) { return a.equals(&b); } template bool operator!=(const TNode& a, const TNode& b) { return !(a == b); } // TNumberNode template TNumberNode::TNumberNode(const T& ANumber) : TNode(TNode::NUMBER_NODE, 0), FNumber(ANumber) { } template T TNumberNode::number() const { return FNumber; } template void TNumberNode::accept(TNodeVisitor& v) { v.visit(this); } template TNumberNode *TNumberNode::clone() const { return new TNumberNode(FNumber); } template bool TNumberNode::equals(const TNode *ANode) const { return this && ANode && ANode->nodeType() == TNode::NUMBER_NODE && FNumber == static_cast *>(ANode)->FNumber; } // TSymbolNode template TSymbolNode::TSymbolNode(const std::string& ASymbol) : TNode(TNode::SYMBOL_NODE, 0), FSymbol(ASymbol) { } template std::string TSymbolNode::symbol() const { return FSymbol; } template void TSymbolNode::accept(TNodeVisitor& v) { v.visit(this); } template TSymbolNode *TSymbolNode::clone() const { return new TSymbolNode(FSymbol); } template bool TSymbolNode::equals(const TNode *ANode) const { return this && ANode && ANode->nodeType() == TNode::SYMBOL_NODE && FSymbol == static_cast *>(ANode)->FSymbol; } // TParamNode template TParamNode::TParamNode() : TNode(TNode::PARAM_NODE, 0) { } template void TParamNode::accept(TNodeVisitor& v) { v.visit(this); } template TParamNode *TParamNode::clone() const { return new TParamNode(); } template bool TParamNode::equals(const TNode *ANode) const { return this && ANode && ANode->nodeType() == TNode::PARAM_NODE; } // TUnaryNodeOp template TUnaryNodeOp::TUnaryNodeOp(typename TUnaryNodeOp::TNodeType AType, short APrio, TNode *ANode) : TNode(AType, APrio), FNode(ANode) { FNode->parent(this); } template TNode *TUnaryNodeOp::node() const { return const_cast *>(FNode.get()); } template TNode *TUnaryNodeOp::right() const { return const_cast *>(FNode.get()); } template bool TUnaryNodeOp::equals(const TNode *ANode) const { // this method does not make use of the left() for performance reasons return this && ANode && this->nodeType() == ANode->nodeType() && FNode->equals(static_cast *>(ANode)->FNode.get()); } // TBinaryNodeOp template TBinaryNodeOp::TBinaryNodeOp(typename TBinaryNodeOp::TNodeType AType, short APrio, TNode *ALeft, TNode *ARight) : TNode(AType, APrio), FLeft(ALeft), FRight(ARight) { FLeft->parent(this); FRight->parent(this); } template TNode *TBinaryNodeOp::left() const { return const_cast *>(FLeft.get()); } template TNode *TBinaryNodeOp::right() const { return const_cast *>(FRight.get()); } template bool TBinaryNodeOp::equals(const TNode *ANode) const { // this method does not make use of the left() and right() methods // for performance reasons return this && ANode && this->nodeType() == ANode->nodeType() && FLeft->equals(static_cast *>(ANode)->FLeft.get()) && FRight->equals(static_cast *>(ANode)->FRight.get()); } // TPlusNode template TPlusNode::TPlusNode(TNode *ALeft, TNode *ARight) : TBinaryNodeOp(TNode::PLUS_NODE, -5, ALeft, ARight) { } template void TPlusNode::accept(TNodeVisitor& v) { v.visit(this); } template TPlusNode *TPlusNode::clone() const { return new TPlusNode(this->left()->clone(), this->right()->clone()); } // TNegNode template TNegNode::TNegNode(TNode *ANode) : TUnaryNodeOp(TNode::NEG_NODE, -5, ANode) { } template void TNegNode::accept(TNodeVisitor& v) { v.visit(this); } template TNegNode *TNegNode::clone() const { return new TNegNode(this->node()->clone()); } // TMulNode template TMulNode::TMulNode(TNode *ALeft, TNode *ARight) : TBinaryNodeOp(TNode::MUL_NODE, -3, ALeft, ARight) { } template void TMulNode::accept(TNodeVisitor& v) { v.visit(this); } template TMulNode *TMulNode::clone() const { return new TMulNode(this->left()->clone(), this->right()->clone()); } // TDivNode template TDivNode::TDivNode(TNode *ALeft, TNode *ARight) : TBinaryNodeOp(TNode::DIV_NODE, -3, ALeft, ARight) { } template void TDivNode::accept(TNodeVisitor& v) { v.visit(this); } template TDivNode *TDivNode::clone() const { return new TDivNode(this->left()->clone(), this->right()->clone()); } // TPowNode template TPowNode::TPowNode(TNode *ALeft, TNode *ARight) : TBinaryNodeOp(TNode::POW_NODE, -1, ALeft, ARight) { } template void TPowNode::accept(TNodeVisitor& v) { v.visit(this); } template TPowNode *TPowNode::clone() const { return new TPowNode(this->left()->clone(), this->right()->clone()); } // TSqrtNode template TSqrtNode::TSqrtNode(TNode *ANode) : TUnaryNodeOp(TNode::SQRT_NODE, -1, ANode) { } template void TSqrtNode::accept(TNodeVisitor& v) { v.visit(this); } template TSqrtNode *TSqrtNode::clone() const { return new TSqrtNode(this->node()->clone()); } // TSinNode template TSinNode::TSinNode(TNode *ANode) : TUnaryNodeOp(TNode::SIN_NODE, -1, ANode) { } template void TSinNode::accept(TNodeVisitor& v) { v.visit(this); } template TSinNode *TSinNode::clone() const { return new TSinNode(this->node()->clone()); } // TCosNode template TCosNode::TCosNode(TNode *ANode) : TUnaryNodeOp(TNode::COS_NODE, -1, ANode) { } template void TCosNode::accept(TNodeVisitor& v) { v.visit(this); } template TCosNode *TCosNode::clone() const { return new TCosNode(this->node()->clone()); } // TTanNode template TTanNode::TTanNode(TNode *ANode) : TUnaryNodeOp(TNode::TAN_NODE, -1, ANode) { } template void TTanNode::accept(TNodeVisitor& v) { v.visit(this); } template TTanNode *TTanNode::clone() const { return new TTanNode(this->node()->clone()); } // TCoTanNode // TArcSinNode // TArcCosNode // TArcTanNode // TArcCoTanNode // TLnNode template TLnNode::TLnNode(TNode *ANode) : TUnaryNodeOp(TNode::LN_NODE, -1, ANode) { } template void TLnNode::accept(TNodeVisitor& v) { v.visit(this); } template TLnNode *TLnNode::clone() const { return new TLnNode(this->node()->clone()); } // TFuncNode template TFuncNode::TFuncNode(const std::string& AName, TNode *AParam) : TUnaryNodeOp(TNode::FUNC_NODE, -1, AParam), FName(AName) { } template std::string TFuncNode::name() const { return FName; } template void TFuncNode::accept(TNodeVisitor& v) { v.visit(this); } template TFuncNode *TFuncNode::clone() const { return new TFuncNode(FName, this->node()->clone()); } // TIfNode template TIfNode::TIfNode(TNode *ACondNode, TNode *AThenNode, TNode *AElseNode) : TBinaryNodeOp(TNode::IF_NODE, -1, AThenNode, AElseNode), FCondition(ACondNode) { } template TNode *TIfNode::condition() const { return FCondition.get(); } template TNode *TIfNode::trueExpr() const { return this->left(); } template TNode *TIfNode::falseExpr() const { return this->right(); } template void TIfNode::accept(TNodeVisitor& v) { v.visit(this); } template TIfNode *TIfNode::clone() const { return new TIfNode(FCondition->clone(), this->left()->clone(), this->right()->clone()); } // TEquNode template TEquNode::TEquNode(TNode *ALeft, TNode *ARight) : TBinaryNodeOp(TNode::EQU_NODE, -10, ALeft, ARight) { } template void TEquNode::accept(TNodeVisitor& v) { v.visit(this); } template TEquNode *TEquNode::clone() const { return new TEquNode(this->left()->clone(), this->right()->clone()); } // TUnEquNode template TUnEquNode::TUnEquNode(TNode *ALeft, TNode *ARight) : TBinaryNodeOp(TNode::UNEQU_NODE, -10, ALeft, ARight) { } template void TUnEquNode::accept(TNodeVisitor& v) { v.visit(this); } template TUnEquNode *TUnEquNode::clone() const { return new TUnEquNode(this->left()->clone(), this->right()->clone()); } // TGreaterNode template TGreaterNode::TGreaterNode(TNode *ALeft, TNode *ARight) : TBinaryNodeOp(TNode::GREATER_NODE, -10, ALeft, ARight) { } template void TGreaterNode::accept(TNodeVisitor& v) { v.visit(this); } template TGreaterNode *TGreaterNode::clone() const { return new TGreaterNode(this->left()->clone(), this->right()->clone()); } // TLessNode template TLessNode::TLessNode(TNode *ALeft, TNode *ARight) : TBinaryNodeOp(TNode::LESS_NODE, -10, ALeft, ARight) { } template void TLessNode::accept(TNodeVisitor& v) { v.visit(this); } template TLessNode *TLessNode::clone() const { return new TLessNode(this->left()->clone(), this->right()->clone()); } // TGreaterEquNode template TGreaterEquNode::TGreaterEquNode(TNode *ALeft, TNode *ARight) : TBinaryNodeOp(TNode::GREATER_EQU_NODE, -10, ALeft, ARight) { } template void TGreaterEquNode::accept(TNodeVisitor& v) { v.visit(this); } template TGreaterEquNode *TGreaterEquNode::clone() const { return new TGreaterEquNode(this->left()->clone(), this->right()->clone()); } // TLessEquNode template TLessEquNode::TLessEquNode(TNode *ALeft, TNode *ARight) : TBinaryNodeOp(TNode::LESS_EQU_NODE, -10, ALeft, ARight) { } template void TLessEquNode::accept(TNodeVisitor& v) { v.visit(this); } template TLessEquNode *TLessEquNode::clone() const { return new TLessEquNode(this->left()->clone(), this->right()->clone()); } } // namespace math