diff --git a/libcudacxx/.upstream-tests/test/std/iterators/iterator.requirements/alg.req.sortable/sortable.compile.pass.cpp b/libcudacxx/.upstream-tests/test/std/iterators/iterator.requirements/alg.req.sortable/sortable.compile.pass.cpp new file mode 100644 index 0000000000..07dc03b431 --- /dev/null +++ b/libcudacxx/.upstream-tests/test/std/iterators/iterator.requirements/alg.req.sortable/sortable.compile.pass.cpp @@ -0,0 +1,55 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +// template +// concept sortable = see below; // since C++20 + +#include + +#include + +using CompInt = bool(*)(int, int); +using CompDefault = cuda::std::ranges::less; + +using AllConstraintsSatisfied = int*; +static_assert( cuda::std::permutable); +static_assert( cuda::std::indirect_strict_weak_order); +static_assert( cuda::std::sortable); +static_assert( cuda::std::indirect_strict_weak_order); +static_assert( cuda::std::sortable); + +struct Foo {}; +using Proj = int(*)(Foo); +static_assert( cuda::std::permutable); +static_assert(!cuda::std::indirect_strict_weak_order); +static_assert( cuda::std::indirect_strict_weak_order>); +static_assert(!cuda::std::sortable); +static_assert( cuda::std::sortable); +static_assert(!cuda::std::indirect_strict_weak_order); +static_assert( cuda::std::indirect_strict_weak_order>); +static_assert(!cuda::std::sortable); +static_assert( cuda::std::sortable); + +using NotPermutable = const int*; +static_assert(!cuda::std::permutable); +static_assert( cuda::std::indirect_strict_weak_order); +static_assert(!cuda::std::sortable); + +struct Empty {}; +using NoIndirectStrictWeakOrder = Empty*; +static_assert( cuda::std::permutable); +static_assert(!cuda::std::indirect_strict_weak_order); +static_assert(!cuda::std::sortable); + +int main(int, char**) +{ + return 0; +} diff --git a/libcudacxx/.upstream-tests/test/std/iterators/iterator.requirements/alg.req.sortable/sortable.subsumption.compile.pass.cpp b/libcudacxx/.upstream-tests/test/std/iterators/iterator.requirements/alg.req.sortable/sortable.subsumption.compile.pass.cpp new file mode 100644 index 0000000000..ec6c662a97 --- /dev/null +++ b/libcudacxx/.upstream-tests/test/std/iterators/iterator.requirements/alg.req.sortable/sortable.subsumption.compile.pass.cpp @@ -0,0 +1,33 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 + +// template +// concept sortable = see below; // since C++20 + +#include + +#include + +template +__host__ __device__ void test_subsumption() requires cuda::std::permutable; + +template +__host__ __device__ void test_subsumption() requires cuda::std::indirect_strict_weak_order>; + +template +__host__ __device__ constexpr bool test_subsumption() requires cuda::std::sortable { return true; } + +static_assert(test_subsumption()); + +int main(int, char**) +{ + return 0; +} diff --git a/libcudacxx/include/cuda/std/detail/libcxx/include/CMakeLists.txt b/libcudacxx/include/cuda/std/detail/libcxx/include/CMakeLists.txt index d31e72704f..a198ffe7a4 100644 --- a/libcudacxx/include/cuda/std/detail/libcxx/include/CMakeLists.txt +++ b/libcudacxx/include/cuda/std/detail/libcxx/include/CMakeLists.txt @@ -109,6 +109,7 @@ set(files __iterator/reverse_access.h __iterator/reverse_iterator.h __iterator/size.h + __iterator/sortable.h __iterator/wrap_iter.h __libcpp_version __locale diff --git a/libcudacxx/include/cuda/std/detail/libcxx/include/__iterator/sortable.h b/libcudacxx/include/cuda/std/detail/libcxx/include/__iterator/sortable.h new file mode 100644 index 0000000000..17966f1cc3 --- /dev/null +++ b/libcudacxx/include/cuda/std/detail/libcxx/include/__iterator/sortable.h @@ -0,0 +1,53 @@ +// -*- 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 +// SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCUDACXX___ITERATOR_SORTABLE_H +#define _LIBCUDACXX___ITERATOR_SORTABLE_H + +#ifndef __cuda_std__ +#include <__config> +#endif // __cuda_std__ + +#include "../__functional/identity.h" +#include "../__functional/ranges_operations.h" +#include "../__iterator/concepts.h" +#include "../__iterator/permutable.h" +#include "../__iterator/projected.h" + +#if defined(_LIBCUDACXX_USE_PRAGMA_GCC_SYSTEM_HEADER) +#pragma GCC system_header +#endif + +_LIBCUDACXX_BEGIN_NAMESPACE_STD + +#if _LIBCUDACXX_STD_VER > 17 + +template +concept sortable = + permutable<_Iter> && + indirect_strict_weak_order<_Comp, projected<_Iter, _Proj>>; + +#elif _LIBCUDACXX_STD_VER > 14 + +template +_LIBCUDACXX_CONCEPT_FRAGMENT( + __sortable_, + requires() // + (requires(permutable<_Iter>), + requires(indirect_strict_weak_order<_Comp, projected<_Iter, _Proj>>))); + +template +_LIBCUDACXX_CONCEPT sortable = _LIBCUDACXX_FRAGMENT(__sortable_, _Iter, _Comp, _Proj); + +#endif // _LIBCUDACXX_STD_VER > 14 + +_LIBCUDACXX_END_NAMESPACE_STD + +#endif // _LIBCUDACXX___ITERATOR_SORTABLE_H diff --git a/libcudacxx/include/cuda/std/detail/libcxx/include/iterator b/libcudacxx/include/cuda/std/detail/libcxx/include/iterator index 5ebd60d861..fb026ab91f 100644 --- a/libcudacxx/include/cuda/std/detail/libcxx/include/iterator +++ b/libcudacxx/include/cuda/std/detail/libcxx/include/iterator @@ -710,6 +710,7 @@ template constexpr const E* data(initializer_list il) noexcept; #include "__iterator/reverse_access.h" #include "__iterator/reverse_iterator.h" #include "__iterator/size.h" +#include "__iterator/sortable.h" #include "__iterator/wrap_iter.h" #include "__memory/addressof.h" #include "__memory/pointer_traits.h" diff --git a/libcudacxx/libcxx/test/std/iterators/iterator.requirements/alg.req.sortable/sortable.compile.pass.cpp b/libcudacxx/libcxx/test/std/iterators/iterator.requirements/alg.req.sortable/sortable.compile.pass.cpp new file mode 100644 index 0000000000..2a2c828500 --- /dev/null +++ b/libcudacxx/libcxx/test/std/iterators/iterator.requirements/alg.req.sortable/sortable.compile.pass.cpp @@ -0,0 +1,53 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +// template +// concept sortable = see below; // since C++20 + +#include + +#include + +using CompInt = bool(*)(int, int); +using CompDefault = std::ranges::less; + +using AllConstraintsSatisfied = int*; +static_assert( std::permutable); +static_assert( std::indirect_strict_weak_order); +static_assert( std::sortable); +static_assert( std::indirect_strict_weak_order); +static_assert( std::sortable); + +struct Foo {}; +using Proj = int(*)(Foo); +static_assert( std::permutable); +static_assert(!std::indirect_strict_weak_order); +static_assert( std::indirect_strict_weak_order>); +static_assert(!std::sortable); +static_assert( std::sortable); +static_assert(!std::indirect_strict_weak_order); +static_assert( std::indirect_strict_weak_order>); +static_assert(!std::sortable); +static_assert( std::sortable); + +using NotPermutable = const int*; +static_assert(!std::permutable); +static_assert( std::indirect_strict_weak_order); +static_assert(!std::sortable); + +struct Empty {}; +using NoIndirectStrictWeakOrder = Empty*; +static_assert( std::permutable); +static_assert(!std::indirect_strict_weak_order); +static_assert(!std::sortable); + +int main(int, char**) { + return 0; +} diff --git a/libcudacxx/libcxx/test/std/iterators/iterator.requirements/alg.req.sortable/sortable.subsumption.compile.pass.cpp b/libcudacxx/libcxx/test/std/iterators/iterator.requirements/alg.req.sortable/sortable.subsumption.compile.pass.cpp new file mode 100644 index 0000000000..8cd5e48d89 --- /dev/null +++ b/libcudacxx/libcxx/test/std/iterators/iterator.requirements/alg.req.sortable/sortable.subsumption.compile.pass.cpp @@ -0,0 +1,29 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +// template +// concept sortable = see below; // since C++20 + +#include + +#include + +template void test_subsumption() requires std::permutable; + +template void test_subsumption() + requires std::indirect_strict_weak_order>; + +template constexpr bool test_subsumption() requires std::sortable { return true; } + +static_assert(test_subsumption()); + +int main(int, char**) { + return 0; +}