int ASTFunctionBase::insertChild(unsigned int n, ASTBase* newChild) { if (newChild == NULL) return LIBSBML_INVALID_OBJECT; int inserted = LIBSBML_INDEX_EXCEEDS_SIZE; unsigned int size = ASTFunctionBase::getNumChildren(); if (n == 0) { ASTFunctionBase::prependChild(newChild); inserted = LIBSBML_OPERATION_SUCCESS; } else if (n <= size) { /* starting at the end take each child in the list and prepend it * then remove it from the end * at the insertion point prepend the newChild * eg list: a, b, c * inserting d at position 2 * list goes: c, a, b : d, c, a, b : b, d, c, a : a, b, d, c */ bool childIsNode = dynamic_cast<ASTNode*>(newChild); if (childIsNode) { mChildren.insert(mChildren.begin() + n, newChild); newChild->setIsChildFlag(true); } else { ASTFunction* childAsFunction = dynamic_cast<ASTFunction*>(newChild); ASTNumber* childAsNumber = dynamic_cast<ASTNumber*>(newChild); // wrap it ASTNode* tmp = childAsFunction != NULL ? new ASTNode(childAsFunction) : new ASTNode(childAsNumber); mChildren.insert(mChildren.begin()+n, tmp); tmp->setIsChildFlag(true); } if (ASTFunctionBase::getNumChildren() == size + 1) { inserted = LIBSBML_OPERATION_SUCCESS; } } return inserted; }
int ASTFunctionBase::addChild(ASTBase * child, bool /*inRead = false*/) { if (child == NULL) { return LIBSBML_INVALID_OBJECT; } unsigned int numBefore = ASTFunctionBase::getNumChildren(); bool childIsNode = dynamic_cast<ASTNode*>(child); if (childIsNode) { mChildren.push_back(child); child->setIsChildFlag(true); } else { ASTFunction* childAsFunction = dynamic_cast<ASTFunction*>(child); ASTNumber* childAsNumber = dynamic_cast<ASTNumber*>(child); // wrap it ASTNode* tmp = childAsFunction != NULL ? new ASTNode(childAsFunction) : new ASTNode(childAsNumber); mChildren.push_back(tmp); tmp->setIsChildFlag(true); } if (ASTFunctionBase::getNumChildren() == numBefore + 1) { return LIBSBML_OPERATION_SUCCESS; } else { return LIBSBML_OPERATION_FAILED; } }
int ASTFunctionBase::prependChild(ASTBase* child) { if (child == NULL) return LIBSBML_INVALID_OBJECT; unsigned int numBefore = ASTFunctionBase::getNumChildren(); child->setIsChildFlag(true); bool childIsNode = dynamic_cast<ASTNode*>(child); if (childIsNode) { mChildren.insert(mChildren.begin(),child); child->setIsChildFlag(true); } else { ASTFunction* childAsFunction = dynamic_cast<ASTFunction*>(child); ASTNumber* childAsNumber = dynamic_cast<ASTNumber*>(child); // wrap it ASTNode* tmp = childAsFunction != NULL ? new ASTNode(childAsFunction) : new ASTNode(childAsNumber); mChildren.insert(mChildren.begin(), tmp); tmp->setIsChildFlag(true); } if (ASTFunctionBase::getNumChildren() == numBefore + 1) { return LIBSBML_OPERATION_SUCCESS; } else { return LIBSBML_OPERATION_FAILED; } }