int Rule::removeItem(Rule* &TRule,const int SurfN) /** Given an item as a surface name, remove the surface from the Rule tree. - If the found leaf is on a @param TRule :: Top rule to down search @param SurfN :: Surface key number to remove @return Number of instances removed */ { int cnt(0); Rule* Ptr = TRule->findKey(SurfN); while (Ptr) { Rule* LevelOne = Ptr->getParent(); // Must work Rule* LevelTwo = (LevelOne) ? LevelOne->getParent() : 0; if (LevelTwo) /// Not the top level { // Decide which of the pairs is to be copied Rule* PObj = (LevelOne->leaf(0) != Ptr) ? LevelOne->leaf(0) : LevelOne->leaf(1); // LevelOne->setLeaves(0, 0); // Delete from Ptr, and copy const int side = (LevelTwo->leaf(0) != LevelOne) ? 1 : 0; LevelTwo->setLeaf(PObj, side); } else if (LevelOne) // LevelOne is the top rule { // Decide which of the pairs is to be copied Rule* PObj = (LevelOne->leaf(0) != Ptr) ? LevelOne->leaf(0) : LevelOne->leaf(1); PObj->setParent(0); /// New Top rule TRule = PObj; LevelOne->setLeaves(0, 0); // Delete from Ptr, and copy // Note we now need to delete this delete LevelOne; } else // Basic surf object { SurfPoint* SX = dynamic_cast<SurfPoint*> (Ptr); SX->setKeyN(0); SX->setKey(0); return cnt + 1; } delete Ptr; //delete LevelOne; // Shouldn't delete now we're deleting in setLeaf. Ptr = TRule->findKey(SurfN); cnt++; } return cnt; }
int Rule::substituteSurf(const int SurfN, const int newSurfN, Surface *SPtr) /** Substitues a surface item if within a rule @param SurfN :: Number number to change @param newSurfN :: New surface number (if -ve then the key is reversed) @param SPtr :: New surface Pointer @return number of substitutions */ { int cnt(0); SurfPoint *Ptr = dynamic_cast<SurfPoint *>(findKey(SurfN)); while (Ptr) { Ptr->setKeyN(Ptr->getSign() * newSurfN); Ptr->setKey(SPtr->clone()); // Clone to ensure uniqueness cnt++; // get next key Ptr = dynamic_cast<SurfPoint *>(findKey(SurfN)); } delete SPtr; // Delete incoming pointer return cnt; }