polynomial operator -(const polynomial &p1) { polynomial negated; Iterator<polyterm> P = p1.Begin(); while(P.GetNode() != NULL) { Node<polyterm> negTerm = -(*P.GetNode()); negated.Append(negTerm.Copy()); P.Next(); } return negated; }
//================================================================================= // This would typically be used during an upgrade of a tree. Node::CopyValueError Node::CopyValueToTree( Node* root, Context& context ) const { List nodeList; if( !root->Find( nodeList, name, true ) ) return CVE_NOT_FOUND; if( nodeList.size() > 1 ) { context.IssueError( "A value cannot be copied to a tree where its associated name cannot be uniquely identified." ); return CVE_NOT_UNIQUE; } Node* node = *nodeList.begin(); CopyParameters copyParameters( COPY_NODE_AND_LEAVE_CHILDREN_UNTOUCHED, false, false ); if( !node->Copy( this, context, copyParameters ) ) return CVE_COPY_ERROR; return CVE_NO_ERROR; }
//================================================================================= Node* Node::Clone( Context& context, const CopyParameters& copyParameters ) const { bool success = false; Node* node = 0; context.ClearAllErrors(); try { // Grab the registered creator for this node's type. Context::NodeCreatorFunc nodeCreatorFunc = context.LookupNodeCreator( GetType() ); if( !nodeCreatorFunc ) throw new Error( "Failed to clone node \"%s\" of type \"%s\", because its creator function is not found in the creation registry.", name.c_str(), GetType().c_str() ); // Create a node of our type. node = ( *nodeCreatorFunc )(); if( !node ) throw new Error( "Failed to clone node \"%s\" of type \"%s\", because its creator function failed to allocate a new node of that type.", name.c_str(), GetType().c_str() ); // Populate the newly created node with our contents. if( !node->Copy( this, context, copyParameters ) ) throw new Error( "Failed to clone node \"%s\" of type \"%s\", because a failure occured while trying to copy the source node's contents into the destination node.", name.c_str(), GetType().c_str() ); success = true; } catch( Error* error ) { context.IssueError( error ); } if( !success ) { // Don't return a partial result on failure. if( node ) { delete node; node = 0; } } return node; }
polynomial& polynomial::operator +=(const Node<polyterm> &a) { //cout << "+= " << a << endl; Iterator<polyterm> P = this->Search(a.Copy()); if(P.GetNode() != NULL) { Node<polyterm>* oldTerm = this->Remove(P); //cout << "oldTerm " << *oldTerm << endl; Node<polyterm> newTerm = *oldTerm + a; //cout << "newTerm " << newTerm << endl; this->InsertSorted(newTerm.Copy()); //cout << "newPoly = " << *this << endl; } else { this->InsertSorted(a.Copy()); } return *this; }