Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Tested in CI on GCC 7+, Clang/LLVM 5+, Apple Clang, MSVC.
Algorithms are added on an as-needed basis. If you need one that is not present feel free to open an issue or submit a PR.

### `<algorithm>`
* [std::all_of](https://en.cppreference.com/w/cpp/algorithm/all_of), [std::any_of](https://en.cppreference.com/w/cpp/algorithm/any_of), [std::none_of](https://en.cppreference.com/w/cpp/algorithm/none_of)
* [std::copy](https://en.cppreference.com/w/cpp/algorithm/copy)
* [std::fill](https://en.cppreference.com/w/cpp/algorithm/fill)
* [std::fill_n](https://en.cppreference.com/w/cpp/algorithm/fill_n)
Expand Down
10 changes: 5 additions & 5 deletions benchmark/algorithm_bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ void all_of(benchmark::State& state) {
auto values = iota_vector(arr_length);

for ([[maybe_unused]] auto _ : state) {
bool res;
if constexpr (is_policy<ExecPolicy>::value) {
res = std::all_of(policy<ExecPolicy>::get(), values.begin(), values.end(), [&](auto v) { return v >= 0; });
auto res = std::all_of(policy<ExecPolicy>::get(), values.begin(), values.end(), [&](auto v) { return v >= 0; });
benchmark::DoNotOptimize(res);
} else {
res = std::all_of(values.begin(), values.end(), [&](auto v) { return v >= 0; });
auto res = std::all_of(values.begin(), values.end(), [&](auto v) { return v >= 0; });
benchmark::DoNotOptimize(res);
}
benchmark::DoNotOptimize(res);
benchmark::ClobberMemory();
}
}

BENCHMARK(all_of<seq>)->Name("all_of()")->UseRealTime();
//BENCHMARK(all_of<poolstl_par>)->Name("all_of(poolstl::par)")->UseRealTime();
BENCHMARK(all_of<poolstl_par>)->Name("all_of(poolstl::par)")->UseRealTime();
#ifdef POOLSTL_BENCH_STD_PAR
BENCHMARK(all_of<std_par>)->Name("all_of(std::execution::par)")->UseRealTime();
#endif
Expand Down
29 changes: 29 additions & 0 deletions include/poolstl/algorithm
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,35 @@ namespace std {
return dest + std::distance(first1, last1);
}

/**
* NOTE: Iterators are expected to be random access.
* See std::all_of https://en.cppreference.com/w/cpp/algorithm/all_of
*/
template <class ExecPolicy, typename RandIt, typename Predicate>
poolstl::internal::enable_if_poolstl_execution_policy<ExecPolicy, bool>
all_of(ExecPolicy&& policy, RandIt first, RandIt last, Predicate pred) {
return last == std::find_if_not(std::forward<ExecPolicy>(policy), first, last, pred);
}

/**
* NOTE: Iterators are expected to be random access.
* See std::none_of https://en.cppreference.com/w/cpp/algorithm/none_of
*/
template <class ExecPolicy, typename RandIt, typename Predicate>
poolstl::internal::enable_if_poolstl_execution_policy<ExecPolicy, bool>
none_of(ExecPolicy&& policy, RandIt first, RandIt last, Predicate pred) {
return last == std::find_if(std::forward<ExecPolicy>(policy), first, last, pred);
}

/**
* NOTE: Iterators are expected to be random access.
* See std::any_of https://en.cppreference.com/w/cpp/algorithm/any_of
*/
template <class ExecPolicy, typename RandIt, typename Predicate>
poolstl::internal::enable_if_poolstl_execution_policy<ExecPolicy, bool>
any_of(ExecPolicy&& policy, RandIt first, RandIt last, Predicate pred) {
return !std::none_of(std::forward<ExecPolicy>(policy), first, last, pred);
}
}

#endif
40 changes: 40 additions & 0 deletions tests/poolstl_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,46 @@

namespace ttp = task_thread_pool;

TEST_CASE("any_all_none", "[alg][algorithm]") {
for (auto num_threads : test_thread_counts) {
ttp::task_thread_pool pool(num_threads);

for (auto vec_size : test_arr_sizes) {
auto haystack = iota_vector(vec_size);
auto half = vec_size / 2;

for (int which = 0; which <= 5; ++which) {
auto pred = [&](auto x) -> bool {
switch (which) {
case 0: return x < half;
case 1: return x > half;
case 2: return x == 1;
case 3: return true;
case 4: return false;
default: return x == -1;
}
};

{
auto seq = std::any_of(haystack.cbegin(), haystack.cend(), pred);
auto par = std::any_of(poolstl::par_pool(pool), haystack.cbegin(), haystack.cend(), pred);
REQUIRE(seq == par);
}
{
auto seq = std::all_of(haystack.cbegin(), haystack.cend(), pred);
auto par = std::all_of(poolstl::par_pool(pool), haystack.cbegin(), haystack.cend(), pred);
REQUIRE(seq == par);
}
{
auto seq = std::none_of(haystack.cbegin(), haystack.cend(), pred);
auto par = std::none_of(poolstl::par_pool(pool), haystack.cbegin(), haystack.cend(), pred);
REQUIRE(seq == par);
}
}
}
}
}

TEST_CASE("copy", "[alg][algorithm]") {
for (auto num_threads : test_thread_counts) {
ttp::task_thread_pool pool(num_threads);
Expand Down