lix/src/libutil/closure.hh
eldritch horrors 964ac8b0e8 libutil: de-callback-ify computeClosure
only two users of this function exist. only one used it in a way that
even bears resemblance to asynchronicity, and even that one didn't do
it right. fully async and parallel computation would have only worked
if any getEdgesAsync never calls the continuation it receives itself,
only from more derived callbacks running on other threads. calling it
directly would cause the decoupling promise to be awaited immediately
*on the original thread*, completely negating all nice async effects.

Change-Id: I0aa640950cf327533a32dee410105efdabb448df
2024-05-07 14:35:20 +00:00

33 lines
517 B
C++

#pragma once
///@file
#include <functional>
#include <set>
namespace nix {
template<typename T>
std::set<T> computeClosure(
std::set<T> startElts,
std::function<std::set<T>(const T &)> getEdges
)
{
std::set<T> res, queue = std::move(startElts);
while (!queue.empty()) {
std::set<T> next;
for (auto & e : queue) {
if (res.insert(e).second) {
next.merge(getEdges(e));
}
}
queue = std::move(next);
}
return res;
}
}