//Alt. version of preservePath() void Graph::preservePath2(std::list< std::list<Node>::iterator >& pidPath, std::list<Node>::iterator pendNode) { std::list< std::list<Node>::iterator >::iterator pathIter = pidPath.begin(); while (*pathIter != pendNode) { std::list<Node>::iterator pn = *pathIter; if (pn->isOpen()) pn->setNodeType(Node::OPEN); else pn->setNodeType(Node::CLOSED); pathIter++; } if (pendNode->isOpen()) pendNode->setNodeType(Node::OPEN); else pendNode->setNodeType(Node::CLOSED); }
//Set nodes in path to OPEN void Graph::preservePath(std::list<Node>::iterator pbaseNode, std::list<Node>::iterator ptargetNode) { //make sure iterators are valid std::list<Node>::iterator pnull = std::list<Node>::iterator(NULL); if (pbaseNode == pnull || ptargetNode == pnull) { //std::cout << "\nNULL ITERATORS PASSED TO preservePath()\n"; return; } std::vector<short> baseWord = pbaseNode->getElemName(); std::vector<short> targetWord = ptargetNode->getElemName(); //target word must be longer than or equal length as base word if (targetWord.size() < baseWord.size()) { //std::cout << "\nPRESERVE PATH BAD RETURN1\n"; return; } //should never touch the identity; this is a special case which is handled in initial step if (baseWord[0] == 0 || targetWord[0] == 0) { //std::cout << "\nPRESERVE PATH BAD RETURN2\n"; return; } //if words are identical there's nothing to do if (baseWord == targetWord) { //std::cout << "\nPRESERVE PATH BAD RETURN3\n"; return; } //return; //At this point we know the nodes exist, their words have appropriate sizes, they're not the identity, //and they're not equal. But we could still have the problem that the base word is not contained //in the leftmost portion of the target word, so we check for that. bool baseContained = true; int i; for (i = 0; i < baseWord.size() - 1; i++) //up until the last letter, each exponent identical if (baseWord[i] != targetWord[i]) baseContained = false; if (baseWord[i] > targetWord[i]) //on the last letter, the exponent in base may be less or equal baseContained = false; if (baseContained == false) { std::cout << "\nPRESERVE PATH not contained:\n"; pbaseNode->printWord(); std::cout << "\n"; ptargetNode->printWord(); std::cout << "\n"; return; } //nodes have been checked std::list<Node>::iterator psetNode = ptargetNode; while (targetWord.size() != baseWord.size()) //stop at last exponent of baseWord { int i = targetWord.size() - 1; //always index of last exponent in targetWord int exp = targetWord[i]; //last exponent on targetWord while (exp > 0) { targetWord[i] = exp; psetNode = searchNodes(&targetWord); if (psetNode == std::list<Node>::iterator(NULL)) { std::cout << "\nAttempt to set node via null iterator in preservePath()\n"; return; } if (psetNode->getNodeType() != Node::TEMP && psetNode != ptargetNode) { //it's okay to hit a preserved node, this means the nodes before this one will also //be preserved return; } psetNode->setNodeType(Node::OPEN); exp--; } targetWord.pop_back(); //remove exponent just cleared } int d = targetWord.size() - 1; if (targetWord[d] == baseWord[d]) { if(pbaseNode->isOpen()) psetNode->setNodeType(Node::OPEN); else psetNode->setNodeType(Node::CLOSED); return; } int exp = targetWord[d]; //last exponent on targetWord while (exp > baseWord[d]) { targetWord[d] = exp; psetNode = searchNodes(&targetWord); if (psetNode == std::list<Node>::iterator(NULL)) { std::cout << "\nAttempt to set node via null iterator in preservePath()\n"; return; } if (psetNode->getNodeType() != Node::TEMP) { //again this is not a problem return; } psetNode->setNodeType(Node::OPEN); } if(pbaseNode->isOpen()) psetNode->setNodeType(Node::OPEN); else psetNode->setNodeType(Node::CLOSED); return; }