diff --git a/Kernel/include_std/algorithm b/Kernel/include_std/algorithm index 5844703a..3404c007 100644 --- a/Kernel/include_std/algorithm +++ b/Kernel/include_std/algorithm @@ -409,4 +409,84 @@ namespace std template constexpr auto lexicographical_compare_three_way(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2); + + namespace detail + { + template + struct __algo_less + { + constexpr bool operator()(const T &lhs, const T &rhs) const + { + return lhs < rhs; + } + }; + + template + struct __algo_less + { + constexpr bool operator()(const T *lhs, const T *rhs) const + { + if (__builtin_is_constant_evaluated()) + return lhs < rhs; + return (uintptr_t)lhs < (uintptr_t)rhs; + } + }; + + template <> + class __algo_less + { + public: + template + constexpr auto operator()(T &&lhs, U &&rhs) const -> decltype(std::forward(lhs) < std::forward(rhs)) + { + return std::forward(lhs) < std::forward(rhs); + } + }; + } + + template + ForwardIt is_sorted_until(ForwardIt first, ForwardIt last) + { + return is_sorted_until(first, last, detail::__algo_less<>()); + } + + template + ForwardIt is_sorted_until(ExecutionPolicy &&policy, ForwardIt first, ForwardIt last); + + template + ForwardIt is_sorted_until(ForwardIt first, ForwardIt last, Compare comp) + { + if (first == last) + return last; + + ForwardIt next = first; + while (++next != last) + { + if (comp(*next, *first)) + return next; + first = next; + } + return last; + } + + template + ForwardIt is_sorted_until(ExecutionPolicy &&policy, ForwardIt first, ForwardIt last, Compare comp); + + template + constexpr bool is_sorted(ForwardIt first, ForwardIt last) + { + return std::is_sorted_until(first, last) == last; + } + + template + bool is_sorted(ExecutionPolicy &&policy, ForwardIt first, ForwardIt last); + + template + constexpr bool is_sorted(ForwardIt first, ForwardIt last, Compare comp) + { + return std::is_sorted_until(first, last, comp) == last; + } + + template + bool is_sorted(ExecutionPolicy &&policy, ForwardIt first, ForwardIt last, Compare comp); }