[libc++][ranges] Add ranges::min_max_result

Reviewed By: Quuxplusone, #libc

Spies: libcxx-commits, mgorny

Differential Revision: https://reviews.llvm.org/D119751
This commit is contained in:
Nikolas Klauser 2022-02-21 22:48:36 +01:00
parent 6766ece133
commit 807766be3a
6 changed files with 164 additions and 3 deletions

View File

@ -48,6 +48,7 @@ set(files
__algorithm/merge.h
__algorithm/min.h
__algorithm/min_element.h
__algorithm/min_max_result.h
__algorithm/minmax.h
__algorithm/minmax_element.h
__algorithm/mismatch.h

View File

@ -0,0 +1,56 @@
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// 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_MIN_MAX_RESULT_H
#define _LIBCPP___ALGORITHM_MIN_MAX_RESULT_H
#include <__concepts/convertible_to.h>
#include <__config>
#include <__utility/move.h>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif
_LIBCPP_PUSH_MACROS
#include <__undef_macros>
_LIBCPP_BEGIN_NAMESPACE_STD
#if!defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
namespace ranges {
template <class _T1>
struct min_max_result {
[[no_unique_address]] _T1 min;
[[no_unique_address]] _T1 max;
template <class _T2>
requires convertible_to<const _T1&, _T2>
_LIBCPP_HIDE_FROM_ABI constexpr operator min_max_result<_T2>() const & {
return {min, max};
}
template <class _T2>
requires convertible_to<_T1, _T2>
_LIBCPP_HIDE_FROM_ABI constexpr operator min_max_result<_T2>() && {
return {std::move(min), std::move(max)};
}
};
} // namespace ranges
#endif
_LIBCPP_END_NAMESPACE_STD
_LIBCPP_POP_MACROS
#endif

View File

@ -20,17 +20,20 @@ namespace std
namespace ranges {
template <class I, class F>
struct in_fun_result; // since C++20
struct in_fun_result; // since C++20
template <class I1, class I2>
struct in_in_result; // since C++20
struct in_in_result; // since C++20
template <class I1, class I2, class O>
struct in_in_out_result; // since C++20
struct in_in_out_result; // since C++20
template <class I, class O1, class O2>
struct in_out_out_result; // since C++20
template <class I1, class I2>
struct min_max_result; // since C++20
template<forward_iterator I, sentinel_for<I> S, class Proj = identity,
indirect_strict_weak_order<projected<I, Proj>> Comp = ranges::less> // since C++20
constexpr I min_element(I first, S last, Comp comp = {}, Proj proj = {});
@ -738,6 +741,7 @@ template <class BidirectionalIterator, class Compare>
#include <__algorithm/merge.h>
#include <__algorithm/min.h>
#include <__algorithm/min_element.h>
#include <__algorithm/min_max_result.h>
#include <__algorithm/minmax.h>
#include <__algorithm/minmax_element.h>
#include <__algorithm/mismatch.h>

View File

@ -270,6 +270,7 @@ module std [system] {
module merge { private header "__algorithm/merge.h" }
module min { private header "__algorithm/min.h" }
module min_element { private header "__algorithm/min_element.h" }
module min_max_result { private header "__algorithm/min_max_result.h" }
module minmax { private header "__algorithm/minmax.h" }
module minmax_element { private header "__algorithm/minmax_element.h" }
module mismatch { private header "__algorithm/mismatch.h" }

View File

@ -0,0 +1,15 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// REQUIRES: modules-build
// WARNING: This test was generated by 'generate_private_header_tests.py'
// and should not be edited manually.
// expected-error@*:* {{use of private header from outside its module: '__algorithm/min_max_result.h'}}
#include <__algorithm/min_max_result.h>

View File

@ -0,0 +1,84 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-no-concepts
// UNSUPPORTED: libcpp-has-no-incomplete-ranges
// template <class I1, class I2>
// struct min_max_result;
#include <algorithm>
#include <cassert>
#include <type_traits>
#include "MoveOnly.h"
template <class T>
struct ConvertibleFrom {
constexpr ConvertibleFrom(T c) : content{c} {}
T content;
};
struct A {
explicit A(int);
};
static_assert(!std::is_constructible_v<std::ranges::min_max_result<A>, std::ranges::min_max_result<int>>);
struct B {
B(const int&);
B(int&&);
};
static_assert(std::is_constructible_v<std::ranges::min_max_result<B>, std::ranges::min_max_result<int>>);
static_assert(std::is_constructible_v<std::ranges::min_max_result<B>, std::ranges::min_max_result<int>&>);
static_assert(std::is_constructible_v<std::ranges::min_max_result<B>, const std::ranges::min_max_result<int>>);
static_assert(std::is_constructible_v<std::ranges::min_max_result<B>, const std::ranges::min_max_result<int>&>);
struct C {
C(int&);
};
static_assert(!std::is_constructible_v<std::ranges::min_max_result<C>, std::ranges::min_max_result<int>&>);
static_assert(std::is_convertible_v<std::ranges::min_max_result<int>&, std::ranges::min_max_result<long>>);
static_assert(std::is_convertible_v<const std::ranges::min_max_result<int>&, std::ranges::min_max_result<long>>);
static_assert(std::is_convertible_v<std::ranges::min_max_result<int>&&, std::ranges::min_max_result<long>>);
static_assert(std::is_convertible_v<const std::ranges::min_max_result<int>&&, std::ranges::min_max_result<long>>);
struct NotConvertible {};
static_assert(!std::is_convertible_v<std::ranges::min_max_result<NotConvertible>, std::ranges::min_max_result<int>>);
constexpr bool test() {
{
std::ranges::min_max_result<double> res{10, 1};
assert(res.min == 10);
assert(res.max == 1);
std::ranges::min_max_result<ConvertibleFrom<int>> res2 = res;
assert(res2.min.content == 10);
assert(res2.max.content == 1);
}
{
std::ranges::min_max_result<MoveOnly> res{MoveOnly{}, MoveOnly{}};
assert(res.min.get() == 1);
assert(res.max.get() == 1);
[[maybe_unused]] auto res2 = static_cast<std::ranges::min_max_result<MoveOnly>>(std::move(res));
assert(res.min.get() == 0);
assert(res.max.get() == 0);
}
auto [min, max] = std::ranges::min_max_result<int>{1, 2};
assert(min == 1);
assert(max == 2);
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}