|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <stdx/ct_string.hpp> |
| 4 | + |
| 5 | +#include <boost/mp11/algorithm.hpp> |
| 6 | + |
| 7 | +#include <algorithm> |
| 8 | +#include <cstddef> |
| 9 | +#include <type_traits> |
| 10 | + |
| 11 | +namespace flow { |
| 12 | +template <typename T> using name_for = typename T::name_t; |
| 13 | + |
| 14 | +[[nodiscard]] constexpr auto edge_size(auto const &nodes, auto const &edges) |
| 15 | + -> std::size_t { |
| 16 | + auto const edge_capacities = transform( |
| 17 | + [&]<typename N>(N const &) { |
| 18 | + return edges.fold_left( |
| 19 | + std::size_t{}, []<typename E>(auto acc, E const &) { |
| 20 | + if constexpr (std::is_same_v<name_for<typename E::source_t>, |
| 21 | + name_for<N>> or |
| 22 | + std::is_same_v<name_for<typename E::dest_t>, |
| 23 | + name_for<N>>) { |
| 24 | + return ++acc; |
| 25 | + } else { |
| 26 | + return acc; |
| 27 | + } |
| 28 | + }); |
| 29 | + }, |
| 30 | + nodes); |
| 31 | + |
| 32 | + return edge_capacities.fold_left(std::size_t{1}, [](auto acc, auto next) { |
| 33 | + return std::max(acc, next); |
| 34 | + }); |
| 35 | +} |
| 36 | + |
| 37 | +namespace detail { |
| 38 | +template <typename N> struct matching_source { |
| 39 | + template <typename Edge> |
| 40 | + using fn = std::is_same<name_for<N>, name_for<typename Edge::source_t>>; |
| 41 | +}; |
| 42 | + |
| 43 | +template <typename N> struct matching_dest { |
| 44 | + template <typename Edge> |
| 45 | + using fn = std::is_same<name_for<N>, name_for<typename Edge::dest_t>>; |
| 46 | +}; |
| 47 | + |
| 48 | +template <typename T> struct matching_name { |
| 49 | + template <typename U> using fn = std::is_same<name_for<T>, name_for<U>>; |
| 50 | +}; |
| 51 | + |
| 52 | +template <typename Nodes> struct contained_edge_q { |
| 53 | + template <typename Edge> |
| 54 | + using fn = std::bool_constant< |
| 55 | + not boost::mp11::mp_empty<boost::mp11::mp_copy_if_q< |
| 56 | + Nodes, matching_name<typename Edge::source_t>>>::value and |
| 57 | + not boost::mp11::mp_empty<boost::mp11::mp_copy_if_q< |
| 58 | + Nodes, matching_name<typename Edge::dest_t>>>::value>; |
| 59 | +}; |
| 60 | + |
| 61 | +template <template <typename> typename Matcher, typename Edges> |
| 62 | +struct has_no_edge_q { |
| 63 | + template <typename Node> |
| 64 | + using fn = |
| 65 | + boost::mp11::mp_empty<boost::mp11::mp_copy_if_q<Edges, Matcher<Node>>>; |
| 66 | +}; |
| 67 | + |
| 68 | +template <typename E> using parent_of = typename E::source_t; |
| 69 | +template <typename E> using child_of = typename E::dest_t; |
| 70 | + |
| 71 | +template <typename Node, typename Edges> |
| 72 | +using parents_of = boost::mp11::mp_transform< |
| 73 | + parent_of, boost::mp11::mp_copy_if_q<Edges, matching_dest<Node>>>; |
| 74 | + |
| 75 | +template <typename Node, typename Edges> |
| 76 | +using children_of = boost::mp11::mp_transform< |
| 77 | + child_of, boost::mp11::mp_copy_if_q<Edges, matching_source<Node>>>; |
| 78 | + |
| 79 | +template <typename...> struct descendant_of_q; |
| 80 | + |
| 81 | +template <typename L, typename Start, typename Edges> |
| 82 | +using any_descendants = |
| 83 | + boost::mp11::mp_any_of_q<L, descendant_of_q<Start, Edges>>; |
| 84 | + |
| 85 | +template <typename Start, typename Edges> struct descendant_of_q<Start, Edges> { |
| 86 | + template <typename Node> |
| 87 | + using fn = boost::mp11::mp_eval_if_c< |
| 88 | + std::is_same_v<name_for<Node>, name_for<Start>>, std::true_type, |
| 89 | + any_descendants, parents_of<Node, Edges>, Start, Edges>; |
| 90 | +}; |
| 91 | + |
| 92 | +template <typename...> struct ancestor_of_q; |
| 93 | + |
| 94 | +template <typename L, typename Start, typename Edges> |
| 95 | +using any_ancestors = boost::mp11::mp_any_of_q<L, ancestor_of_q<Start, Edges>>; |
| 96 | + |
| 97 | +template <typename Start, typename Edges> struct ancestor_of_q<Start, Edges> { |
| 98 | + template <typename Node> |
| 99 | + using fn = boost::mp11::mp_eval_if_c< |
| 100 | + std::is_same_v<name_for<Node>, name_for<Start>>, std::true_type, |
| 101 | + any_ancestors, children_of<Node, Edges>, Start, Edges>; |
| 102 | +}; |
| 103 | +} // namespace detail |
| 104 | +} // namespace flow |
0 commit comments