2016-09-18 20:50:08 -07:00
|
|
|
#pragma once
|
|
|
|
|
2020-02-03 14:22:52 -08:00
|
|
|
#include <string>
|
2016-09-18 20:50:08 -07:00
|
|
|
#include <unordered_map>
|
2020-02-03 14:22:52 -08:00
|
|
|
#include <vector>
|
2016-09-18 20:50:08 -07:00
|
|
|
|
2017-10-13 18:37:41 -07:00
|
|
|
#include <vcpkg/base/checks.h>
|
2019-04-08 23:26:18 -07:00
|
|
|
#include <vcpkg/base/system.print.h>
|
2017-10-13 18:37:41 -07:00
|
|
|
|
2017-01-05 12:47:08 -08:00
|
|
|
namespace vcpkg::Graphs
|
2016-09-18 20:50:08 -07:00
|
|
|
{
|
|
|
|
enum class ExplorationStatus
|
|
|
|
{
|
|
|
|
// We have not visited this vertex
|
|
|
|
NOT_EXPLORED,
|
|
|
|
|
|
|
|
// We have visited this vertex but haven't visited all vertices in its subtree
|
|
|
|
PARTIALLY_EXPLORED,
|
|
|
|
|
|
|
|
// We have visited this vertex and all vertices in its subtree
|
|
|
|
FULLY_EXPLORED
|
|
|
|
};
|
|
|
|
|
2017-04-27 17:56:06 -07:00
|
|
|
template<class V, class U>
|
2017-10-16 11:44:04 -07:00
|
|
|
struct AdjacencyProvider
|
2016-09-18 20:50:08 -07:00
|
|
|
{
|
2017-10-16 11:44:04 -07:00
|
|
|
virtual std::vector<V> adjacency_list(const U& vertex) const = 0;
|
2017-12-18 23:00:11 -08:00
|
|
|
virtual std::string to_string(const V& vertex) const = 0;
|
2017-10-16 11:44:04 -07:00
|
|
|
virtual U load_vertex_data(const V& vertex) const = 0;
|
2017-04-11 19:37:38 -07:00
|
|
|
};
|
2017-04-10 18:45:44 -07:00
|
|
|
|
2019-01-18 19:03:37 -08:00
|
|
|
struct Randomizer
|
|
|
|
{
|
|
|
|
virtual int random(int max_exclusive) = 0;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
~Randomizer() {}
|
|
|
|
};
|
|
|
|
|
2017-12-18 23:00:11 -08:00
|
|
|
namespace details
|
2017-04-11 19:37:38 -07:00
|
|
|
{
|
2019-01-18 19:03:37 -08:00
|
|
|
template<class Container>
|
|
|
|
void shuffle(Container& c, Randomizer* r)
|
|
|
|
{
|
|
|
|
if (!r) return;
|
2019-06-19 11:49:57 -07:00
|
|
|
for (auto i = c.size(); i > 1; --i)
|
2019-01-18 19:03:37 -08:00
|
|
|
{
|
2019-08-09 11:16:35 -07:00
|
|
|
std::size_t j = r->random(static_cast<int>(i));
|
2019-01-18 19:03:37 -08:00
|
|
|
if (j != i - 1)
|
|
|
|
{
|
|
|
|
std::swap(c[i - 1], c[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-18 23:00:11 -08:00
|
|
|
template<class V, class U>
|
|
|
|
void topological_sort_internal(const V& vertex,
|
|
|
|
const AdjacencyProvider<V, U>& f,
|
|
|
|
std::unordered_map<V, ExplorationStatus>& exploration_status,
|
2019-01-18 19:03:37 -08:00
|
|
|
std::vector<U>& sorted,
|
|
|
|
Randomizer* randomizer)
|
2017-04-11 14:44:14 -07:00
|
|
|
{
|
2017-12-18 23:00:11 -08:00
|
|
|
ExplorationStatus& status = exploration_status[vertex];
|
|
|
|
switch (status)
|
2017-04-27 17:56:06 -07:00
|
|
|
{
|
2017-12-18 23:00:11 -08:00
|
|
|
case ExplorationStatus::FULLY_EXPLORED: return;
|
|
|
|
case ExplorationStatus::PARTIALLY_EXPLORED:
|
|
|
|
{
|
2019-04-08 23:26:18 -07:00
|
|
|
System::print2("Cycle detected within graph at ", f.to_string(vertex), ":\n");
|
2017-12-18 23:00:11 -08:00
|
|
|
for (auto&& node : exploration_status)
|
|
|
|
{
|
|
|
|
if (node.second == ExplorationStatus::PARTIALLY_EXPLORED)
|
|
|
|
{
|
2019-04-08 23:26:18 -07:00
|
|
|
System::print2(" ", f.to_string(node.first), '\n');
|
2017-12-18 23:00:11 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Checks::exit_fail(VCPKG_LINE_INFO);
|
|
|
|
}
|
|
|
|
case ExplorationStatus::NOT_EXPLORED:
|
|
|
|
{
|
|
|
|
status = ExplorationStatus::PARTIALLY_EXPLORED;
|
|
|
|
U vertex_data = f.load_vertex_data(vertex);
|
2019-01-18 19:03:37 -08:00
|
|
|
auto neighbours = f.adjacency_list(vertex_data);
|
|
|
|
details::shuffle(neighbours, randomizer);
|
|
|
|
for (const V& neighbour : neighbours)
|
|
|
|
topological_sort_internal(neighbour, f, exploration_status, sorted, randomizer);
|
2017-12-18 23:00:11 -08:00
|
|
|
|
|
|
|
sorted.push_back(std::move(vertex_data));
|
|
|
|
status = ExplorationStatus::FULLY_EXPLORED;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
default: Checks::unreachable(VCPKG_LINE_INFO);
|
2017-04-27 17:56:06 -07:00
|
|
|
}
|
2017-04-11 14:44:14 -07:00
|
|
|
}
|
|
|
|
}
|
2016-09-18 20:50:08 -07:00
|
|
|
|
2020-02-03 14:22:52 -08:00
|
|
|
template<class Range, class V, class U>
|
|
|
|
std::vector<U> topological_sort(Range starting_vertices, const AdjacencyProvider<V, U>& f, Randomizer* randomizer)
|
2017-04-11 14:44:14 -07:00
|
|
|
{
|
2017-04-11 19:37:38 -07:00
|
|
|
std::vector<U> sorted;
|
2017-04-11 14:44:14 -07:00
|
|
|
std::unordered_map<V, ExplorationStatus> exploration_status;
|
|
|
|
|
2019-01-18 19:03:37 -08:00
|
|
|
details::shuffle(starting_vertices, randomizer);
|
|
|
|
|
2017-12-18 23:00:11 -08:00
|
|
|
for (auto&& vertex : starting_vertices)
|
2017-04-11 14:44:14 -07:00
|
|
|
{
|
2019-01-18 19:03:37 -08:00
|
|
|
details::topological_sort_internal(vertex, f, exploration_status, sorted, randomizer);
|
2016-09-18 20:50:08 -07:00
|
|
|
}
|
|
|
|
|
2017-04-11 14:44:14 -07:00
|
|
|
return sorted;
|
|
|
|
}
|
2017-01-05 12:47:08 -08:00
|
|
|
}
|