Skip to content

Commit

Permalink
add stable_partition sequential implementation and add advance_and_ge…
Browse files Browse the repository at this point in the history
…t_distance
  • Loading branch information
Jedi18 committed Sep 17, 2021
1 parent c6432af commit b852d7a
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ namespace hpx { namespace traits {

template <typename Sent, typename Iter>
struct is_sentinel_for<Sent, Iter,
typename std::enable_if<is_iterator<Iter>::value &&
is_weakly_equality_comparable_with<Iter, Sent>::value>::type>
typename std::enable_if_t<is_iterator_v<Iter> &&
is_weakly_equality_comparable_with<Iter, Sent>::value>>
: std::true_type
{
};

template <typename Sent, typename Iter>
HPX_INLINE_CONSTEXPR_VARIABLE bool is_sentinel_for_v =
is_sentinel_for<Sent, Iter>::value;

///////////////////////////////////////////////////////////////////////////
#if defined(HPX_HAVE_CXX20_STD_DISABLE_SIZED_SENTINEL_FOR)
template <typename Sent, typename Iter>
Expand All @@ -50,10 +54,9 @@ namespace hpx { namespace traits {
template <typename Sent, typename Iter>
struct is_sized_sentinel_for<Sent, Iter,
typename util::always_void<
typename std::enable_if<
hpx::traits::is_sentinel_for<Sent, Iter>::value &&
std::enable_if_t<hpx::traits::is_sentinel_for<Sent, Iter>::value &&
!disable_sized_sentinel_for<typename std::remove_cv<Sent>::type,
typename std::remove_cv<Iter>::type>>::type,
typename std::remove_cv<Iter>::type>>,
typename detail::subtraction_result<Iter, Sent>::type,
typename detail::subtraction_result<Sent, Iter>::type>::type>
: std::true_type
Expand Down
1 change: 1 addition & 0 deletions libs/parallelism/algorithms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ set(algorithms_headers
hpx/parallel/algorithms/count.hpp
hpx/parallel/algorithms/destroy.hpp
hpx/parallel/algorithms/detail/accumulate.hpp
hpx/parallel/algorithms/detail/advance_and_get_distance.hpp
hpx/parallel/algorithms/detail/advance_to_sentinel.hpp
hpx/parallel/algorithms/detail/dispatch.hpp
hpx/parallel/algorithms/detail/distance.hpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ namespace hpx { namespace parallel { namespace traits {
{
};

template <typename Proj, typename Rng>
HPX_INLINE_CONSTEXPR_VARIABLE bool is_projected_range_v =
is_projected_range<Proj, Rng>::value;

///////////////////////////////////////////////////////////////////////////
template <typename Proj, typename Rng, typename Enable = void>
struct projected_range
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) 2019 Hartmut Kaiser
// Copyright (c) 2021 Akhil J Nair
//
// 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 <hpx/config.hpp>
#include <hpx/iterator_support/traits/is_iterator.hpp>
#include <hpx/iterator_support/traits/is_sentinel_for.hpp>

#include <iterator>
#include <type_traits>

namespace hpx { namespace parallel { inline namespace v1 { namespace detail {
// helper facility to both advance the iterator to the sentinel and return the
// distance
template <typename Iter, typename Sent>
constexpr inline typename std::iterator_traits<Iter>::difference_type
advance_and_get_distance(Iter& first, Sent last)
{
using difference_type =
typename std::iterator_traits<Iter>::difference_type;

// we add this since passing in random access iterators
// as begin and end might not pass the sized sentinel check
if constexpr (std::is_same_v<Iter, Sent> &&
hpx::traits::is_random_access_iterator_v<Iter>)
{
difference_type offset = last - first;
first = last;
return offset;
}

if constexpr (hpx::traits::is_sized_sentinel_for_v<Sent, Iter>)
{
difference_type offset = last - first;
std::advance(first, offset);
return offset;
}
else
{
difference_type offset = 0;
for (/**/; first != last; ++first)
{
++offset;
}
return offset;
}
}
}}}} // namespace hpx::parallel::v1::detail
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2019 Hartmut Kaiser
// Copyright (c) 2021 Akhil J Nair
//
// SPDX-License-Identifier: BSL-1.0
// Distributed under the Boost Software License, Version 1.0. (See accompanying
Expand All @@ -16,29 +17,28 @@ namespace hpx { namespace parallel { inline namespace v1 { namespace detail {
// provide implementation of std::distance supporting iterators/sentinels
template <typename InIterB, typename InIterE>
constexpr inline typename std::iterator_traits<InIterB>::difference_type
distance(InIterB first, InIterE last, std::false_type)
distance(InIterB first, InIterE last)
{
typename std::iterator_traits<InIterB>::difference_type offset = 0;
for (/**/; first != last; ++first)
// we add this since passing in random access iterators
// as begin and end might not pass the sized sentinel check
if constexpr (std::is_same_v<InIterB, InIterE> &&
hpx::traits::is_random_access_iterator_v<InIterB>)
{
++offset;
return last - first;
}
return offset;
}

template <typename RanIterB, typename RanIterE>
constexpr inline typename std::iterator_traits<RanIterB>::difference_type
distance(RanIterB first, RanIterE last, std::true_type)
{
return last - first;
}

template <typename InIterB, typename InIterE>
constexpr inline typename std::iterator_traits<InIterB>::difference_type
distance(InIterB first, InIterE last)
{
return distance(first, last,
typename hpx::traits::is_sized_sentinel_for<InIterE,
InIterB>::type{});
if constexpr (hpx::traits::is_sized_sentinel_for_v<InIterE, InIterB>)
{
return last - first;
}
else
{
typename std::iterator_traits<InIterB>::difference_type offset = 0;
for (/**/; first != last; ++first)
{
++offset;
}
return offset;
}
}
}}}} // namespace hpx::parallel::v1::detail
Loading

0 comments on commit b852d7a

Please sign in to comment.