[libc++][ranges] Implement ranges::minmax and ranges::minmax_element

Reviewed By: var-const, #libc, ldionne

Spies: sstefan1, ldionne, BRevzin, libcxx-commits, mgorny

Differential Revision: https://reviews.llvm.org/D120637
This commit is contained in:
Nikolas Klauser 2022-04-13 22:59:09 +02:00
parent 0b55a8dc6f
commit 58d9ab70ae
19 changed files with 1061 additions and 107 deletions

View File

@ -352,6 +352,21 @@ struct PopHeap {
};
};
template <class ValueType, class Order>
struct MinMaxElement {
size_t Quantity;
void run(benchmark::State& state) const {
runOpOnCopies<ValueType>(state, Quantity, Order(), BatchSize::CountElements, [](auto& Copy) {
benchmark::DoNotOptimize(std::minmax_element(Copy.begin(), Copy.end()));
});
}
std::string name() const {
return "BM_MinMaxElement" + ValueType::name() + Order::name() + "_" + std::to_string(Quantity);
}
};
} // namespace
int main(int argc, char** argv) {
@ -376,5 +391,6 @@ int main(int argc, char** argv) {
Quantities);
makeCartesianProductBenchmark<PushHeap, AllValueTypes, AllOrders>(Quantities);
makeCartesianProductBenchmark<PopHeap, AllValueTypes>(Quantities);
makeCartesianProductBenchmark<MinMaxElement, AllValueTypes, AllOrders>(Quantities);
benchmark::RunSpecifiedBenchmarks();
}

View File

@ -139,7 +139,7 @@
"`3144 <https://wg21.link/LWG3144>`__","``span``\ does not have a ``const_pointer``\ typedef","Kona","|Complete|",""
"`3173 <https://wg21.link/LWG3173>`__","Enable CTAD for ``ref-view``\ ","Kona","","","|ranges|"
"`3179 <https://wg21.link/LWG3179>`__","``subrange``\ should always model ``Range``\ ","Kona","","","|ranges|"
"`3180 <https://wg21.link/LWG3180>`__","Inconsistently named return type for ``ranges::minmax_element``\ ","Kona","","","|ranges|"
"`3180 <https://wg21.link/LWG3180>`__","Inconsistently named return type for ``ranges::minmax_element``\ ","Kona","|Complete|","15.0","|ranges|"
"`3182 <https://wg21.link/LWG3182>`__","Specification of ``Same``\ could be clearer","Kona","",""
"","","","",""
"`2899 <https://wg21.link/LWG2899>`__","``is_(nothrow_)move_constructible``\ and ``tuple``\ , ``optional``\ and ``unique_ptr``\ ","Cologne","",""

Can't render this file because it has a wrong number of fields in line 2.

View File

@ -17,10 +17,10 @@ Search,equal_range,Christopher Di Bella,n/a,Not started
Search,binary_search,Christopher Di Bella,n/a,Not started
Search,min,Nikolas Klauser,`D119589 <https://llvm.org/D119589>`_,
Search,max,Nikolas Klauser,`D122002 <https://llvm.org/D122002>`_,
Search,minmax,Not assigned,n/a,Not started
Search,minmax,Nikolas Klauser,`D120637 <https://llvm.org/D120637>`_,
Search,min_element,Nikolas Klauser,`D117025 <https://llvm.org/D117025>`_,
Search,max_element,Nikolas Klauser,`D117523 <https://llvm.org/D117523>`_,
Search,minmax_element,Not assigned,n/a,Not started
Search,minmax_element,Nikolas Klauser,`D120637 <https://llvm.org/D120637>`_,
Search,count,Nikolas Klauser, `D121523 <https://llvm.org/D121523>`_,
Search,count_if,Nikolas Klauser, `D121523 <https://llvm.org/D121523>`_,
Search,search,Not assigned,n/a,Not started

1 Category,Algorithm,Assignee,CL,Complete
17 Search,binary_search,Christopher Di Bella,n/a,Not started
18 Search,min,Nikolas Klauser,`D119589 <https://llvm.org/D119589>`_,✅
19 Search,max,Nikolas Klauser,`D122002 <https://llvm.org/D122002>`_,✅
20 Search,minmax,Not assigned,n/a,Not started Search,minmax,Nikolas Klauser,`D120637 <https://llvm.org/D120637>`_,✅
21 Search,min_element,Nikolas Klauser,`D117025 <https://llvm.org/D117025>`_,✅
22 Search,max_element,Nikolas Klauser,`D117523 <https://llvm.org/D117523>`_,✅
23 Search,minmax_element,Not assigned,n/a,Not started Search,minmax_element,Nikolas Klauser,`D120637 <https://llvm.org/D120637>`_,✅
24 Search,count,Nikolas Klauser, `D121523 <https://llvm.org/D121523>`_,✅
25 Search,count_if,Nikolas Klauser, `D121523 <https://llvm.org/D121523>`_,✅
26 Search,search,Not assigned,n/a,Not started

View File

@ -75,6 +75,8 @@ set(files
__algorithm/ranges_max_element.h
__algorithm/ranges_min.h
__algorithm/ranges_min_element.h
__algorithm/ranges_minmax.h
__algorithm/ranges_minmax_element.h
__algorithm/ranges_mismatch.h
__algorithm/ranges_swap_ranges.h
__algorithm/ranges_transform.h

View File

@ -10,7 +10,10 @@
#define _LIBCPP___ALGORITHM_MINMAX_H
#include <__algorithm/comp.h>
#include <__algorithm/minmax_element.h>
#include <__config>
#include <__functional/identity.h>
#include <__type_traits/is_callable.h>
#include <__utility/pair.h>
#include <initializer_list>
@ -36,47 +39,18 @@ _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
pair<const _Tp&, const _Tp&>
minmax(const _Tp& __a, const _Tp& __b)
{
return _VSTD::minmax(__a, __b, __less<_Tp>());
return std::minmax(__a, __b, __less<_Tp>());
}
#ifndef _LIBCPP_CXX03_LANG
template<class _Tp, class _Compare>
_LIBCPP_NODISCARD_EXT inline
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
pair<_Tp, _Tp>
minmax(initializer_list<_Tp> __t, _Compare __comp)
{
typedef typename initializer_list<_Tp>::const_iterator _Iter;
_Iter __first = __t.begin();
_Iter __last = __t.end();
pair<_Tp, _Tp> __result(*__first, *__first);
++__first;
if (__t.size() % 2 == 0)
{
if (__comp(*__first, __result.first))
__result.first = *__first;
else
__result.second = *__first;
++__first;
}
while (__first != __last)
{
_Tp __prev = *__first++;
if (__comp(*__first, __prev)) {
if ( __comp(*__first, __result.first)) __result.first = *__first;
if (!__comp(__prev, __result.second)) __result.second = __prev;
}
else {
if ( __comp(__prev, __result.first)) __result.first = __prev;
if (!__comp(*__first, __result.second)) __result.second = *__first;
}
__first++;
}
return __result;
_LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
pair<_Tp, _Tp> minmax(initializer_list<_Tp> __t, _Compare __comp) {
static_assert(__is_callable<_Compare, _Tp, _Tp>::value, "The comparator has to be callable");
__identity __proj;
auto __ret = std::__minmax_element_impl(__t.begin(), __t.end(), __comp, __proj);
return pair<_Tp, _Tp>(*__ret.first, *__ret.second);
}
template<class _Tp>
@ -85,7 +59,7 @@ _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
pair<_Tp, _Tp>
minmax(initializer_list<_Tp> __t)
{
return _VSTD::minmax(__t, __less<_Tp>());
return std::minmax(__t, __less<_Tp>());
}
#endif // _LIBCPP_CXX03_LANG

View File

@ -11,8 +11,10 @@
#include <__algorithm/comp.h>
#include <__config>
#include <__functional/identity.h>
#include <__iterator/iterator_traits.h>
#include <__utility/pair.h>
#include <type_traits>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
@ -20,64 +22,78 @@
_LIBCPP_BEGIN_NAMESPACE_STD
template <class _ForwardIterator, class _Compare>
_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX11
pair<_ForwardIterator, _ForwardIterator>
minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
{
static_assert(__is_cpp17_forward_iterator<_ForwardIterator>::value,
"std::minmax_element requires a ForwardIterator");
pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
if (__first != __last)
{
if (++__first != __last)
{
if (__comp(*__first, *__result.first))
__result.first = __first;
else
__result.second = __first;
while (++__first != __last)
{
_ForwardIterator __i = __first;
if (++__first == __last)
{
if (__comp(*__i, *__result.first))
__result.first = __i;
else if (!__comp(*__i, *__result.second))
__result.second = __i;
break;
}
else
{
if (__comp(*__first, *__i))
{
if (__comp(*__first, *__result.first))
__result.first = __first;
if (!__comp(*__i, *__result.second))
__result.second = __i;
}
else
{
if (__comp(*__i, *__result.first))
__result.first = __i;
if (!__comp(*__first, *__result.second))
__result.second = __first;
}
}
}
}
template <class _Comp, class _Proj>
class _MinmaxElementLessFunc {
_Comp& __comp;
_Proj& __proj;
public:
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
_MinmaxElementLessFunc(_Comp& __comp_, _Proj& __proj_) : __comp(__comp_), __proj(__proj_) {}
template <class _Iter>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
bool operator()(_Iter& __it1, _Iter& __it2) {
return std::__invoke(__comp, std::__invoke(__proj, *__it1), std::__invoke(__proj, *__it2));
}
};
template <class _Iter, class _Sent, class _Proj, class _Comp>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
pair<_Iter, _Iter> __minmax_element_impl(_Iter __first, _Sent __last, _Comp& __comp, _Proj& __proj) {
auto __less = _MinmaxElementLessFunc<_Comp, _Proj>(__comp, __proj);
pair<_Iter, _Iter> __result(__first, __first);
if (__first == __last || ++__first == __last)
return __result;
if (__less(__first, __result.first))
__result.first = __first;
else
__result.second = __first;
while (++__first != __last) {
_Iter __i = __first;
if (++__first == __last) {
if (__less(__i, __result.first))
__result.first = __i;
else if (!__less(__i, __result.second))
__result.second = __i;
return __result;
}
if (__less(__first, __i)) {
if (__less(__first, __result.first))
__result.first = __first;
if (!__less(__i, __result.second))
__result.second = __i;
} else {
if (__less(__i, __result.first))
__result.first = __i;
if (!__less(__first, __result.second))
__result.second = __first;
}
}
return __result;
}
template <class _ForwardIterator>
_LIBCPP_NODISCARD_EXT inline
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
template <class _ForwardIterator, class _Compare>
_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX11
pair<_ForwardIterator, _ForwardIterator>
minmax_element(_ForwardIterator __first, _ForwardIterator __last)
{
return _VSTD::minmax_element(__first, __last,
__less<typename iterator_traits<_ForwardIterator>::value_type>());
minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) {
static_assert(__is_cpp17_forward_iterator<_ForwardIterator>::value,
"std::minmax_element requires a ForwardIterator");
static_assert(__is_callable<_Compare, decltype(*__first), decltype(*__first)>::value,
"The comparator has to be callable");
auto __proj = __identity();
return std::__minmax_element_impl(__first, __last, __comp, __proj);
}
template <class _ForwardIterator>
_LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
pair<_ForwardIterator, _ForwardIterator> minmax_element(_ForwardIterator __first, _ForwardIterator __last) {
return std::minmax_element(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
}
_LIBCPP_END_NAMESPACE_STD

View File

@ -0,0 +1,128 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef _LIBCPP___ALGORITHM_RANGES_MINMAX_H
#define _LIBCPP___ALGORITHM_RANGES_MINMAX_H
#include <__algorithm/min_max_result.h>
#include <__algorithm/minmax_element.h>
#include <__assert>
#include <__concepts/copyable.h>
#include <__config>
#include <__functional/identity.h>
#include <__functional/invoke.h>
#include <__functional/ranges_operations.h>
#include <__iterator/concepts.h>
#include <__iterator/projected.h>
#include <__ranges/access.h>
#include <__ranges/concepts.h>
#include <__utility/forward.h>
#include <__utility/move.h>
#include <initializer_list>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif
#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
_LIBCPP_BEGIN_NAMESPACE_STD
namespace ranges {
template <class _T1>
using minmax_result = min_max_result<_T1>;
namespace __minmax {
struct __fn {
template <class _Type, class _Proj = identity,
indirect_strict_weak_order<projected<const _Type*, _Proj>> _Comp = ranges::less>
_LIBCPP_HIDE_FROM_ABI constexpr ranges::minmax_result<const _Type&>
operator()(const _Type& __a, const _Type& __b, _Comp __comp = {}, _Proj __proj = {}) const {
if (std::invoke(__comp, std::invoke(__proj, __b), std::invoke(__proj, __a)))
return {__b, __a};
return {__a, __b};
}
template <copyable _Type, class _Proj = identity,
indirect_strict_weak_order<projected<const _Type*, _Proj>> _Comp = ranges::less>
_LIBCPP_HIDE_FROM_ABI constexpr
ranges::minmax_result<_Type> operator()(initializer_list<_Type> __il, _Comp __comp = {}, _Proj __proj = {}) const {
_LIBCPP_ASSERT(__il.begin() != __il.end(), "initializer_list has to contain at least one element");
auto __iters = std::__minmax_element_impl(__il.begin(), __il.end(), __comp, __proj);
return ranges::minmax_result<_Type> { *__iters.first, *__iters.second };
}
template <input_range _Range, class _Proj = identity,
indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>> _Comp = ranges::less>
requires indirectly_copyable_storable<iterator_t<_Range>, range_value_t<_Range>*>
_LIBCPP_HIDE_FROM_ABI constexpr
ranges::minmax_result<range_value_t<_Range>> operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const {
auto __first = ranges::begin(__r);
auto __last = ranges::end(__r);
using _ValueT = range_value_t<_Range>;
_LIBCPP_ASSERT(__first != __last, "range has to contain at least one element");
if constexpr (forward_range<_Range>) {
auto __result = std::__minmax_element_impl(__first, __last, __comp, __proj);
return {*__result.first, *__result.second};
} else {
// input_iterators can't be copied, so the implementation for input_iterators has to store
// the values instead of a pointer to the correct values
auto __less = [&](auto&& __a, auto&& __b) -> bool {
return std::invoke(__comp, std::invoke(__proj, std::forward<decltype(__a)>(__a)),
std::invoke(__proj, std::forward<decltype(__b)>(__b)));
};
ranges::minmax_result<_ValueT> __result = {*__first, __result.min};
if (__first == __last || ++__first == __last)
return __result;
if (__less(*__first, __result.min))
__result.min = *__first;
else
__result.max = *__first;
while (++__first != __last) {
_ValueT __i = *__first;
if (++__first == __last) {
if (__less(__i, __result.min))
__result.min = __i;
else if (!__less(__i, __result.max))
__result.max = __i;
return __result;
}
if (__less(*__first, __i)) {
if (__less(*__first, __result.min))
__result.min = *__first;
if (!__less(__i, __result.max))
__result.max = std::move(__i);
} else {
if (__less(__i, __result.min))
__result.min = std::move(__i);
if (!__less(*__first, __result.max))
__result.max = *__first;
}
}
return __result;
}
}
};
} // namespace __minmax
inline namespace __cpo {
inline constexpr auto minmax = __minmax::__fn{};
} // namespace __cpo
} // namespace ranges
_LIBCPP_END_NAMESPACE_STD
#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
#endif // _LIBCPP___ALGORITHM_RANGES_MINMAX_H

View File

@ -0,0 +1,72 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef _LIBCPP___ALGORITHM_RANGES_MINMAX_ELEMENT_H
#define _LIBCPP___ALGORITHM_RANGES_MINMAX_ELEMENT_H
#include <__algorithm/min_max_result.h>
#include <__algorithm/minmax_element.h>
#include <__config>
#include <__functional/identity.h>
#include <__functional/invoke.h>
#include <__functional/ranges_operations.h>
#include <__iterator/concepts.h>
#include <__iterator/projected.h>
#include <__ranges/access.h>
#include <__ranges/concepts.h>
#include <__ranges/dangling.h>
#include <__utility/forward.h>
#include <__utility/move.h>
#include <__utility/pair.h>
#include <type_traits>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif
#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
_LIBCPP_BEGIN_NAMESPACE_STD
namespace ranges {
template <class _T1>
using minmax_element_result = min_max_result<_T1>;
namespace __minmax_element {
struct __fn {
template <forward_iterator _Ip, sentinel_for<_Ip> _Sp, class _Proj = identity,
indirect_strict_weak_order<projected<_Ip, _Proj>> _Comp = ranges::less>
_LIBCPP_HIDE_FROM_ABI constexpr
ranges::minmax_element_result<_Ip> operator()(_Ip __first, _Sp __last, _Comp __comp = {}, _Proj __proj = {}) const {
auto __ret = std::__minmax_element_impl(std::move(__first), std::move(__last), __comp, __proj);
return {__ret.first, __ret.second};
}
template <forward_range _Rp, class _Proj = identity,
indirect_strict_weak_order<projected<iterator_t<_Rp>, _Proj>> _Comp = ranges::less>
_LIBCPP_HIDE_FROM_ABI constexpr
ranges::minmax_element_result<borrowed_iterator_t<_Rp>>
operator()(_Rp&& __r, _Comp __comp = {}, _Proj __proj = {}) const {
auto __ret = std::__minmax_element_impl(ranges::begin(__r), ranges::end(__r), __comp, __proj);
return {__ret.first, __ret.second};
}
};
} // namespace __minmax_element
inline namespace __cpo {
inline constexpr auto minmax_element = __minmax_element::__fn{};
} // namespace __cpo
} // namespace ranges
_LIBCPP_END_NAMESPACE_STD
#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
#endif // _LIBCPP___ALGORITHM_RANGES_MINMAX_H

View File

@ -19,6 +19,15 @@
_LIBCPP_BEGIN_NAMESPACE_STD
struct __identity {
template <class _Tp>
_LIBCPP_NODISCARD _LIBCPP_CONSTEXPR _Tp&& operator()(_Tp&& __t) const _NOEXCEPT {
return std::forward<_Tp>(__t);
}
using is_transparent = void;
};
#if _LIBCPP_STD_VER > 17
struct identity {

View File

@ -165,6 +165,32 @@ namespace ranges {
indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
constexpr range_difference_t<R>
count_if(R&& r, Pred pred, Proj proj = {}); // since C++20
template<class T, class Proj = identity,
indirect_strict_weak_order<projected<const T*, Proj>> Comp = ranges::less>
constexpr ranges::minmax_result<const T&>
minmax(const T& a, const T& b, Comp comp = {}, Proj proj = {}); // since C++20
template<copyable T, class Proj = identity,
indirect_strict_weak_order<projected<const T*, Proj>> Comp = ranges::less>
constexpr ranges::minmax_result<T>
minmax(initializer_list<T> r, Comp comp = {}, Proj proj = {}); // since C++20
template<input_range R, class Proj = identity,
indirect_strict_weak_order<projected<iterator_t<R>, Proj>> Comp = ranges::less>
requires indirectly_copyable_storable<iterator_t<R>, range_value_t<R>*>
constexpr ranges::minmax_result<range_value_t<R>>
minmax(R&& r, Comp comp = {}, Proj proj = {}); // since C++20
template<forward_iterator I, sentinel_for<I> S, class Proj = identity,
indirect_strict_weak_order<projected<I, Proj>> Comp = ranges::less>
constexpr ranges::minmax_element_result<I>
minmax_element(I first, S last, Comp comp = {}, Proj proj = {}); // since C++20
template<forward_range R, class Proj = identity,
indirect_strict_weak_order<projected<iterator_t<R>, Proj>> Comp = ranges::less>
constexpr ranges::minmax_element_result<borrowed_iterator_t<R>>
minmax_element(R&& r, Comp comp = {}, Proj proj = {}); // since C++20
}
constexpr bool // constexpr in C++20
@ -891,6 +917,8 @@ template <class BidirectionalIterator, class Compare>
#include <__algorithm/ranges_max_element.h>
#include <__algorithm/ranges_min.h>
#include <__algorithm/ranges_min_element.h>
#include <__algorithm/ranges_minmax.h>
#include <__algorithm/ranges_minmax_element.h>
#include <__algorithm/ranges_mismatch.h>
#include <__algorithm/ranges_swap_ranges.h>
#include <__algorithm/ranges_transform.h>

View File

@ -303,6 +303,8 @@ module std [system] {
module ranges_max_element { private header "__algorithm/ranges_max_element.h" }
module ranges_min { private header "__algorithm/ranges_min.h" }
module ranges_min_element { private header "__algorithm/ranges_min_element.h" }
module ranges_minmax { private header "__algorithm/ranges_minmax.h" }
module ranges_minmax_element { private header "__algorithm/ranges_minmax_element.h" }
module ranges_mismatch { private header "__algorithm/ranges_mismatch.h" }
module ranges_swap_ranges { private header "__algorithm/ranges_swap_ranges.h" }
module ranges_transform { private header "__algorithm/ranges_transform.h" }

View File

@ -416,7 +416,6 @@ namespace std
}
*/
#include <__assert> // all public C++ headers provide the assertion handler
#include <__config>
#include <__type_traits/integral_constant.h>
@ -3568,6 +3567,15 @@ struct __invoke_of
{
};
#else
// Assume that it's a functor in C++03
template <class _Func, class... _Args>
_LIBCPP_HIDE_FROM_ABI
decltype(std::declval<_Func>()(std::declval<_Args>()...)) __invoke(_Func&& __func, _Args&&... __args) {
return static_cast<_Func&&>(__func)(static_cast<_Args&&>(__args)...);
}
#endif // _LIBCPP_CXX03_LANG
// result_of

View File

@ -0,0 +1,30 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// <algorithm>
// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-has-no-incomplete-ranges
// UNSUPPORTED: windows
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_ASSERTIONS=1
#include <algorithm>
#include <array>
#include "check_assertion.h"
int main(int, char**) {
std::initializer_list<int> init_list{};
TEST_LIBCPP_ASSERT_FAILURE(std::ranges::minmax(init_list),
"initializer_list has to contain at least one element");
TEST_LIBCPP_ASSERT_FAILURE(std::ranges::minmax(std::array<int, 0>{}),
"range has to contain at least one element");
return 0;
}

View File

@ -86,7 +86,7 @@ constexpr bool all_the_algorithms()
void **first2 = b;
//void **mid2 = b+5;
void **last2 = b+10;
//void *value = nullptr;
void *value = nullptr;
//int count = 1;
int copies = 0;
@ -158,11 +158,11 @@ constexpr bool all_the_algorithms()
//(void)std::ranges::min(a, Less(&copies)); assert(copies == 0);
(void)std::ranges::min_element(first, last, Less(&copies)); assert(copies == 0);
(void)std::ranges::min_element(a, Less(&copies)); assert(copies == 0);
//(void)std::ranges::minmax(value, value, Less(&copies)); assert(copies == 0);
//(void)std::ranges::minmax({ value, value }, Less(&copies)); assert(copies == 0);
//(void)std::ranges::minmax(a, Less(&copies)); assert(copies == 0);
//(void)std::ranges::minmax_element(first, last, Less(&copies)); assert(copies == 0);
//(void)std::ranges::minmax_element(a, Less(&copies)); assert(copies == 0);
(void)std::ranges::minmax(value, value, Less(&copies)); assert(copies == 0);
(void)std::ranges::minmax({ value, value }, Less(&copies)); assert(copies == 0);
(void)std::ranges::minmax(a, Less(&copies)); assert(copies == 0);
(void)std::ranges::minmax_element(first, last, Less(&copies)); assert(copies == 0);
(void)std::ranges::minmax_element(a, Less(&copies)); assert(copies == 0);
(void)std::ranges::mismatch(first, last, first2, last2, Equal(&copies)); assert(copies == 0);
(void)std::ranges::mismatch(a, b, Equal(&copies)); assert(copies == 0);
//(void)std::ranges::next_permutation(first, last, Less(&copies)); assert(copies == 0);

View File

@ -141,11 +141,11 @@ constexpr bool all_the_algorithms()
//(void)std::ranges::min(a, Less(), Proj(&copies)); assert(copies == 0);
(void)std::ranges::min_element(first, last, Less(), Proj(&copies)); assert(copies == 0);
(void)std::ranges::min_element(a, Less(), Proj(&copies)); assert(copies == 0);
//(void)std::ranges::minmax(T(), T(), Less(), Proj(&copies)); assert(copies == 0);
//(void)std::ranges::minmax({ T(), T() }, Less(), Proj(&copies)); assert(copies == 0);
//(void)std::ranges::minmax(a, Less(), Proj(&copies)); assert(copies == 0);
//(void)std::ranges::minmax_element(first, last, Less(), Proj(&copies)); assert(copies == 0);
//(void)std::ranges::minmax_element(a, Less(), Proj(&copies)); assert(copies == 0);
(void)std::ranges::minmax(T(), T(), Less(), Proj(&copies)); assert(copies == 0);
(void)std::ranges::minmax({ T(), T() }, Less(), Proj(&copies)); assert(copies == 0);
(void)std::ranges::minmax(a, Less(), Proj(&copies)); assert(copies == 0);
(void)std::ranges::minmax_element(first, last, Less(), Proj(&copies)); assert(copies == 0);
(void)std::ranges::minmax_element(a, Less(), Proj(&copies)); assert(copies == 0);
(void)std::ranges::mismatch(first, last, first2, last2, Equal(), Proj(&copies), Proj(&copies)); assert(copies == 0);
(void)std::ranges::mismatch(a, b, Equal(), Proj(&copies), Proj(&copies)); assert(copies == 0);
//(void)std::ranges::next_permutation(first, last, Less(), Proj(&copies)); assert(copies == 0);

View File

@ -112,6 +112,8 @@ END-SCRIPT
#include <__algorithm/ranges_max_element.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_max_element.h'}}
#include <__algorithm/ranges_min.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_min.h'}}
#include <__algorithm/ranges_min_element.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_min_element.h'}}
#include <__algorithm/ranges_minmax.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_minmax.h'}}
#include <__algorithm/ranges_minmax_element.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_minmax_element.h'}}
#include <__algorithm/ranges_mismatch.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_mismatch.h'}}
#include <__algorithm/ranges_swap_ranges.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_swap_ranges.h'}}
#include <__algorithm/ranges_transform.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_transform.h'}}

View File

@ -0,0 +1,351 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// <algorithm>
// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-has-no-incomplete-ranges
// template<class T, class Proj = identity,
// indirect_strict_weak_order<projected<const T*, Proj>> Comp = ranges::less>
// constexpr ranges::minmax_result<const T&>
// ranges::minmax(const T& a, const T& b, Comp comp = {}, Proj proj = {});
// template<copyable T, class Proj = identity,
// indirect_strict_weak_order<projected<const T*, Proj>> Comp = ranges::less>
// constexpr ranges::minmax_result<T>
// ranges::minmax(initializer_list<T> r, Comp comp = {}, Proj proj = {});
// template<input_range R, class Proj = identity,
// indirect_strict_weak_order<projected<iterator_t<R>, Proj>> Comp = ranges::less>
// requires indirectly_copyable_storable<iterator_t<R>, range_value_t<R>*>
// constexpr ranges::minmax_result<range_value_t<R>>
// ranges::minmax(R&& r, Comp comp = {}, Proj proj = {});
#include <algorithm>
#include <array>
#include <cassert>
#include <ranges>
#include "test_iterators.h"
template <class T>
concept HasMinMax = requires { std::ranges::minmax(std::declval<T>()); };
struct NoLessThanOp {};
struct NotTotallyOrdered {
int i;
bool operator<(const NotTotallyOrdered& o) const { return i < o.i; }
};
struct MoveOnly {
MoveOnly(MoveOnly&&) = default;
MoveOnly& operator=(MoveOnly&&) = default;
MoveOnly(const MoveOnly&) = delete;
};
static_assert(!HasMinMax<int>);
static_assert(HasMinMax<int (&)[10]>); // make sure HasMinMax works with an array
static_assert(!HasMinMax<NoLessThanOp (&)[10]>);
static_assert(!HasMinMax<NotTotallyOrdered (&)[10]>);
static_assert(!HasMinMax<MoveOnly (&)[10]>);
static_assert(HasMinMax<std::initializer_list<int>>); // make sure HasMinMax works with an initializer_list
static_assert(!HasMinMax<std::initializer_list<NoLessThanOp>>);
static_assert(!HasMinMax<std::initializer_list<NotTotallyOrdered>>);
static_assert(!HasMinMax<std::initializer_list<MoveOnly>>);
static_assert(std::is_same_v<std::ranges::minmax_result<int>, std::ranges::min_max_result<int>>);
static_assert(std::is_same_v<decltype(std::ranges::minmax(1, 2)), std::ranges::minmax_result<const int&>>);
constexpr void test_2_arguments() {
const int one = 1;
const int two = 2;
{
auto result = std::ranges::minmax(one, two);
assert(result.min == 1);
assert(result.max == 2);
}
{
auto result = std::ranges::minmax(two, one);
assert(result.min == 1);
assert(result.max == 2);
}
{
// test comparator
auto result = std::ranges::minmax(one, two, std::ranges::greater{});
assert(result.min == 2);
assert(result.max == 1);
}
{
// test projection
auto result = std::ranges::minmax(one, two, std::ranges::less{}, [](int i) { return i == 1 ? 10 : i; });
assert(result.min == 2);
assert(result.max == 1);
}
{
// test if std::invoke is used for the predicate
struct S {
int i;
};
S a[3] = {S{2}, S{1}, S{3}};
std::same_as<std::ranges::minmax_result<const S&>> auto ret = std::ranges::minmax(a[0], a[1], {}, &S::i);
assert(&ret.min == &a[1]);
assert(&ret.max == &a[0]);
assert(ret.min.i == 1);
assert(ret.max.i == 2);
}
{
// check that std::invoke is used for the comparator
struct S {
int i;
constexpr bool comp(S rhs) const { return i < rhs.i; }
};
S a[] = {{2}, {5}};
auto ret = std::ranges::minmax(a[0], a[1], &S::comp);
assert(ret.min.i == 2);
assert(ret.max.i == 5);
}
{
// make sure that {a, b} is returned if b is not less than a
auto r = std::ranges::minmax(one, two);
assert(&r.min == &one);
assert(&r.max == &two);
}
}
constexpr void test_initializer_list() {
{
// test projection
auto proj = [](int i) { return i == 5 ? -100 : i; };
auto ret = std::ranges::minmax({7, 6, 9, 3, 5, 1, 2, 4}, std::ranges::less{}, proj);
assert(ret.min == 5);
assert(ret.max == 9);
}
{
// test comparator
auto ret = std::ranges::minmax({7, 6, 9, 3, 5, 1, 2, 4}, std::ranges::greater{});
assert(ret.min == 9);
assert(ret.max == 1);
}
{
// check predicate and projection call counts
int compares = 0;
int projections = 0;
auto comparator = [&](int a, int b) {
++compares;
return a < b;
};
auto projection = [&](int a) {
++projections;
return a;
};
std::same_as<std::ranges::minmax_result<int>> auto ret = std::ranges::minmax({1, 2, 3}, comparator, projection);
assert(ret.min == 1);
assert(ret.max == 3);
assert(compares == 3);
assert(projections == 6);
}
{
// check that std::invoke is used for the predicate
struct S {
int i;
};
std::same_as<std::ranges::minmax_result<S>> auto ret = std::ranges::minmax({S{2}, S{1}, S{3}}, {}, &S::i);
assert(ret.min.i == 1);
assert(ret.max.i == 3);
}
{
// check that std::invoke is used for the comparator
struct S {
int i;
constexpr bool comp(S rhs) const { return i < rhs.i; }
};
auto ret = std::ranges::minmax({S {1}, S {2}, S {3}, S {4}}, &S::comp);
assert(ret.min.i == 1);
assert(ret.max.i == 4);
}
{
// check that a single element works
auto ret = std::ranges::minmax({ 1 });
assert(ret.min == 1);
assert(ret.max == 1);
}
{
// check in ascending order
auto ret = std::ranges::minmax({1, 2, 3});
assert(ret.min == 1);
assert(ret.max == 3);
}
{
// check in descending order
auto ret = std::ranges::minmax({3, 2, 1});
assert(ret.min == 1);
assert(ret.max == 3);
}
}
template <class Iter, class Sent = Iter>
constexpr void test_range_types() {
{
// test projection
int a[] = {7, 6, 9, 3, 5, 1, 2, 4};
auto proj = [](int& i) { return i == 5 ? -100 : i; };
auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 8)));
auto ret = std::ranges::minmax(range, std::ranges::less{}, proj);
assert(ret.min == 5);
assert(ret.max == 9);
}
{
// test comparator
int a[] = {7, 6, 9, 3, 5, 1, 2, 4};
auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 8)));
auto ret = std::ranges::minmax(range, std::ranges::greater{});
assert(ret.min == 9);
assert(ret.max == 1);
}
{
// check that complexity requirements are met
int compares = 0;
int projections = 0;
auto comparator = [&](int x, int y) {
++compares;
return x < y;
};
auto projection = [&](int x) {
++projections;
return x;
};
int a[] = {1, 2, 3};
auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 3)));
std::same_as<std::ranges::minmax_result<int>> auto ret = std::ranges::minmax(range, comparator, projection);
assert(ret.min == 1);
assert(ret.max == 3);
assert(compares == 3);
assert(projections == 6);
}
{
// check that a single element works
int a[] = { 1 };
auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 1)));
auto ret = std::ranges::minmax(range);
assert(ret.min == 1);
assert(ret.max == 1);
}
{
// check in ascending order
int a[] = {1, 2, 3};
auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 3)));
auto ret = std::ranges::minmax(range);
assert(ret.min == 1);
assert(ret.max == 3);
}
{
// check in descending order
int a[] = {3, 2, 1};
auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 3)));
auto ret = std::ranges::minmax(range);
assert(ret.min == 1);
assert(ret.max == 3);
}
}
constexpr void test_range() {
test_range_types<cpp20_input_iterator<int*>, sentinel_wrapper<cpp20_input_iterator<int*>>>();
test_range_types<forward_iterator<int*>>();
test_range_types<bidirectional_iterator<int*>>();
test_range_types<random_access_iterator<int*>>();
test_range_types<contiguous_iterator<int*>>();
{
// check that std::invoke is used for the predicate
struct S {
int i;
};
S b[3] = {S{2}, S{1}, S{3}};
std::same_as<std::ranges::minmax_result<S>> auto ret = std::ranges::minmax(b, {}, &S::i);
assert(ret.min.i == 1);
assert(ret.max.i == 3);
}
{
// check that std::invoke is used for the comparator
struct S {
int i;
constexpr bool comp(S rhs) const { return i < rhs.i; }
};
S a[] = {{1}, {2}, {3}, {4}};
auto ret = std::ranges::minmax(a, &S::comp);
assert(ret.min.i == 1);
assert(ret.max.i == 4);
}
{
// check that the leftmost value for min an rightmost for max are returned
struct S {
int comp;
int other;
};
S a[] = { {0, 0}, {0, 2}, {0, 1} };
auto ret = std::ranges::minmax(a, {}, &S::comp);
assert(ret.min.comp == 0);
assert(ret.max.comp == 0);
assert(ret.min.other == 0);
assert(ret.max.other == 1);
}
{
// check that an rvalue array works
int a[] = {1, 2, 3, 4};
auto ret = std::ranges::minmax(std::move(a));
assert(ret.min == 1);
assert(ret.max == 4);
}
}
constexpr bool test() {
test_2_arguments();
test_initializer_list();
test_range();
return true;
}
int main(int, char**) {
test();
static_assert(test());
{
// check that the iterator isn't moved from multiple times
std::shared_ptr<int> a[] = { std::make_shared<int>(42) };
auto range = std::ranges::subrange(std::move_iterator(a), std::move_iterator(a + 1));
auto [min, max] = std::ranges::minmax(range);
assert(a[0] == nullptr);
assert(min != nullptr);
assert(max == min);
}
return 0;
}

View File

@ -0,0 +1,316 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// <algorithm>
// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-has-no-incomplete-ranges
// template<forward_iterator I, sentinel_for<I> S, class Proj = identity,
// indirect_strict_weak_order<projected<I, Proj>> Comp = ranges::less>
// constexpr ranges::minmax_element_result<I> ranges::minmax_element(I first, S last, Comp comp = {}, Proj proj = {});
//
// template<forward_range R, class Proj = identity,
// indirect_strict_weak_order<projected<iterator_t<R>, Proj>> Comp = ranges::less>
// constexpr ranges::minmax_element_result<borrowed_iterator_t<R>>
// ranges::minmax_element(R&& r, Comp comp = {}, Proj proj = {});
#include <algorithm>
#include <array>
#include <cassert>
#include <ranges>
#include "almost_satisfies_types.h"
#include "test_iterators.h"
template <class T>
concept HasMinMaxElementR = requires(T t) { std::ranges::minmax_element(t); };
struct NoLessThanOp {};
struct NotTotallyOrdered {
int i;
bool operator<(const NotTotallyOrdered& o) const { return i < o.i; }
};
static_assert(HasMinMaxElementR<int (&)[10]>); // make sure HasMinMaxElementR works with an array
static_assert(!HasMinMaxElementR<NoLessThanOp (&)[10]>);
static_assert(!HasMinMaxElementR<NotTotallyOrdered (&)[10]>);
static_assert(HasMinMaxElementR<std::initializer_list<int>>); // make sure HasMinMaxElementR works with an initializer_list
static_assert(!HasMinMaxElementR<std::initializer_list<NoLessThanOp>>);
static_assert(!HasMinMaxElementR<std::initializer_list<NotTotallyOrdered>>);
static_assert(!HasMinMaxElementR<InputRangeNotDerivedFrom>);
static_assert(!HasMinMaxElementR<InputRangeNotIndirectlyReadable>);
static_assert(!HasMinMaxElementR<InputRangeNotInputOrOutputIterator>);
static_assert(!HasMinMaxElementR<InputRangeNotSentinelSemiregular>);
static_assert(!HasMinMaxElementR<InputRangeNotSentinelEqualityComparableWith>);
template <class It, class Sent = sentinel_wrapper<It>>
concept HasMinMaxElementIt = requires(It it, Sent sent) { std::ranges::minmax_element(it, sent); };
static_assert(HasMinMaxElementIt<int*>); // make sure HasMinMaxElementIt works
static_assert(!HasMinMaxElementIt<InputIteratorNotDerivedFrom>);
static_assert(!HasMinMaxElementIt<InputIteratorNotIndirectlyReadable>);
static_assert(!HasMinMaxElementIt<InputIteratorNotInputOrOutputIterator>);
static_assert(!HasMinMaxElementIt<int*, SentinelForNotSemiregular>);
static_assert(!HasMinMaxElementIt<int*, SentinelForNotWeaklyEqualityComparableWith>);
static_assert(std::is_same_v<std::ranges::minmax_element_result<int>, std::ranges::min_max_result<int>>);
template <class It>
constexpr void test_iterators(std::initializer_list<int> a, int expectedMin, int expectedMax) {
using Expected = std::ranges::minmax_element_result<It>;
const int* first = a.begin();
const int* last = a.end();
{
std::same_as<Expected> auto it = std::ranges::minmax_element(It(first), It(last));
assert(base(it.min) == first + expectedMin);
assert(base(it.max) == first + expectedMax);
}
{
using Sent = sentinel_wrapper<It>;
std::same_as<Expected> auto it = std::ranges::minmax_element(It(first), Sent(It(last)));
assert(base(it.min) == first + expectedMin);
assert(base(it.max) == first + expectedMax);
}
{
auto range = std::ranges::subrange(It(first), It(last));
std::same_as<Expected> auto it = std::ranges::minmax_element(range);
assert(base(it.min) == first + expectedMin);
assert(base(it.max) == first + expectedMax);
}
{
using Sent = sentinel_wrapper<It>;
auto range = std::ranges::subrange(It(first), Sent(It(last)));
std::same_as<Expected> auto it = std::ranges::minmax_element(range);
assert(base(it.min) == first + expectedMin);
assert(base(it.max) == first + expectedMax);
}
}
template <class It>
constexpr bool test_iterators() {
test_iterators<It>({}, 0, 0);
test_iterators<It>({1}, 0, 0);
test_iterators<It>({1, 2}, 0, 1);
test_iterators<It>({2, 1}, 1, 0);
test_iterators<It>({2, 1, 2}, 1, 2);
test_iterators<It>({2, 1, 1}, 1, 0);
test_iterators<It>({2, 2, 1}, 2, 1);
return true;
}
constexpr void test_borrowed_range_and_sentinel() {
int a[] = {7, 6, 1, 3, 5, 1, 2, 4};
std::ranges::minmax_element_result<int*> ret = std::ranges::minmax_element(std::views::all(a));
assert(ret.min == a + 2);
assert(ret.max == a + 0);
assert(*ret.min == 1);
assert(*ret.max == 7);
}
constexpr void test_comparator() {
int a[] = {7, 6, 9, 3, 5, 1, 2, 4};
std::ranges::minmax_element_result<int*> ret = std::ranges::minmax_element(a, std::ranges::greater{});
assert(ret.min == a + 2);
assert(ret.max == a + 5);
assert(*ret.min == 9);
assert(*ret.max == 1);
}
constexpr void test_projection() {
{
int a[] = {7, 6, 9, 3, 5, 1, 2, 4};
std::ranges::minmax_element_result<int*> ret =
std::ranges::minmax_element(a, std::ranges::less{}, [](int i) { return i == 5 ? -100 : i; });
assert(ret.min == a + 4);
assert(ret.max == a + 2);
assert(*ret.min == 5);
assert(*ret.max == 9);
}
{
int a[] = {7, 6, 9, 3, 5, 1, 2, 4};
std::ranges::minmax_element_result<int*> ret =
std::ranges::minmax_element(a, std::less<int*>{}, [](int& i) { return &i; });
assert(ret.min == a + 0);
assert(ret.max == a + 7);
assert(*ret.min == 7);
assert(*ret.max == 4);
}
}
struct Immobile {
int i;
constexpr Immobile(int i_) : i(i_) {}
Immobile(const Immobile&) = delete;
Immobile(Immobile&&) = delete;
auto operator<=>(const Immobile&) const = default;
};
constexpr void test_immobile() {
{
Immobile arr[]{1, 2, 3};
auto ret = std::ranges::minmax_element(arr);
assert(ret.min == arr + 0);
assert(ret.max == arr + 2);
}
{
Immobile arr[]{1, 2, 3};
auto ret = std::ranges::minmax_element(arr, arr + 3);
assert(ret.min == arr + 0);
assert(ret.max == arr + 2);
}
}
constexpr void test_dangling() {
int compares = 0;
int projections = 0;
auto comparator = [&](int a, int b) {
++compares;
return a < b;
};
auto projection = [&](int a) {
++projections;
return a;
};
[[maybe_unused]] std::same_as<std::ranges::minmax_element_result<std::ranges::dangling>> auto ret =
std::ranges::minmax_element(std::array{1, 2, 3}, comparator, projection);
assert(compares == 3);
assert(projections == 6);
}
constexpr bool test() {
test_iterators<forward_iterator<const int*>>();
test_iterators<bidirectional_iterator<const int*>>();
test_iterators<random_access_iterator<const int*>>();
test_iterators<contiguous_iterator<const int*>>();
test_iterators<const int*>();
test_iterators<int*>();
test_borrowed_range_and_sentinel();
test_comparator();
test_projection();
test_dangling();
{ // check that std::invoke is used
{
struct S {
int i;
};
S b[3] = {S{2}, S{1}, S{3}};
std::same_as<std::ranges::minmax_element_result<S*>> auto ret = std::ranges::minmax_element(b, {}, &S::i);
assert(ret.min->i == 1);
assert(ret.max->i == 3);
assert(ret.min == b + 1);
assert(ret.max == b + 2);
}
{
struct S {
int i;
};
S b[3] = {S{2}, S{1}, S{3}};
std::same_as<std::ranges::minmax_element_result<S*>> auto ret = std::ranges::minmax_element(b, b + 3, {}, &S::i);
assert(ret.min->i == 1);
assert(ret.max->i == 3);
assert(ret.min == b + 1);
assert(ret.max == b + 2);
}
}
{ // check that the leftmost value for min an rightmost for max are returned
{
struct S {
int comp;
int other;
};
S a[] = { {0, 0}, {0, 2}, {0, 1} };
auto ret = std::ranges::minmax_element(a, a + 3, {}, &S::comp);
assert(ret.min->comp == 0);
assert(ret.max->comp == 0);
assert(ret.min->other == 0);
assert(ret.max->other == 1);
}
{
struct S {
int comp;
int other;
};
S a[] = { {0, 0}, {0, 2}, {0, 1} };
auto ret = std::ranges::minmax_element(a, {}, &S::comp);
assert(ret.min->comp == 0);
assert(ret.max->comp == 0);
assert(ret.min->other == 0);
assert(ret.max->other == 1);
}
}
{ // check that an empty range works
{
std::array<int, 0> a = {};
auto ret = std::ranges::minmax_element(a.begin(), a.end());
assert(ret.min == a.begin());
assert(ret.max == a.begin());
}
{
std::array<int, 0> a = {};
auto ret = std::ranges::minmax_element(a);
assert(ret.min == a.begin());
assert(ret.max == a.begin());
}
}
{ // check in ascending order
{
int a[] = {1, 2, 3};
auto ret = std::ranges::minmax_element(a, a + 3);
assert(ret.min == a + 0);
assert(ret.max == a + 2);
assert(*ret.min == 1);
assert(*ret.max == 3);
}
{
int a[] = {1, 2, 3};
auto ret = std::ranges::minmax_element(a);
assert(ret.min == a + 0);
assert(ret.max == a + 2);
assert(*ret.min == 1);
assert(*ret.max == 3);
}
}
{ // check in descending order
{
int a[] = {3, 2, 1};
auto ret = std::ranges::minmax_element(a, a + 3);
assert(ret.min == a + 2);
assert(ret.max == a + 0);
assert(*ret.min == 1);
assert(*ret.max == 3);
}
{
int a[] = {3, 2, 1};
auto ret = std::ranges::minmax_element(a);
assert(ret.min == a + 2);
assert(ret.max == a + 0);
assert(*ret.min == 1);
assert(*ret.max == 3);
}
}
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}

View File

@ -101,8 +101,8 @@ static_assert(test(std::ranges::max_element, a));
//static_assert(test(std::ranges::merge, a, a, a));
static_assert(test(std::ranges::min, a));
static_assert(test(std::ranges::min_element, a));
//static_assert(test(std::ranges::minmax, a));
//static_assert(test(std::ranges::minmax_element, a));
static_assert(test(std::ranges::minmax, a));
static_assert(test(std::ranges::minmax_element, a));
static_assert(test(std::ranges::mismatch, a, a));
//static_assert(test(std::ranges::move, a, a));
//static_assert(test(std::ranges::move_backward, a, a));