Headers move, CMake targets merge, C++ namespaces stay unchanged: - Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant - Functional: Pipeline, Either, Result, Retry - Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval - Math: +Matrix, Numeric, Random, Histogram - Text: Str, Format, Tokenizer, Diff, Encoding, Semver - Data: Json, Csv, Ini - Async: Coro, Concurrency, EventBus, Observable - Time: +Bench - Log: Log facade + Logging framework (facade now header-only, breaking the Stacktrace<->Log link cycle the merge would have created) - System: FileSystem + Process - Configuration: +Argparse - Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail Module system fixes: - OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no longer depends on the alphabetical discovery order of src/ subdirs (Testing -> Text would have been silently dropped). - Every header-only module now has a tracked module.cmake; previously the empty src/<Name> marker dirs were untracked, so a fresh clone would have silently skipped those modules. - VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT. Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files prefixed: ResultBasic etc.). Test count verified identical (831) before and after. README/CLAUDE.md module tables updated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
206 lines
6.2 KiB
C++
206 lines
6.2 KiB
C++
#pragma once
|
|
|
|
#include <algorithm>
|
|
#include <cstddef>
|
|
#include <functional>
|
|
#include <optional>
|
|
#include <queue>
|
|
#include <ranges>
|
|
#include <span>
|
|
#include <stdexcept>
|
|
#include <unordered_map>
|
|
#include <unordered_set>
|
|
#include <vector>
|
|
|
|
namespace Void::Graph
|
|
{
|
|
|
|
// ── DiGraph<V, E> ─────────────────────────────────────────────────────────────
|
|
// Directed graph with vertex labels of type V and edge weights of type E.
|
|
// Vertices are identified by a user-supplied key of type V (must be hashable).
|
|
|
|
template <typename V, typename E = int>
|
|
class DiGraph
|
|
{
|
|
public:
|
|
struct Edge
|
|
{
|
|
V to;
|
|
E weight;
|
|
};
|
|
|
|
void add_vertex(const V& v) { adj_[v]; }
|
|
|
|
void add_edge(const V& from, const V& to, E weight = E{})
|
|
{
|
|
adj_[from].push_back({to, weight});
|
|
adj_.try_emplace(to);
|
|
}
|
|
|
|
[[nodiscard]] bool has_vertex(const V& v) const
|
|
{
|
|
return adj_.contains(v);
|
|
}
|
|
|
|
[[nodiscard]] bool has_edge(const V& from, const V& to) const
|
|
{
|
|
auto it = adj_.find(from);
|
|
if (it == adj_.end()) return false;
|
|
return std::ranges::any_of(it->second,
|
|
[&](const Edge& e) { return e.to == to; });
|
|
}
|
|
|
|
[[nodiscard]] const std::vector<Edge>& neighbors(const V& v) const
|
|
{
|
|
return adj_.at(v);
|
|
}
|
|
|
|
[[nodiscard]] std::size_t vertex_count() const noexcept
|
|
{
|
|
return adj_.size();
|
|
}
|
|
|
|
[[nodiscard]] std::size_t edge_count() const noexcept
|
|
{
|
|
std::size_t n = 0;
|
|
for (const auto& [_, edges] : adj_) n += edges.size();
|
|
return n;
|
|
}
|
|
|
|
// ── BFS ──────────────────────────────────────────────────────────────────
|
|
|
|
void bfs(const V& start,
|
|
std::function<void(const V&)> visit) const
|
|
{
|
|
if (!adj_.contains(start)) return;
|
|
std::unordered_set<V> visited;
|
|
std::queue<V> q;
|
|
visited.insert(start);
|
|
q.push(start);
|
|
while (!q.empty()) {
|
|
const V cur = q.front(); q.pop();
|
|
visit(cur);
|
|
for (const auto& e : adj_.at(cur)) {
|
|
if (!visited.contains(e.to)) {
|
|
visited.insert(e.to);
|
|
q.push(e.to);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
[[nodiscard]] std::vector<V> bfs_order(const V& start) const
|
|
{
|
|
std::vector<V> result;
|
|
bfs(start, [&](const V& v) { result.push_back(v); });
|
|
return result;
|
|
}
|
|
|
|
// ── DFS ──────────────────────────────────────────────────────────────────
|
|
|
|
void dfs(const V& start,
|
|
std::function<void(const V&)> visit) const
|
|
{
|
|
std::unordered_set<V> visited;
|
|
dfs_impl(start, visited, visit);
|
|
}
|
|
|
|
[[nodiscard]] std::vector<V> dfs_order(const V& start) const
|
|
{
|
|
std::vector<V> result;
|
|
dfs(start, [&](const V& v) { result.push_back(v); });
|
|
return result;
|
|
}
|
|
|
|
// ── Topological sort (Kahn's algorithm) ──────────────────────────────────
|
|
// Returns nullopt if the graph has a cycle.
|
|
|
|
[[nodiscard]] std::optional<std::vector<V>> topological_sort() const
|
|
{
|
|
std::unordered_map<V, int> in_degree;
|
|
for (const auto& [v, _] : adj_) in_degree[v] = 0;
|
|
for (const auto& [v, edges] : adj_) {
|
|
for (const auto& e : edges) in_degree[e.to]++;
|
|
}
|
|
|
|
std::queue<V> q;
|
|
for (const auto& [v, deg] : in_degree) {
|
|
if (deg == 0) q.push(v);
|
|
}
|
|
|
|
std::vector<V> order;
|
|
while (!q.empty()) {
|
|
V cur = q.front(); q.pop();
|
|
order.push_back(cur);
|
|
for (const auto& e : adj_.at(cur)) {
|
|
if (--in_degree[e.to] == 0) q.push(e.to);
|
|
}
|
|
}
|
|
|
|
if (order.size() != adj_.size()) return std::nullopt; // cycle
|
|
return order;
|
|
}
|
|
|
|
// ── Reachability ─────────────────────────────────────────────────────────
|
|
|
|
[[nodiscard]] bool can_reach(const V& from, const V& to) const
|
|
{
|
|
bool found = false;
|
|
bfs(from, [&](const V& v) { if (v == to) found = true; });
|
|
return found;
|
|
}
|
|
|
|
// ── Shortest path (BFS, unweighted) ──────────────────────────────────────
|
|
|
|
[[nodiscard]] std::optional<std::vector<V>>
|
|
shortest_path(const V& from, const V& to) const
|
|
{
|
|
if (!adj_.contains(from) || !adj_.contains(to)) return std::nullopt;
|
|
std::unordered_map<V, V> parent;
|
|
std::unordered_set<V> visited;
|
|
std::queue<V> q;
|
|
visited.insert(from);
|
|
q.push(from);
|
|
bool found = false;
|
|
while (!q.empty() && !found) {
|
|
V cur = q.front(); q.pop();
|
|
for (const auto& e : adj_.at(cur)) {
|
|
if (!visited.contains(e.to)) {
|
|
visited.insert(e.to);
|
|
parent[e.to] = cur;
|
|
if (e.to == to) { found = true; break; }
|
|
q.push(e.to);
|
|
}
|
|
}
|
|
}
|
|
if (!found && from != to) return std::nullopt;
|
|
std::vector<V> path;
|
|
V cur = to;
|
|
while (cur != from) {
|
|
path.push_back(cur);
|
|
cur = parent.at(cur);
|
|
}
|
|
path.push_back(from);
|
|
std::ranges::reverse(path);
|
|
return path;
|
|
}
|
|
|
|
private:
|
|
void dfs_impl(const V& v, std::unordered_set<V>& visited,
|
|
std::function<void(const V&)>& visit) const
|
|
{
|
|
if (visited.contains(v)) return;
|
|
visited.insert(v);
|
|
visit(v);
|
|
if (adj_.contains(v)) {
|
|
for (const auto& e : adj_.at(v)) {
|
|
dfs_impl(e.to, visited, visit);
|
|
}
|
|
}
|
|
}
|
|
|
|
std::unordered_map<V, std::vector<Edge>> adj_;
|
|
};
|
|
|
|
} // namespace Void::Graph
|