-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Move emulation of std::lexicographical_compare_three_way to its own algorithm.hpp file.
- Loading branch information
1 parent
4031546
commit 76b5b6f
Showing
4 changed files
with
51 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#pragma once | ||
|
||
#include <algorithm> | ||
|
||
#include "config.hpp" | ||
|
||
#if AMC_CXX20 | ||
#include <compare> | ||
#endif | ||
|
||
namespace amc { | ||
|
||
#if AMC_CXX20 | ||
#if !defined(_LIBCPP_VERSION) || _LIBCPP_VERSION >= 170000 | ||
using std::lexicographical_compare_three_way; | ||
#else | ||
// Adjusted from https://en.cppreference.com/w/cpp/algorithm/lexicographical_compare_three_way | ||
template <class I1, class I2, class Cmp> | ||
constexpr auto lexicographical_compare_three_way(I1 f1, I1 l1, I2 f2, I2 l2, Cmp comp) -> decltype(comp(*f1, *f2)) { | ||
using ret_t = decltype(comp(*f1, *f2)); | ||
static_assert(std::disjunction_v<std::is_same<ret_t, std::strong_ordering>, std::is_same<ret_t, std::weak_ordering>, | ||
std::is_same<ret_t, std::partial_ordering> >, | ||
"The return type must be a comparison category type."); | ||
|
||
for (; f1 != l1; ++f1, ++f2) { | ||
if (f2 == l2) return std::strong_ordering::greater; | ||
if (auto c = comp(*f1, *f2); c != 0) return c; | ||
} | ||
return (f2 == l2) <=> true; | ||
} | ||
#if _LIBCPP_VERSION >= 14000 | ||
using std::compare_three_way; | ||
#else | ||
struct compare_three_way { | ||
using is_transparent = void; | ||
template <class T, class U> | ||
constexpr auto operator()(T&& t, U&& u) const noexcept(noexcept(std::declval<T>() <=> std::declval<U>())) { | ||
return static_cast<T&&>(t) <=> static_cast<U&&>(u); | ||
} | ||
}; | ||
#endif | ||
template <class I1, class I2> | ||
constexpr auto lexicographical_compare_three_way(I1 f1, I1 l1, I2 f2, I2 l2) { | ||
return lexicographical_compare_three_way(f1, l1, f2, l2, amc::compare_three_way()); | ||
} | ||
#endif | ||
#endif | ||
|
||
} // namespace amc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters