//Alt. version of reduceIdent() Identity Graph::reduceIdent2(std::list<Identity>::iterator pid, std::list< std::list<Node>::iterator >& pidPath, bool report) { std::list<Node>::iterator prightNode; prightNode = pid->getRight(); //use to trace back through pidPath std::list< std::list<Node>::iterator >::reverse_iterator pleftPath = pidPath.rbegin(); std::list< std::list<Node>::iterator >::reverse_iterator pleftPathPlus1 = pleftPath; pleftPathPlus1++; while (pleftPathPlus1 != pidPath.rend()) { //look for pleft in pleftPlus1's xout list if ((*pleftPathPlus1)->isInXout(*pleftPath)) { if (prightNode->isXinEmpty()) break; prightNode = prightNode->getXin(); } //look in pleftPlus1's yout list else if ((*pleftPathPlus1)->isInYout(*pleftPath)) { if (prightNode->isYinEmpty()) break; prightNode = prightNode->getYin(); } else //in this case the path is not valid { std::cout << "\nBAD PATH FOR IDENTITY:\n"; pid->printIdent(); Identity nullid; return nullid; } pleftPath++; pleftPathPlus1++; } //make and return reduced identity; Identity reducedId(*pleftPath, prightNode, pid->getBase()); if (report) { std::cout << "\nIdentity "; pid->printIdent(); std::cout << "\nreduced to "; reducedId.printIdent(); std::cout << "\n"; } return reducedId; }
//Reduce and return identity Identity Graph::reduceIdent(std::list<Identity>::iterator pid) { std::list<Node>::iterator pleftNode, prightNode, pbaseNode; pleftNode = pid->getLeft(); prightNode = pid->getRight(); pbaseNode = pid->getBase(); std::vector<short> leftWord = pleftNode->getElemName(); std::vector<short> baseWord = pbaseNode->getElemName(); bool letterSwitch = pleftNode->lastLetterIsX(); //x <-> true, y <-> false bool endPath = false; //detect if right reduction has reached an empty product while (leftWord.size() != baseWord.size()) //loop over exponents until last exp. of baseWord { while (leftWord[leftWord.size() - 1] > 0) { //check right path; if exists, move left and right back one product if (letterSwitch) //for letter x { if (!prightNode->isXinEmpty()) { prightNode = prightNode->getXin(); pleftNode = pleftNode->getXin(); /* ///Test: use search instead of pointers std::vector<short> searchWord = leftWord; searchWord[leftWord.size() - 1] = leftWord[leftWord.size() - 1] - 1; if (searchWord[leftWord.size() - 1] == 0) searchWord.pop_back(); pleftNode = searchNodes(&searchWord); ///Test */ } else { endPath = true; break; } } else //for letter y { if (!prightNode->isYinEmpty()) { prightNode = prightNode->getYin(); pleftNode = pleftNode->getYin(); /* ///Test: use search instead of pointers std::vector<short> searchWord = leftWord; searchWord[leftWord.size() - 1] = leftWord[leftWord.size() - 1] - 1; if (searchWord[leftWord.size() - 1] == 0) searchWord.pop_back(); pleftNode = searchNodes(&searchWord); ///Test */ } else { endPath = true; break; } } //decrement last exponent leftWord[leftWord.size() - 1] = leftWord[leftWord.size() - 1] - 1; } //either loop broken prematurely by end of path... if (endPath) break; //...or exponent is now 0, so move to next exponent leftWord.pop_back(); letterSwitch = !letterSwitch; } //if not at end of path, continue reducing if (!endPath) { while (baseWord[baseWord.size() - 1] != leftWord[leftWord.size() - 1]) { if (letterSwitch) { if (!prightNode->isXinEmpty()) { prightNode = prightNode->getXin(); pleftNode = pleftNode->getXin(); /* ///Test: use search instead of pointers std::vector<short> searchWord = leftWord; searchWord[leftWord.size() - 1] = leftWord[leftWord.size() - 1] - 1; if (searchWord[leftWord.size() - 1] == 0) searchWord.pop_back(); pleftNode = searchNodes(&searchWord); ///Test */ } else { endPath = true; break; } } else { if (!prightNode->isYinEmpty()) { prightNode = prightNode->getYin(); pleftNode = pleftNode->getYin(); /* ///Test: use search instead of pointers std::vector<short> searchWord = leftWord; searchWord[leftWord.size() - 1] = leftWord[leftWord.size() - 1] - 1; if (searchWord[leftWord.size() - 1] == 0) searchWord.pop_back(); pleftNode = searchNodes(&searchWord); ///Test */ } else { endPath = true; break; } } leftWord[leftWord.size() - 1] = leftWord[leftWord.size() - 1] - 1; } } //make and return reduced identity; Identity reducedId(pleftNode, prightNode, pbaseNode); /* std::cout << "\nIdentity "; pid->printIdent(); std::cout << "\nreduced to "; reducedId.printIdent(); std::cout << "\n"; */ return reducedId; }