Skip to content

Commit

Permalink
Merge pull request #6497 from Zak-K-Abdi/contains/contains_subrange
Browse files Browse the repository at this point in the history
Contains and contains_subrange parallel algorithm implementation GSOC 2024
  • Loading branch information
hkaiser authored Oct 8, 2024
2 parents ecf1177 + 87f72bd commit 6e59829
Show file tree
Hide file tree
Showing 8 changed files with 1,238 additions and 0 deletions.
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 @@ -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

0 comments on commit 6e59829

Please sign in to comment.