void XMLElementSommet::update() { //On réaffecte l'ID : setAttribute(QString("id"),sommet->getNom()); //De même avec la forme : QDomElement ancienDomForme=firstChildElement("Forme"); QDomElement newDomForme=createElement(QString("Forme")); newDomForme.setNodeValue(sommet->getForme()); if(ancienDomForme.isNull()) domSommet->appendChild(newDomForme); else replaceChild(newDomForme, ancienDomForme); //Si c'est coloré, on s'en occupe : if(colore){ SommetColore* sommetColore=(SommetColore*) sommet; QDomElement ancienDomCouleur=firstChildElement("Couleur"); QDomElement newDomCouleur=createElement(QString("Couleur")); newDomForme.setNodeValue(QString(sommetColore->getCouleur())); if(ancienDomCouleur.isNull()) domSommet->appendChild(newDomCouleur); else replaceChild(newDomCouleur, ancienDomCouleur); } }
void OTMLNode::addChild(const OTMLNodePtr& newChild) { // replace is needed when the tag is marked as unique if(newChild->hasTag()) { for(const OTMLNodePtr& node : m_children) { if(node->tag() == newChild->tag() && (node->isUnique() || newChild->isUnique())) { newChild->setUnique(true); if(node->hasChildren() && newChild->hasChildren()) { OTMLNodePtr tmpNode = node->clone(); tmpNode->merge(newChild); newChild->copy(tmpNode); } replaceChild(node, newChild); // remove any other child with the same tag auto it = m_children.begin(); while(it != m_children.end()) { OTMLNodePtr node = (*it); if(node != newChild && node->tag() == newChild->tag()) { it = m_children.erase(it); } else ++it; } return; } } } m_children.push_back(newChild); }
void YGNode::cloneChildrenIfNeeded() { // YGNodeRemoveChild in yoga.cpp has a forked variant of this algorithm // optimized for deletions. const uint32_t childCount = static_cast<uint32_t>(children_.size()); if (childCount == 0) { // This is an empty set. Nothing to clone. return; } const YGNodeRef firstChild = children_.front(); if (firstChild->getParent() == this) { // If the first child has this node as its parent, we assume that it is // already unique. We can do this because if we have it has a child, that // means that its parent was at some point cloned which made that subtree // immutable. We also assume that all its sibling are cloned as well. return; } const YGNodeClonedFunc cloneNodeCallback = config_->cloneNodeCallback; for (uint32_t i = 0; i < childCount; ++i) { const YGNodeRef oldChild = children_[i]; const YGNodeRef newChild = YGNodeClone(oldChild); replaceChild(newChild, i); newChild->setParent(this); if (cloneNodeCallback) { cloneNodeCallback(oldChild, newChild, this, i); } } }
OOModel::Expression* CallDescriptor::create(const QList<OOModel::Expression*>& operands) { Q_ASSERT(operands.size() == 2); auto varName = dynamic_cast<OOModel::VariableAccess*>( operands.first()); Q_ASSERT(varName); OOModel::MethodCallExpression* opr = new OOModel::MethodCallExpression(); opr->ref()->set(varName->ref()->path()); OOModel::Expression* prefix = varName->prefix(); varName->replaceChild(prefix, new OOModel::EmptyExpression()); SAFE_DELETE(varName); opr->setPrefix(prefix); if (auto comma = dynamic_cast<OOModel::CommaExpression*>(operands.last())) { for(auto arg : comma->allSubOperands(true)) opr->arguments()->append(arg); SAFE_DELETE(comma); } else if (!dynamic_cast<OOModel::EmptyExpression*>(operands.last()) ) opr->arguments()->append(operands.last()); return opr; }
void RotateUp(AVLNode* cur) { AVLNode* k = cur->parent; if (k == NULL) { return; } AVLNode* parent = k->parent; if (k->left == cur) { k->left = cur->right; if (k->left) { k->left->parent = k; } cur->right = k; k->parent = cur; } else { k->right = cur->left; if (k->right) { k->right->parent = k; } cur->left = k; k->parent = cur; } replaceChild(parent, k, cur); fixHeight(k); fixHeight(cur); fixHeight(parent); fixSize(k); fixSize(cur); fixSize(parent); }
OOModel::Expression* CompoundObjectDescriptor::create(const QList<OOModel::Expression*>& operands) { Q_ASSERT(operands.size() == 1); auto ilit = dynamic_cast<OOModel::IntegerLiteral*> (operands.first()); Q_ASSERT(ilit); auto e = storedExpressions().value(ilit->value()); SAFE_DELETE(ilit); Q_ASSERT(e); if (auto model = e->model()) { auto beingModified = model->isBeingModified(); auto oldModificationTarget = beingModified ? nullptr : model->modificationTarget(); auto oldParent = e->parent(); Q_ASSERT(e!=oldModificationTarget && !e->isAncestorOf(oldModificationTarget)); if (!beingModified) oldParent->beginModification("extract expression"); else model->changeModificationTarget(oldParent); auto replaceSuccessfull = oldParent->replaceChild(e, new OOModel::EmptyExpression()); Q_ASSERT(replaceSuccessfull); if (!beingModified) oldParent->endModification(); else model->changeModificationTarget(oldModificationTarget); } else { auto replaceSuccessfull = e->parent()->replaceChild(e, new OOModel::EmptyExpression()); Q_ASSERT(replaceSuccessfull); } return e; }
void BracketPairLayout::collapseSiblingsAndMoveCursor(ExpressionLayoutCursor * cursor) { // If the operand layout is not an HorizontalLayout, replace it with one. if (!operandLayout()->isHorizontal()) { ExpressionLayout * previousOperand = operandLayout(); HorizontalLayout * horizontalOperandLayout = new HorizontalLayout(previousOperand, false); replaceChild(previousOperand, horizontalOperandLayout, false); } ExpressionLayout::collapseOnDirection(HorizontalDirection::Right, 0); cursor->setPointedExpressionLayout(operandLayout()); cursor->setPosition(ExpressionLayoutCursor::Position::Left); }
static IHqlExpression * optimizedReplaceSelector(IHqlExpression * expr, IHqlExpression * oldDataset, IHqlExpression * newDataset) { switch (expr->getOperator()) { case no_constant: case no_attr: return LINK(expr); case no_select: { IHqlExpression * lhs = expr->queryChild(0); IHqlExpression * field = expr->queryChild(1); OwnedHqlExpr newLhs; if (expr->hasAttribute(newAtom)) { newLhs.setown(optimizedReplaceSelector(lhs, oldDataset, newDataset)); } else { if (lhs == oldDataset) { if (newDataset->getOperator() == no_newrow) return createNewSelectExpr(LINK(newDataset->queryChild(0)), LINK(field)); if (newDataset->getOperator() == no_activerow) newDataset = newDataset->queryChild(0); return createSelectExpr(LINK(newDataset->queryNormalizedSelector()), LINK(field)); } else newLhs.setown(optimizedReplaceSelector(lhs, oldDataset, newDataset)); } if (newLhs) return replaceChild(expr, 0, newLhs); return NULL; } case no_implicitcast: case no_cast: { IHqlExpression * newUncast = optimizedReplaceSelector(expr->queryChild(0), oldDataset, newDataset); if (!newUncast) return NULL; OwnedHqlExpr ret = createValue(expr->getOperator(), expr->getType(), newUncast); return expr->cloneAllAnnotations(ret); } case no_hash: case no_hash32: case no_hash64: case no_sortlist: case no_concat: case no_trim: case no_negate: case no_eq: case no_ne: case no_sizeof: case no_attr_expr: case no_add: { HqlExprArray args; ForEachChild(i, expr) { IHqlExpression * cur = expr->queryChild(i); IHqlExpression * newCur = optimizedReplaceSelector(cur, oldDataset, newDataset); if (!newCur) return NULL; args.append(*newCur); } return expr->clone(args); } }
int main(void) { element *html = readDocument(); // createElement element *t1 = createElement("t1", "Text node"); assert("createElement - empty pointers - parentNode", t1->parentNode == NULL); assert("createElement - empty pointers - nextSibling", t1->nextSibling == NULL); assert("createElement - empty pointers - previousSibling", t1->previousSibling == NULL); assert("createElement - empty pointers - firstChild", t1->firstChild == NULL); assert("createElement - empty pointers - lastChild", t1->lastChild == NULL); // appendChild appendChild(html->lastChild, t1); assert("Append child test - parent pointer", t1->parentNode == html->lastChild); assert("Append only child test - previousSibling pointer", t1->previousSibling == NULL); assert("Append only child test - nextSibling pointer", t1->nextSibling == NULL); element *t2 = createElement("t2", NULL); appendChild(html->lastChild, t2); assert("Append second child test - previousSibling pointer to the first child", t2->previousSibling == html->lastChild->firstChild); assert("Append second child test - nextSibling pointer to NULL", t2->nextSibling == NULL); element *t3 = createElement("t3", NULL); appendChild(html->lastChild, t3); assert("Append third child test - second child nextSibling pointer equals to the pointer to the last child", t2->nextSibling == html->lastChild->lastChild); // replaceChild element *r2 = createElement("r2", NULL); replaceChild(html->lastChild, r2, html->lastChild->firstChild->nextSibling); assert("Replace child in the middle - parent", r2->parentNode == html->lastChild); assert("Replace child in the middle - prevSibling", r2->previousSibling == html->lastChild->firstChild); assert("Replace child in the middle - nextSibling", r2->nextSibling == html->lastChild->lastChild); element *r3 = createElement("r3", NULL); replaceChild(html->lastChild, r3, html->lastChild->lastChild); assert("Replace last child - prevSibling", (r3->previousSibling == html->lastChild->firstChild->nextSibling) && (r3->previousSibling == r2)); assert("Replace last child - nextSibling", r3->nextSibling == NULL); element *r1 = createElement("r1", NULL); replaceChild(html->lastChild, r1, html->lastChild->firstChild); assert("Replace first child - prevSibling", r1->previousSibling == NULL); assert("Replace first child - nextSibling", r1->nextSibling == r2); // removeChild removeChild(html->lastChild, r1); assert("Remove child - first child removed", html->lastChild->firstChild == r2); removeChild(html->lastChild, r3); assert("Remove child - last (not least) child is removed", html->lastChild->lastChild == r2); assert("Remove child - last (not least) child is removed - siblings check for the remaining child", r2->previousSibling == NULL && r2->nextSibling == NULL); removeChild(r2->parentNode, r2); assert("Remove child - body is empty", html->lastChild->firstChild == NULL && html->lastChild->lastChild == NULL); appendChild(html->lastChild, r1); appendChild(html->lastChild, r2); appendChild(html->lastChild, r3); removeChild(r2->parentNode, r2); assert("Remove the middle child - relation between first and last", html->lastChild->firstChild->nextSibling == html->lastChild->lastChild && html->lastChild->firstChild == html->lastChild->lastChild->previousSibling); removeChild(html->lastChild, r1); removeChild(html->lastChild, r3); // insertBefore appendChild(html->lastChild, r3); insertBefore(html->lastChild, r2, r3); assert("insertBefore - new firstChild", html->lastChild->firstChild == r2); assert("insertBefore - lastChild kept", html->lastChild->lastChild == r3); assert("insertBefore - parentNode set", html->lastChild == r2->parentNode); insertBefore(html->lastChild, r1, r3); assert("insertBefore - siblings", r1->previousSibling == r2 && r1->nextSibling == r3); assert("insertBefore - previousSibling->nextSibling == added element", r2->nextSibling == r1); removeChild(html->lastChild, r1); removeChild(html->lastChild, r2); removeChild(html->lastChild, r3); // cloneNode appendChild(r2, t1); appendChild(t1, t2); appendChild(r2, t3); element *c0 = cloneNode(t2, false); insertBefore(r2, c0, t3); element *c1 = cloneNode(r2, true); element *c2 = cloneNode(r2, false); appendChild(html->lastChild, c1); appendChild(html->lastChild, c2); element *c3 = cloneNode(r3, true); // r3 insertBefore(html->lastChild, c3, c1); // Now it should be in order: r3, r2 deep, r2 assert("Clone - first r2 is deep", html->lastChild->firstChild->nextSibling->firstChild->firstChild != NULL // html body r2 t1 t2 && html->lastChild->firstChild->nextSibling->lastChild != NULL); // html body r2 t3 deleteElement(r1); deleteElement(r2); deleteElement(r3); deleteElement(html); return 0; }