Nix 2.93.3
Lix: A modern, delicious implementation of the Nix package manager; unstable internal interfaces
Loading...
Searching...
No Matches
closure.hh
Go to the documentation of this file.
1#pragma once
3
6#include <functional>
7#include <kj/async.h>
8#include <set>
9
10namespace nix {
11
12template<typename T>
13std::set<T> computeClosure(
14 std::set<T> startElts,
15 std::function<std::set<T>(const T &)> getEdges
16)
17{
18 std::set<T> res, queue = std::move(startElts);
19
20 while (!queue.empty()) {
21 std::set<T> next;
22
23 for (auto & e : queue) {
24 if (res.insert(e).second) {
25 next.merge(getEdges(e));
26 }
27 }
28
29 queue = std::move(next);
30 }
31
32 return res;
33}
34
35template<typename T>
36kj::Promise<Result<std::set<T>>> computeClosureAsync(
37 std::set<T> startElts,
38 std::function<kj::Promise<Result<std::set<T>>>(const T &)> getEdges
39)
40try {
41 std::set<T> res, queue = std::move(startElts);
42
43 while (!queue.empty()) {
44 std::set<T> next;
45
46 for (auto & e : queue) {
47 if (res.insert(e).second) {
48 next.merge(LIX_TRY_AWAIT(getEdges(e)));
49 }
50 }
51
52 queue = std::move(next);
53 }
54
55 co_return res;
56} catch (...) {
57 co_return result::current_exception();
58}
59
60}