Updating
This commit is contained in:
@@ -6,6 +6,7 @@
|
|||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
namespace daggy {
|
namespace daggy {
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -20,7 +21,29 @@ namespace daggy {
|
|||||||
void dropEdge(const T & src, const T & dst);
|
void dropEdge(const T & src, const T & dst);
|
||||||
|
|
||||||
// Returns the path from {from} to {to}
|
// Returns the path from {from} to {to}
|
||||||
std::deque<T> shortest_path(const T & from, const T & to);
|
std::deque<T> shortestPath(const T & from, const T & to);
|
||||||
|
|
||||||
|
// Traversal
|
||||||
|
|
||||||
|
// Traverse all nodes, calling f(id). If f() returns
|
||||||
|
// false, stop traversing this branch.
|
||||||
|
// Guarantees to only visit each vertex once.
|
||||||
|
void traverse(std::function<bool> f);
|
||||||
|
|
||||||
|
// Same as traverse, but starts the traversal at
|
||||||
|
// the given id.
|
||||||
|
void traverseFrom(T& id, std::function<bool> f);
|
||||||
|
|
||||||
|
|
||||||
|
// Peek at the next node
|
||||||
|
T & next() const;
|
||||||
|
|
||||||
|
// Get the next node and mark it visited
|
||||||
|
T & visit_next();
|
||||||
|
|
||||||
|
// Clear the visited flag, optionally for downstream
|
||||||
|
// vertices
|
||||||
|
void unvisit(T &id, bool cascade = true);
|
||||||
|
|
||||||
// Breadth First Iterator
|
// Breadth First Iterator
|
||||||
struct Iterator {
|
struct Iterator {
|
||||||
@@ -32,12 +55,15 @@ namespace daggy {
|
|||||||
};
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
|
||||||
struct Vertex {
|
struct Vertex {
|
||||||
// It's a bit redundant to preserve both, but
|
// It's a bit redundant to preserve both, but
|
||||||
// it makes it possible with and against the
|
// it makes it possible with and against the
|
||||||
// directions possible
|
// directions possible
|
||||||
std::unordered_set<T> parents;
|
std::unordered_set<T> parents;
|
||||||
std::unordered_set<T> children;
|
std::unordered_set<T> children;
|
||||||
|
bool visited;
|
||||||
};
|
};
|
||||||
|
|
||||||
std::unordered_map<T, Vertex> vertices;
|
std::unordered_map<T, Vertex> vertices;
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ template<typename T>
|
|||||||
void DAG<T>::addVertex(T id) {
|
void DAG<T>::addVertex(T id) {
|
||||||
if (vertices.find(id) != vertices.end())
|
if (vertices.find(id) != vertices.end())
|
||||||
throw std::runtime_error("Vertex already exists in graph");
|
throw std::runtime_error("Vertex already exists in graph");
|
||||||
vertices.emplace(id, Vertex{});
|
vertices.emplace(id, Vertex{.visited = false});
|
||||||
roots.insert(id);
|
roots.insert(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,7 +43,7 @@ void DAG<T>::addEdge(const T & from, const T & to) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
std::deque<T> DAG<T>::shortest_path(const T & from, const T & to) {
|
std::deque<T> DAG<T>::shortestPath(const T & from, const T & to) {
|
||||||
std::deque<T> subpath;
|
std::deque<T> subpath;
|
||||||
|
|
||||||
if (from == to) return {to};
|
if (from == to) return {to};
|
||||||
|
|||||||
Reference in New Issue
Block a user