#include "GraphStructuredStack.h" GraphStructuredStack::GraphStructuredStack() { // } GraphStructuredStack::~GraphStructuredStack() { // } NodeTree* GraphStructuredStack::newNode(int stateNum) { return new NodeTree("gssNode", stateNum); } void GraphStructuredStack::addToFrontier(int frontier, NodeTree* node) { //First, make sure our vector has this and lesser frontiers. If not, add it and up to it while (frontier >= gss.size()) { gss.push_back(new std::vector*>()); } gss[frontier]->push_back(node); } NodeTree* GraphStructuredStack::inFrontier(int frontier, int state) { if (frontierIsEmpty()) return NULL; for (std::vector*>::size_type i = 0; i < gss[frontier]->size(); i++) { if ((*(gss[frontier]))[i]->getData() == state) return (*(gss[frontier]))[i]; } return NULL; } bool GraphStructuredStack::frontierIsEmpty(int frontier) { return frontier >= gss.size() || gss[frontier]->size() == 0; } bool GraphStructuredStack::frontierHasAccState(int frontier) { //The acc state is always state 1, for now return inFrontier(frontier, 1); } std::vector*>* GraphStructuredStack::getReachable(NodeTree* start, int lenght) { std::vector*>* reachableList = new std::vector*>(); std::queue*> currentNodes; std::queue*> nextNodes; currentNodes.push_back(start); for (int i = 0; i < lenght; i++) { while (!currentNodes.empty()) { NodeTree* currentNode = currentNodes.front(); currentNodes.pop(); std::vector*> children = currentNode->getChildren(); for (std::vector*>::size_type j = 0; j < children.size(); j++) nextNodes.push_back(children[j]); } currentNodes = nextNodes; nextNodes.clear(); } while (!currentNodes.empty()) { reachableList->push_back(currentNodes.front()); currentNodes.pop(); } return reachableList; } bool GraphStructuredStack::hasEdge(NodeTree* start, NodeTree* end) { //Really, either testing for parent or child should work. return start->findChild(end) != -1; } void GraphStructuredStack::addEdge(NodeTree* start, NodeTree* end) { start->addChild(end); end->addChild(start); }