From a43f622e97e08e586633905931e6a717280affa9 Mon Sep 17 00:00:00 2001 From: isidorostsa Date: Sun, 10 Sep 2023 17:00:02 +0300 Subject: [PATCH] move is_contiguous_iterator to type_support, support array iterators --- .../algorithms/traits/pointer_category.hpp | 1 + .../include/hpx/parallel/util/transfer.hpp | 1 + .../iterator_support/traits/is_iterator.hpp | 48 ----------- libs/core/type_support/CMakeLists.txt | 1 + .../type_support/is_contiguous_iterator.hpp | 80 +++++++++++++++++++ .../uninitialized_relocate_n_primitive.hpp | 7 +- .../type_support/tests/unit/CMakeLists.txt | 4 +- .../tests/unit/is_contiguous_iterator.cpp | 49 ++++++++++++ 8 files changed, 139 insertions(+), 52 deletions(-) create mode 100644 libs/core/type_support/include/hpx/type_support/is_contiguous_iterator.hpp create mode 100644 libs/core/type_support/tests/unit/is_contiguous_iterator.cpp diff --git a/libs/core/algorithms/include/hpx/algorithms/traits/pointer_category.hpp b/libs/core/algorithms/include/hpx/algorithms/traits/pointer_category.hpp index 0d59da372ae9..3550596b9507 100644 --- a/libs/core/algorithms/include/hpx/algorithms/traits/pointer_category.hpp +++ b/libs/core/algorithms/include/hpx/algorithms/traits/pointer_category.hpp @@ -8,6 +8,7 @@ #pragma once #include +#include #include #include diff --git a/libs/core/algorithms/include/hpx/parallel/util/transfer.hpp b/libs/core/algorithms/include/hpx/parallel/util/transfer.hpp index 69f2556ebb26..e63be42abf0d 100644 --- a/libs/core/algorithms/include/hpx/parallel/util/transfer.hpp +++ b/libs/core/algorithms/include/hpx/parallel/util/transfer.hpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include diff --git a/libs/core/iterator_support/include/hpx/iterator_support/traits/is_iterator.hpp b/libs/core/iterator_support/include/hpx/iterator_support/traits/is_iterator.hpp index c7a6a010358e..ab579147a272 100644 --- a/libs/core/iterator_support/include/hpx/iterator_support/traits/is_iterator.hpp +++ b/libs/core/iterator_support/include/hpx/iterator_support/traits/is_iterator.hpp @@ -564,52 +564,4 @@ namespace hpx::traits { template inline constexpr bool is_zip_iterator_v = is_zip_iterator::value; - /////////////////////////////////////////////////////////////////////////// - // Iterators are contiguous if they are pointers (without concepts we have - // no generic way of determining whether an iterator is contiguous) - - namespace detail { - - // Iterators returned from std::vector are contiguous (by definition) - - // different versions of clang-format disagree - // clang-format off - template > - struct is_vector_iterator - : std::integral_constant&>().begin()), Iter> || - std::is_same_v< - decltype(std::declval&>().cbegin()), Iter>> - { - }; - // clang-format on - } // namespace detail - - template ::value -#endif - > - struct is_contiguous_iterator : std::is_pointer::type - { - }; - - template - struct is_contiguous_iterator : std::true_type - { - }; - - template - using is_contiguous_iterator_t = - typename is_contiguous_iterator::type; - - template - inline constexpr bool is_contiguous_iterator_v = - is_contiguous_iterator::value; } // namespace hpx::traits diff --git a/libs/core/type_support/CMakeLists.txt b/libs/core/type_support/CMakeLists.txt index 5bf549a16256..ba11cca32dd8 100644 --- a/libs/core/type_support/CMakeLists.txt +++ b/libs/core/type_support/CMakeLists.txt @@ -18,6 +18,7 @@ set(type_support_headers hpx/type_support/identity.hpp hpx/type_support/is_relocatable.hpp hpx/type_support/is_trivially_relocatable.hpp + hpx/type_support/is_contiguous_iterator.hpp hpx/type_support/lazy_conditional.hpp hpx/type_support/lazy_enable_if.hpp hpx/type_support/pack.hpp diff --git a/libs/core/type_support/include/hpx/type_support/is_contiguous_iterator.hpp b/libs/core/type_support/include/hpx/type_support/is_contiguous_iterator.hpp new file mode 100644 index 000000000000..f1d5f5b4f509 --- /dev/null +++ b/libs/core/type_support/include/hpx/type_support/is_contiguous_iterator.hpp @@ -0,0 +1,80 @@ +// Copyright (c) 2007-2022 Hartmut Kaiser +// +// SPDX-License-Identifier: BSL-1.0 +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#pragma once + +#include +#include +#include +#include +#include +#include + +namespace hpx::traits { + /////////////////////////////////////////////////////////////////////////// + // Iterators are contiguous if they are pointers (without concepts we have + // no generic way of determining whether an iterator is contiguous) + + namespace detail { + + template + using iter_value_type_t = + typename std::iterator_traits::value_type; + + template + struct is_known_contiguous_iterator : std::false_type + { + }; + + template + struct is_known_contiguous_iterator>> + : std::bool_constant< + std::is_same_v< // for std::vector + typename std::vector>::iterator, + Iter> || + std::is_same_v>::const_iterator, + Iter> || // for std::array + std::is_same_v< + typename std::array, 1>::iterator, + Iter> || + std::is_same_v, + 1>::const_iterator, + Iter> || // for std::string + std::is_same_v || + std::is_same_v> + { + }; + } // namespace detail + + template ::value +#endif + > + struct is_contiguous_iterator : std::is_pointer::type + { + }; + + template + struct is_contiguous_iterator : std::true_type + { + }; + + template + using is_contiguous_iterator_t = + typename is_contiguous_iterator::type; + + template + inline constexpr bool is_contiguous_iterator_v = + is_contiguous_iterator::value; +} // namespace hpx::traits diff --git a/libs/core/type_support/include/hpx/type_support/uninitialized_relocate_n_primitive.hpp b/libs/core/type_support/include/hpx/type_support/uninitialized_relocate_n_primitive.hpp index 7d1b682feea6..ebd4bfb9ef12 100644 --- a/libs/core/type_support/include/hpx/type_support/uninitialized_relocate_n_primitive.hpp +++ b/libs/core/type_support/include/hpx/type_support/uninitialized_relocate_n_primitive.hpp @@ -6,6 +6,7 @@ #pragma once +#include #include #include #include @@ -165,6 +166,7 @@ namespace hpx::experimental::util { FwdIter uninitialized_relocate_n_primitive(InIter first, Size n, FwdIter dst, iterators_are_contiguous_t) noexcept( detail::relocation_traits::is_noexcept_relocatable_v) + // clang-format on { static_assert( detail::relocation_traits::valid_relocation, @@ -176,7 +178,6 @@ namespace hpx::experimental::util { return detail::uninitialized_relocate_n_primitive_helper( first, n, dst, implementation_tag{}); } - // clang-format on template FwdIter uninitialized_relocate_n_primitive(InIter first, Size n, @@ -184,8 +185,8 @@ namespace hpx::experimental::util { FwdIter>::is_noexcept_relocatable_v) { using iterators_are_contiguous_default_t = - std::bool_constant && - std::is_pointer_v>; + std::bool_constant && + hpx::traits::is_contiguous_iterator_v>; return uninitialized_relocate_n_primitive( first, n, dst, iterators_are_contiguous_default_t{}); diff --git a/libs/core/type_support/tests/unit/CMakeLists.txt b/libs/core/type_support/tests/unit/CMakeLists.txt index ae53059b2564..10a2ef767386 100644 --- a/libs/core/type_support/tests/unit/CMakeLists.txt +++ b/libs/core/type_support/tests/unit/CMakeLists.txt @@ -32,7 +32,9 @@ endforeach() if(HPX_WITH_COMPILE_ONLY_TESTS) # add compile time tests - set(compile_tests is_relocatable is_trivially_relocatable) + set(compile_tests is_relocatable is_trivially_relocatable + is_contiguous_iterator + ) if(HPX_WITH_FAIL_COMPILE_TESTS) set(fail_compile_tests fail_relocate_at fail_uninitialized_relocate) diff --git a/libs/core/type_support/tests/unit/is_contiguous_iterator.cpp b/libs/core/type_support/tests/unit/is_contiguous_iterator.cpp new file mode 100644 index 000000000000..add047d47069 --- /dev/null +++ b/libs/core/type_support/tests/unit/is_contiguous_iterator.cpp @@ -0,0 +1,49 @@ +// Copyright (c) 2023 Isidoros Tsaousis-Seiras +// +// SPDX-License-Identifier: BSL-1.0 +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include +#include +#include + +#include + +using hpx::traits::is_contiguous_iterator_v; + +// std::vector::iterator is a contiguous iterator +static_assert(is_contiguous_iterator_v::iterator>); +static_assert(is_contiguous_iterator_v::const_iterator>); +// reverse_iterator is not a contiguous iterator +static_assert(!is_contiguous_iterator_v::reverse_iterator>); +static_assert( + !is_contiguous_iterator_v::const_reverse_iterator>); + +// std::array::iterator is a contiguous iterator +static_assert(is_contiguous_iterator_v::iterator>); +static_assert(is_contiguous_iterator_v::const_iterator>); +// reverse_iterator is not a contiguous iterator +static_assert(!is_contiguous_iterator_v::reverse_iterator>); +static_assert( + !is_contiguous_iterator_v::const_reverse_iterator>); + +// pointers are contiguous iterators +static_assert(is_contiguous_iterator_v); +static_assert(is_contiguous_iterator_v); +static_assert(is_contiguous_iterator_v); +static_assert(is_contiguous_iterator_v); +static_assert(is_contiguous_iterator_v); +static_assert(is_contiguous_iterator_v); + +// arrays are not contiguous iterators +static_assert(!is_contiguous_iterator_v); +static_assert(!is_contiguous_iterator_v); +static_assert(!is_contiguous_iterator_v); +static_assert(!is_contiguous_iterator_v); + +// std::string::iterator is a contiguous iterator +static_assert(is_contiguous_iterator_v); +static_assert(is_contiguous_iterator_v); + +int main(int, char*[]) {}