Files
WFR-lib/include/wfr/wep/utils/WepSearch.h
T
2017-04-11 15:49:21 +02:00

95 lines
2.0 KiB
C++
Executable File

#pragma once
#include <functional>
namespace wfr {
namespace wep {
class AbstractWepNode;
}
namespace utils {
namespace wep {
template<typename VisitType>
void wepDepthFirstSearch(wfr::wep::AbstractWepNode* startNode, const VisitType& visitFunction,
std::set<wfr::wep::AbstractWepNode*>& visitedNodes = std::set<wfr::wep::AbstractWepNode*>()) {
visitedNodes.insert(startNode);
if (visitFunction(startNode)) {
for (auto c : startNode->getSuccessors()) {
if (visitedNodes.find(c) == visitedNodes.end()) {
wepDepthFirstSearch(c, visitFunction, visitedNodes);
}
}
}
}
template<typename VisitType>
void wepBreadthFirstSearch(wfr::wep::AbstractWepNode* startNode, const VisitType& visitFunction) {
std::vector<wfr::wep::AbstractWepNode*> nodeQueue;
std::set<wfr::wep::AbstractWepNode*> visitedNodes;
nodeQueue.push_back(startNode);
while (!nodeQueue.empty()) {
auto cn = nodeQueue.front();
visitedNodes.insert(cn);
nodeQueue.erase(nodeQueue.begin());
if (visitFunction(cn)) {
for (auto c : cn->getSuccessors()) {
if (visitedNodes.find(c) == visitedNodes.end()) {
bool isInQueue = false;
for (auto e : nodeQueue) {
if (c == e) {
isInQueue = true;
break;
}
}
if (!isInQueue) {
nodeQueue.push_back(c);
}
}
}
}
}
}
template<typename VisitType>
void wepBreadthFirstReverseSearch(wfr::wep::AbstractWepNode* startNode, const VisitType& visitFunction) {
std::vector<wfr::wep::AbstractWepNode*> nodeQueue;
std::set<wfr::wep::AbstractWepNode*> visitedNodes;
nodeQueue.push_back(startNode);
while (!nodeQueue.empty()) {
auto cn = nodeQueue.front();
visitedNodes.insert(cn);
nodeQueue.erase(nodeQueue.begin());
if (visitFunction(cn)) {
for (auto c : cn->getPredecessors()) {
if (visitedNodes.find(c) == visitedNodes.end()) {
bool isInQueue = false;
for (auto e : nodeQueue) {
if (c == e) {
isInQueue = true;
break;
}
}
if (!isInQueue) {
nodeQueue.push_back(c);
}
}
}
}
}
}
}
}
}