[libcxx-commits] [libcxx] 807766b - [libc++][ranges] Add ranges::min_max_result
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Feb 21 13:52:27 PST 2022
Author: Nikolas Klauser
Date: 2022-02-21T22:52:01+01:00
New Revision: 807766be3a89e2d4c9c935db2edd8c665f4d7567
URL: https://github.com/llvm/llvm-project/commit/807766be3a89e2d4c9c935db2edd8c665f4d7567
DIFF: https://github.com/llvm/llvm-project/commit/807766be3a89e2d4c9c935db2edd8c665f4d7567.diff
LOG: [libc++][ranges] Add ranges::min_max_result
Reviewed By: Quuxplusone, #libc
Spies: libcxx-commits, mgorny
Differential Revision: https://reviews.llvm.org/D119751
Added:
libcxx/include/__algorithm/min_max_result.h
libcxx/test/libcxx/diagnostics/detail.headers/algorithm/min_max_result.module.verify.cpp
libcxx/test/std/algorithms/algorithms.results/min_max_result.pass.cpp
Modified:
libcxx/include/CMakeLists.txt
libcxx/include/algorithm
libcxx/include/module.modulemap
Removed:
################################################################################
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 72d06fc3a7532..3e5baa2d33d28 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -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
diff --git a/libcxx/include/__algorithm/min_max_result.h b/libcxx/include/__algorithm/min_max_result.h
new file mode 100644
index 0000000000000..d20d98a521d8c
--- /dev/null
+++ b/libcxx/include/__algorithm/min_max_result.h
@@ -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
diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm
index b35ad674cbe77..9149995caef7a 100644
--- a/libcxx/include/algorithm
+++ b/libcxx/include/algorithm
@@ -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>
diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap
index 890b7fef933ad..846da2f77a2d0 100644
--- a/libcxx/include/module.modulemap
+++ b/libcxx/include/module.modulemap
@@ -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" }
diff --git a/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/min_max_result.module.verify.cpp b/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/min_max_result.module.verify.cpp
new file mode 100644
index 0000000000000..69270642bdfac
--- /dev/null
+++ b/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/min_max_result.module.verify.cpp
@@ -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>
diff --git a/libcxx/test/std/algorithms/algorithms.results/min_max_result.pass.cpp b/libcxx/test/std/algorithms/algorithms.results/min_max_result.pass.cpp
new file mode 100644
index 0000000000000..b2d6081b6a88e
--- /dev/null
+++ b/libcxx/test/std/algorithms/algorithms.results/min_max_result.pass.cpp
@@ -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;
+}
More information about the libcxx-commits
mailing list