aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/closure.hh
blob: 146931fc85eaf22345b1ba36d5a03abab01d5686 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#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;
}

}