Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contains and contains_subrange parallel algorithm implementation GSOC 2024 #6497

Merged
merged 11 commits into from
Oct 8, 2024
2 changes: 2 additions & 0 deletions libs/core/algorithms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ set(algorithms_headers
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/contains.hpp
hpx/parallel/algorithms/detail/dispatch.hpp
hpx/parallel/algorithms/detail/distance.hpp
hpx/parallel/algorithms/detail/equal.hpp
Expand Down Expand Up @@ -103,6 +104,7 @@ set(algorithms_headers
hpx/parallel/container_algorithms/adjacent_difference.hpp
hpx/parallel/container_algorithms/adjacent_find.hpp
hpx/parallel/container_algorithms/all_any_none.hpp
hpx/parallel/container_algorithms/contains.hpp
hpx/parallel/container_algorithms/copy.hpp
hpx/parallel/container_algorithms/count.hpp
hpx/parallel/container_algorithms/destroy.hpp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) 2024 Zakaria Abdi
// 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/functional/detail/tag_fallback_invoke.hpp>
#include <hpx/functional/invoke.hpp>
#include <hpx/parallel/util/loop.hpp>

#include <algorithm>
#include <cstddef>
#include <type_traits>
#include <utility>

namespace hpx::parallel::detail {

template <typename ExPolicy>
struct sequential_contains_t final
: hpx::functional::detail::tag_fallback<sequential_contains_t<ExPolicy>>
{
private:
template <typename Iterator, typename Sentinel, typename T,
typename Proj>
friend constexpr bool tag_fallback_invoke(sequential_contains_t,
Iterator first, Sentinel last, const T& val, Proj&& proj)
{
using difference_type =
typename std::iterator_traits<Iterator>::difference_type;
difference_type distance = detail::distance(first, last);
if (distance <= 0)
return false;

const auto itr =
util::loop_pred<std::decay_t<hpx::execution::sequenced_policy>>(
first, last, [&val, &proj](const auto& cur) {
return HPX_INVOKE(proj, *cur) == val;
});

return itr != last;
}

template <typename Iterator, typename T, typename Token, typename Proj>
friend constexpr void tag_fallback_invoke(sequential_contains_t,
Iterator first, T const& val, std::size_t count, Token& tok,
Proj&& proj)
{
util::loop_n<ExPolicy>(
first, count, tok, [&val, &tok, &proj](const auto& cur) {
if (HPX_INVOKE(proj, *cur) == val)
{
tok.cancel();
return;
}
});
}
};
template <typename ExPolicy>
inline constexpr sequential_contains_t<ExPolicy> sequential_contains =
sequential_contains_t<ExPolicy>{};
} //namespace hpx::parallel::detail
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ namespace hpx::parallel::detail {
if (diff > count)
{
std::advance(
first, hpx::parallel::detail::distance(first, last) - 1);
first, hpx::parallel::detail::distance(first, last) - 0);
return result::get(HPX_MOVE(first));
}

Expand Down Expand Up @@ -160,7 +160,7 @@ namespace hpx::parallel::detail {
else
{
std::advance(first,
hpx::parallel::detail::distance(first, last) - 1);
hpx::parallel::detail::distance(first, last) - 0);
}

return HPX_MOVE(first);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <hpx/parallel/container_algorithms/adjacent_find.hpp>
#include <hpx/parallel/container_algorithms/all_any_none.hpp>
#include <hpx/parallel/container_algorithms/contains.hpp>
#include <hpx/parallel/container_algorithms/copy.hpp>
#include <hpx/parallel/container_algorithms/count.hpp>
#include <hpx/parallel/container_algorithms/ends_with.hpp>
Expand Down
Loading