forked from lix-project/lix
Abstract out topo sorting logic
This commit is contained in:
parent
86805a2c0a
commit
8065c6d160
|
@ -4,6 +4,7 @@
|
||||||
#include "local-store.hh"
|
#include "local-store.hh"
|
||||||
#include "store-api.hh"
|
#include "store-api.hh"
|
||||||
#include "thread-pool.hh"
|
#include "thread-pool.hh"
|
||||||
|
#include "topo-sort.hh"
|
||||||
|
|
||||||
|
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
@ -246,41 +247,21 @@ void Store::queryMissing(const std::vector<StorePathWithOutputs> & targets,
|
||||||
|
|
||||||
StorePaths Store::topoSortPaths(const StorePathSet & paths)
|
StorePaths Store::topoSortPaths(const StorePathSet & paths)
|
||||||
{
|
{
|
||||||
StorePaths sorted;
|
return topoSort(paths,
|
||||||
StorePathSet visited, parents;
|
{[&](const StorePath & path) {
|
||||||
|
|
||||||
std::function<void(const StorePath & path, const StorePath * parent)> dfsVisit;
|
|
||||||
|
|
||||||
dfsVisit = [&](const StorePath & path, const StorePath * parent) {
|
|
||||||
if (parents.count(path))
|
|
||||||
throw BuildError("cycle detected in the references of '%s' from '%s'",
|
|
||||||
printStorePath(path), printStorePath(*parent));
|
|
||||||
|
|
||||||
if (!visited.insert(path).second) return;
|
|
||||||
parents.insert(path);
|
|
||||||
|
|
||||||
StorePathSet references;
|
StorePathSet references;
|
||||||
try {
|
try {
|
||||||
references = queryPathInfo(path)->references;
|
references = queryPathInfo(path)->references;
|
||||||
} catch (InvalidPath &) {
|
} catch (InvalidPath &) {
|
||||||
}
|
}
|
||||||
|
return references;
|
||||||
for (auto & i : references)
|
}},
|
||||||
/* Don't traverse into paths that don't exist. That can
|
{[&](const StorePath & path, const StorePath & parent) {
|
||||||
happen due to substitutes for non-existent paths. */
|
return BuildError(
|
||||||
if (i != path && paths.count(i))
|
"cycle detected in the references of '%s' from '%s'",
|
||||||
dfsVisit(i, &path);
|
printStorePath(path),
|
||||||
|
printStorePath(parent));
|
||||||
sorted.push_back(path);
|
}});
|
||||||
parents.erase(path);
|
|
||||||
};
|
|
||||||
|
|
||||||
for (auto & i : paths)
|
|
||||||
dfsVisit(i, nullptr);
|
|
||||||
|
|
||||||
std::reverse(sorted.begin(), sorted.end());
|
|
||||||
|
|
||||||
return sorted;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
40
src/libutil/topo-sort.hh
Normal file
40
src/libutil/topo-sort.hh
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
#include "error.hh"
|
||||||
|
|
||||||
|
namespace nix {
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
std::vector<T> topoSort(std::set<T> items,
|
||||||
|
std::function<std::set<T>(const T &)> getChildren,
|
||||||
|
std::function<Error(const T &, const T &)> makeCycleError)
|
||||||
|
{
|
||||||
|
std::vector<T> sorted;
|
||||||
|
std::set<T> visited, parents;
|
||||||
|
|
||||||
|
std::function<void(const T & path, const T * parent)> dfsVisit;
|
||||||
|
|
||||||
|
dfsVisit = [&](const T & path, const T * parent) {
|
||||||
|
if (parents.count(path)) throw makeCycleError(path, *parent);
|
||||||
|
|
||||||
|
if (!visited.insert(path).second) return;
|
||||||
|
parents.insert(path);
|
||||||
|
|
||||||
|
std::set<T> references = getChildren(path);
|
||||||
|
|
||||||
|
for (auto & i : references)
|
||||||
|
/* Don't traverse into items that don't exist in our starting set. */
|
||||||
|
if (i != path && items.count(i))
|
||||||
|
dfsVisit(i, &path);
|
||||||
|
|
||||||
|
sorted.push_back(path);
|
||||||
|
parents.erase(path);
|
||||||
|
};
|
||||||
|
|
||||||
|
for (auto & i : items)
|
||||||
|
dfsVisit(i, nullptr);
|
||||||
|
|
||||||
|
std::reverse(sorted.begin(), sorted.end());
|
||||||
|
|
||||||
|
return sorted;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue