// Copyright (c) 20123 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_UTIL_WEAKENING_MAP_HPP #define MAMBA_UTIL_WEAKENING_MAP_HPP #include #include namespace mamba::util { /** * A Map wrapper that can weaken a key to find more matches. * * The API of a standard map is unmodified, only methods ending with "weaken" look for * multiple keys. * This can be understood as an extreme generalization of defaults: when a key is not found, * the behaviour is to look for another key. * The behaviour for generating the sequence of weaken keys is controlled by the Weakener. */ template class weakening_map : private Map { public: using Base = Map; using typename Base::key_type; using typename Base::mapped_type; using typename Base::value_type; using typename Base::size_type; using typename Base::iterator; using typename Base::const_iterator; using weakener_type = Weakener; using Base::Base; explicit weakening_map(Base map); template explicit weakening_map(weakener_type weakener, Args&&... args); [[nodiscard]] auto generic() const -> const Base&; using Base::begin; using Base::end; using Base::cbegin; using Base::cend; using Base::empty; using Base::max_size; using Base::clear; using Base::insert; using Base::insert_or_assign; using Base::emplace; using Base::emplace_hint; using Base::try_emplace; using Base::erase; using Base::swap; using Base::extract; using Base::merge; using Base::reserve; using Base::at; [[nodiscard]] auto size() const -> std::size_t; [[nodiscard]] auto at_weaken(const key_type& key) -> mapped_type&; [[nodiscard]] auto at_weaken(const key_type& key) const -> const mapped_type&; using Base::find; auto find_weaken(const key_type& key) -> iterator; auto find_weaken(const key_type& key) const -> const_iterator; [[nodiscard]] auto contains(const key_type& key) const -> bool; [[nodiscard]] auto contains_weaken(const key_type& key) const -> bool; private: Weakener m_weakener = {}; }; template auto operator==(const weakening_map& a, const weakening_map& b) -> bool; template auto operator!=(const weakening_map& a, const weakening_map& b) -> bool; /************************************* * Implementation of weakening_map * *************************************/ template weakening_map::weakening_map(Base map) : Base(std::move(map)) { } template template weakening_map::weakening_map(weakener_type weakener, Args&&... args) : Base(std::forward(args)...) , m_weakener(std::move(weakener)) { } template auto weakening_map::generic() const -> const Base& { return *this; } template auto weakening_map::size() const -> std::size_t { // https://github.com/pybind/pybind11/pull/4952 return Base::size(); } template auto weakening_map::at_weaken(const key_type& key) -> mapped_type& { if (auto it = find_weaken(key); it != end()) { return it->second; } throw std::out_of_range(fmt::format(R"(No entry for key "{}")", key)); } template auto weakening_map::at_weaken(const key_type& key) const -> const mapped_type& { return const_cast*>(this)->at_weaken(key); } template auto weakening_map::find_weaken(const key_type& key) -> iterator { auto k = key_type(m_weakener.make_first_key(key)); auto it = Base::find(k); while (it == Base::end()) { if (auto kk = m_weakener.weaken_key(k)) { // Try weakening the key more k = *kk; it = Base::find(k); } else { // No more weakened key to try return Base::end(); } } return it; } template auto weakening_map::find_weaken(const key_type& key) const -> const_iterator { return static_cast(const_cast*>(this)->find_weaken(key)); } template auto weakening_map::contains(const key_type& key) const -> bool { return find(key) != end(); } template auto weakening_map::contains_weaken(const key_type& key) const -> bool { return find_weaken(key) != end(); } template auto operator==(const weakening_map& a, const weakening_map& b) -> bool { return a.generic() == b.generic(); } template auto operator!=(const weakening_map& a, const weakening_map& b) -> bool { return a.generic() != b.generic(); } } #endif