// Copyright (c) 2023, 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_VARIANT_CMP_HPP #define MAMBA_UTIL_VARIANT_CMP_HPP #include #include #include namespace mamba::util { template [[nodiscard]] auto make_variant_cmp(IndexCmp&& index_cmp, AlternativeCmp&& alternative_cmp); /******************** * Implementation * ********************/ template auto make_variant_cmp(IndexCmp&& index_cmp, AlternativeCmp&& alternative_cmp) { return [int_cmp = std::forward(index_cmp), alt_cmp = std::forward(alternative_cmp) // ](const auto& lhs, const auto& rhs) -> bool { // When alternatives are different, compare the index. if (lhs.index() != rhs.index()) { return int_cmp(lhs.index(), rhs.index()); } // When alternatives are the same, compare the underlying type. return std::visit( [&](const auto& l) -> bool { using Alt = std::decay_t; const auto& r = std::get(rhs); return alt_cmp(l, r); }, lhs ); }; } } #endif