48#include <lal/graphs/free_tree.hpp>
49#include <lal/iterators/E_iterator.hpp>
50#include <lal/detail/arrangement_wrapper.hpp>
51#include <lal/detail/sorting/counting_sort.hpp>
52#include <lal/detail/sorting/sorted_vector.hpp>
53#include <lal/detail/array.hpp>
55#define max_pos(u,v) (std::max(arr[u], arr[v]))
71template <
class depflux,
class arrangement_t>
75 const arrangement_t& arr,
76 const std::vector<std::pair<edge_t,uint64_t>>& edge_with_max_pos_at,
78 std::vector<depflux>& flux,
79 std::vector<edge>& cur_deps
87 cur_deps = flux[cur_pos - 1].get_dependencies();
91 if (edge_with_max_pos_at[cur_pos].second > 0) {
92 const auto [first, last] =
94 cur_deps.begin(), cur_deps.end(),
95 edge_with_max_pos_at[cur_pos].first,
97 const auto pos_e1 = max_pos(e1.first, e1.second);
98 const auto pos_e2 = max_pos(e2.first, e2.second);
99 return pos_e1 < pos_e2;
102 if (first != cur_deps.end()) {
103 cur_deps.erase(first, last);
108 for (
const node_t v : t.get_neighbors(u)) {
109 if (arr[v] > cur_pos) {
110 cur_deps.push_back({u,*v});
115 for (
const auto& [v,w] : cur_deps) {
119 for (
node_t v : set_endpoints) {
120 flux[cur_pos].get_left_span() += (arr[v] <= cur_pos);
121 flux[cur_pos].get_right_span() += (arr[v] > cur_pos);
135 if (dependencies.size() <= 1) {
return dependencies.size(); }
138 ug.set_edges(dependencies);
146 const auto find_leaf =
148 for (
node u = 0; u < g.get_num_nodes(); ++u) {
149 if (g.get_degree(u) == 1) {
return u; }
156 while (
const auto leaf = find_leaf(ug)) {
157 const node u = *leaf;
158 const node v = ug.get_neighbors(u)[0];
162 ug.remove_edges_incident_to(v);
175template <
class depflux,
class arrangement_t>
176[[nodiscard]] std::vector<depflux> dependency_flux_compute
180 const uint64_t n = t.get_num_nodes();
181 if (n == 1) {
return {}; }
184 std::vector<std::pair<edge_t,uint64_t>> edge_with_max_pos_at(n, {{}, 0});
185 for (iterators::E_iterator e_it(t); not e_it.end(); e_it.next()) {
186 const auto [u, v] = e_it.get_edge_t();
188 edge_with_max_pos_at[max].first = {u,v};
189 ++edge_with_max_pos_at[max].second;
193 graphs::undirected_graph ug(n);
196 sorting::countingsort::memory<edge> mem(n, n);
199 std::vector<depflux> flux(n - 1);
201 for (
position cur_pos = 0; cur_pos < n - 1; ++cur_pos) {
203 std::vector<edge> cur_deps;
208 (t, arr, edge_with_max_pos_at, cur_pos, flux, cur_deps);
221 cur_deps.begin(), cur_deps.end(),
225 [&](
const edge_t& e) -> std::size_t
226 {
return max_pos(e.first, e.second); },
232 flux[cur_pos].set_dependencies(std::move(cur_deps));
Sorted vector class.
Definition sorted_vector.hpp:65
iterator_t insert_sorted(const T &x) noexcept
Insert an element to the vector.
Definition sorted_vector.hpp:94
Free tree graph class.
Definition free_tree.hpp:60
Undirected graph class.
Definition undirected_graph.hpp:66
@ non_decreasing
Non-decreasing sort type.
void counting_sort(const value_iterator_t begin, const value_iterator_t end, const std::size_t largest_key_plus_1, const Callable &key, countingsort::memory< value_t > &mem) noexcept
Counting sort algorithm with reusable memory.
Definition counting_sort.hpp:152
uint64_t calculate_weight(const std::vector< edge > &dependencies, graphs::undirected_graph &ug) noexcept
Calculates the weight of a set of dependencies in a flux.
Definition dependency_flux.hpp:132
void calculate_dependencies_and_span(const graphs::free_tree &t, const arrangement_t &arr, const std::vector< std::pair< edge_t, uint64_t > > &edge_with_max_pos_at, const position cur_pos, std::vector< depflux > &flux, std::vector< edge > &cur_deps) noexcept
Calculate the dependencies and their span.
Definition dependency_flux.hpp:73
Main namespace of the library.
Definition basic_types.hpp:48
std::pair< node, node > edge
See Edge page for further details.
Definition basic_types.hpp:56
uint64_t position
Node's position type.
Definition basic_types.hpp:53
uint64_t node
Node type. See Node / Vertex page for further details.
Definition basic_types.hpp:51
std::pair< node_t, node_t > edge_t
Similar to edge.
Definition basic_types.hpp:239
Typesafe node type.
Definition basic_types.hpp:70
Typesafe position type.
Definition basic_types.hpp:244