2023-04-01 03:18:41 +00:00
|
|
|
#pragma once
|
|
|
|
///@file
|
|
|
|
|
2024-04-26 23:02:22 +00:00
|
|
|
#include <functional>
|
2021-05-18 12:30:32 +00:00
|
|
|
#include <set>
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
template<typename T>
|
2024-04-26 23:02:22 +00:00
|
|
|
std::set<T> computeClosure(
|
|
|
|
std::set<T> startElts,
|
|
|
|
std::function<std::set<T>(const T &)> getEdges
|
2021-05-18 12:30:32 +00:00
|
|
|
)
|
|
|
|
{
|
2024-04-26 23:02:22 +00:00
|
|
|
std::set<T> res, queue = std::move(startElts);
|
2021-05-18 12:30:32 +00:00
|
|
|
|
2024-04-26 23:02:22 +00:00
|
|
|
while (!queue.empty()) {
|
|
|
|
std::set<T> next;
|
2021-05-18 12:30:32 +00:00
|
|
|
|
2024-04-26 23:02:22 +00:00
|
|
|
for (auto & e : queue) {
|
|
|
|
if (res.insert(e).second) {
|
|
|
|
next.merge(getEdges(e));
|
|
|
|
}
|
2021-05-18 12:30:32 +00:00
|
|
|
}
|
|
|
|
|
2024-04-26 23:02:22 +00:00
|
|
|
queue = std::move(next);
|
2021-05-18 12:30:32 +00:00
|
|
|
}
|
2024-04-26 23:02:22 +00:00
|
|
|
|
|
|
|
return res;
|
2021-05-18 12:30:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|