Short live q20::is_sorted{,_until}!

This is just the C++11 version with constexpr added, very useful to,
e.g., statically check sorted-ness of static arrays.

Task-number: QTBUG-103721
Change-Id: I60164e49db1cc3892280a19851e01193e3c1fb2a
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
bb10
Marc Mutz 2022-05-19 10:09:36 +02:00
parent b7fcaa41a1
commit c0bb5b2aa1
1 changed files with 28 additions and 0 deletions

View File

@ -26,6 +26,34 @@
QT_BEGIN_NAMESPACE
namespace q20 {
// like std::is_sorted{,_until} (ie. constexpr)
#ifdef __cpp_lib_constexpr_algorithms
using std::is_sorted_until;
using std::is_sorted;
#else
template <typename ForwardIterator, typename BinaryPredicate = std::less<>>
constexpr ForwardIterator
is_sorted_until(ForwardIterator first, ForwardIterator last, BinaryPredicate p = {})
{
if (first == last)
return first;
auto prev = first;
while (++first != last) {
if (p(*first, *prev))
return first;
prev = first;
}
return first;
}
template <typename ForwardIterator, typename BinaryPredicate = std::less<>>
constexpr bool is_sorted(ForwardIterator first, ForwardIterator last, BinaryPredicate p = {})
{
return q20::is_sorted_until(first, last, p) == last;
}
#endif
}
namespace q20::ranges {
// like std::ranges::{any,all,none}_of, just unconstrained, so no range-overload
#ifdef __cpp_lib_ranges