// Copyright (c) 2019, QuantStack and Mamba Contributors // // Distributed under the terms of the BSD 3-Clause License. // // The full license is in the file LICENSE, distributed with this software. #ifndef MAMBA_PROBLEMS_GRAPH_HPP #define MAMBA_PROBLEMS_GRAPH_HPP #include #include #include #include #include #include #include #include #include #include #include "mamba/specs/match_spec.hpp" #include "mamba/specs/package_info.hpp" #include "mamba/util/flat_set.hpp" #include "mamba/util/graph.hpp" namespace mamba::solver { namespace libsolv { class Database; } template class conflict_map : private std::unordered_map> { public: using Base = std::unordered_map>; using typename Base::const_iterator; using typename Base::key_type; using typename Base::value_type; conflict_map() = default; conflict_map(std::initializer_list> conflicts_pairs); using Base::empty; using Base::size; [[nodiscard]] auto has_conflict(const key_type& a) const -> bool; [[nodiscard]] auto conflicts(const key_type& a) const -> const util::flat_set&; [[nodiscard]] auto in_conflict(const key_type& a, const key_type& b) const -> bool; using Base::cbegin; using Base::cend; auto begin() const noexcept -> const_iterator; auto end() const noexcept -> const_iterator; using Base::clear; auto add(const key_type& a, const key_type& b) -> bool; auto remove(const key_type& a, const key_type& b) -> bool; auto remove(const key_type& a) -> bool; private: auto base() const -> const Base&; auto remove_asym(const key_type& a, const key_type& b) -> bool; template friend auto operator==(const conflict_map& lhs, const conflict_map& rhs) -> bool; template friend auto operator!=(const conflict_map& lhs, const conflict_map& rhs) -> bool; }; template auto operator==(const conflict_map& lhs, const conflict_map& rhs) -> bool; template auto operator!=(const conflict_map& lhs, const conflict_map& rhs) -> bool; /** * A directed graph of the packages involved in a libsolv conflict. */ class ProblemsGraph { public: struct RootNode { }; struct PackageNode : specs::PackageInfo { }; struct UnresolvedDependencyNode : specs::MatchSpec { }; struct ConstraintNode : specs::MatchSpec { }; using node_t = std::variant; using edge_t = specs::MatchSpec; using graph_t = util::DiGraph; using node_id = graph_t::node_id; using conflicts_t = conflict_map; ProblemsGraph(graph_t graph, conflicts_t conflicts, node_id root_node); [[nodiscard]] auto graph() const noexcept -> const graph_t&; [[nodiscard]] auto conflicts() const noexcept -> const conflicts_t&; [[nodiscard]] auto root_node() const noexcept -> node_id; private: graph_t m_graph; conflicts_t m_conflicts; node_id m_root_node; }; /** * Hand-crafted hurisitics to simplify conflicts in messy situations. */ auto simplify_conflicts(const ProblemsGraph& pbs) -> ProblemsGraph; class CompressedProblemsGraph { public: using RootNode = ProblemsGraph::RootNode; /** * A rough comparison for nodes. * * We need to be able to compare nodes for using them in a set but we do not have * proper version parsing. * Ideally we would like proper comparison for printing packages in order. * * As with ``NamedList`` below, the implementation is currently kept private for * needed types. */ template struct RoughCompare { auto operator()(const T& a, const T& b) const -> bool; }; /** * A list of objects with a name, version, and build member. * * For simplicity, the implementation is kept in the translation unit with * specialization for needed types. */ template > class NamedList : private util::flat_set, Allocator> { public: using Base = util::flat_set, Allocator>; using typename Base::allocator_type; using typename Base::const_iterator; using typename Base::const_reverse_iterator; using typename Base::value_type; NamedList() = default; template NamedList(InputIterator first, InputIterator last); using Base::empty; using Base::size; auto front() const noexcept -> const value_type&; auto back() const noexcept -> const value_type&; using Base::cbegin; using Base::cend; using Base::crbegin; using Base::crend; auto begin() const noexcept -> const_iterator; auto end() const noexcept -> const_iterator; auto rbegin() const noexcept -> const_reverse_iterator; auto rend() const noexcept -> const_reverse_iterator; [[nodiscard]] auto name() const -> const std::string&; [[nodiscard]] auto versions_trunc( std::string_view sep = "|", std::string_view etc = "...", std::size_t threshold = 5, bool remove_duplicates = true ) const -> std::pair; [[nodiscard]] auto build_strings_trunc( std::string_view sep = "|", std::string_view etc = "...", std::size_t threshold = 5, bool remove_duplicates = true ) const -> std::pair; [[nodiscard]] auto versions_and_build_strings_trunc( std::string_view sep = "|", std::string_view etc = "...", std::size_t threshold = 5, bool remove_duplicates = true ) const -> std::pair; using Base::clear; using Base::reserve; void insert(const value_type& e); void insert(value_type&& e); template void insert(InputIterator first, InputIterator last); private: template void insert_impl(T_&& e); }; using PackageListNode = NamedList; using UnresolvedDependencyListNode = NamedList; using ConstraintListNode = NamedList; using node_t = std::variant< RootNode, // PackageListNode, UnresolvedDependencyListNode, ConstraintListNode>; using edge_t = NamedList; using graph_t = util::DiGraph; using node_id = graph_t::node_id; using conflicts_t = conflict_map; using merge_criteria_t = std::function< bool(const ProblemsGraph&, ProblemsGraph::node_id, ProblemsGraph::node_id)>; static auto from_problems_graph(const ProblemsGraph& pbs, const merge_criteria_t& merge_criteria = {}) -> CompressedProblemsGraph; CompressedProblemsGraph(graph_t graph, conflicts_t conflicts, node_id root_node); [[nodiscard]] auto graph() const noexcept -> const graph_t&; [[nodiscard]] auto conflicts() const noexcept -> const conflicts_t&; [[nodiscard]] auto root_node() const noexcept -> node_id; private: graph_t m_graph; conflicts_t m_conflicts; node_id m_root_node; }; /** * Formatting options for error message functions. */ struct ProblemsMessageFormat { fmt::text_style unavailable = fmt::fg(fmt::terminal_color::red); fmt::text_style available = fmt::fg(fmt::terminal_color::green); std::array indents = { "│ ", " ", "├─ ", "└─ " }; }; auto print_problem_tree_msg( std::ostream& out, const CompressedProblemsGraph& pbs, const ProblemsMessageFormat& format = {} ) -> std::ostream&; auto problem_tree_msg( // const CompressedProblemsGraph& pbs, const ProblemsMessageFormat& format = {} ) -> std::string; /************************************ * Implementation of conflict_map * ************************************/ template conflict_map::conflict_map(std::initializer_list> conflicts_pairs) { for (const auto& [a, b] : conflicts_pairs) { add(a, b); } } template auto conflict_map::has_conflict(const key_type& a) const -> bool { return Base::find(a) != end(); } template auto conflict_map::conflicts(const key_type& a) const -> const util::flat_set& { return Base::at(a); } template auto conflict_map::in_conflict(const key_type& a, const key_type& b) const -> bool { return has_conflict(a) && Base::at(a).contains(b); } template auto conflict_map::begin() const noexcept -> const_iterator { return Base::begin(); } template auto conflict_map::end() const noexcept -> const_iterator { return Base::end(); } template auto conflict_map::add(const key_type& a, const key_type& b) -> bool { auto [_, inserted] = Base::operator[](a).insert(b); if (a != b) { Base::operator[](b).insert(a); } return inserted; } template auto conflict_map::base() const -> const Base& { return static_cast(*this); } template auto conflict_map::remove_asym(const key_type& a, const key_type& b) -> bool { auto iter = Base::find(a); if (iter == Base::end()) { return false; } auto& cflcts = iter->second; const bool erased = cflcts.erase(b); if (cflcts.empty()) { Base::erase(a); } return erased; }; template auto conflict_map::remove(const key_type& a, const key_type& b) -> bool { return remove_asym(a, b) && ((a == b) || remove_asym(b, a)); } template auto conflict_map::remove(const key_type& a) -> bool { auto a_iter = Base::find(a); if (a_iter == Base::end()) { return false; } for (const auto& b : a_iter->second) { if (a != b) // Cannot modify while we iterate on it { remove_asym(b, a); } } Base::erase(a); return true; } template auto operator==(const conflict_map& lhs, const conflict_map& rhs) -> bool { return lhs.base() == rhs.base(); } template auto operator!=(const conflict_map& lhs, const conflict_map& rhs) -> bool { return !(lhs == rhs); } /********************************* * Implementation of NamedList * *********************************/ template template void CompressedProblemsGraph::NamedList::insert(InputIterator first, InputIterator last) { for (; first < last; ++first) { insert(*first); } } } #endif