Skip to content

Commit 92b7277

Browse files
committed
Add std::copy_n
1 parent f0809ee commit 92b7277

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ It is not meant as a full implementation, only the basics are expected to be cov
1616
Supports C++11 and higher, C++17 preferred.
1717
Tested in CI on GCC 7+, Clang/LLVM 5+, Apple Clang, MSVC.
1818

19-
## Implemented algorithms
19+
## Implemented Algorithms
2020
Algorithms are added on an as-needed basis. If you need one [open an issue](https://github.com/alugowski/poolSTL/issues) or contribute a PR.
2121

2222
### `<algorithm>`
2323
* [all_of](https://en.cppreference.com/w/cpp/algorithm/all_of), [any_of](https://en.cppreference.com/w/cpp/algorithm/any_of), [none_of](https://en.cppreference.com/w/cpp/algorithm/none_of)
24-
* [copy](https://en.cppreference.com/w/cpp/algorithm/copy)
24+
* [copy](https://en.cppreference.com/w/cpp/algorithm/copy), [copy_n](https://en.cppreference.com/w/cpp/algorithm/copy_n)
2525
* [fill](https://en.cppreference.com/w/cpp/algorithm/fill), [fill_n](https://en.cppreference.com/w/cpp/algorithm/fill_n)
2626
* [find](https://en.cppreference.com/w/cpp/algorithm/find), [find_if](https://en.cppreference.com/w/cpp/algorithm/find_if), [find_if_not](https://en.cppreference.com/w/cpp/algorithm/find_if_not)
2727
* [for_each](https://en.cppreference.com/w/cpp/algorithm/for_each), [for_each_n](https://en.cppreference.com/w/cpp/algorithm/for_each_n)
@@ -49,21 +49,21 @@ int main() {
4949
std::vector<int> v = {0, 1, 2, 3, 4, 5};
5050
auto sum = std::reduce(poolstl::par, v.cbegin(), v.cend());
5151
// ^^^^^^^^^^^^
52-
// Just add this to make your code parallel.
52+
// Add this to make your code parallel.
5353
std::cout << "Sum=" << sum << std::endl;
5454
return 0;
5555
}
5656
```
5757

58-
### Pool control
58+
### Controlling Thread Pool Size
5959

6060
The thread pool used by `poolstl::par` is managed internally by poolSTL. It is started on first use.
6161

62-
Full control over thread count, startup/shutdown, etc. with your own [thread pool](https://github.com/alugowski/task-thread-pool)
63-
and `poolstl::par_pool`:
62+
Use your own [thread pool](https://github.com/alugowski/task-thread-pool)
63+
with `poolstl::par_pool` for full control over thread count, startup/shutdown, etc.:
6464

6565
```c++
66-
task_thread_pool::task_thread_pool pool;
66+
task_thread_pool::task_thread_pool pool{4}; // 4 threads
6767

6868
std::reduce(poolstl::par_pool(pool), v.cbegin(), v.cbegin());
6969
```
@@ -127,7 +127,7 @@ reduce(poolstl::par)/real_time 4.21 ms
127127
reduce(std::execution::par)/real_time 3.55 ms 3.09 ms 199
128128
```
129129

130-
# poolSTL as `std::execution::par` substitute
130+
# poolSTL as `std::execution::par` Substitute
131131
**USE AT YOUR OWN RISK!**
132132

133133
Two-line hack for missing compiler support. A no-op on compilers with support.

include/poolstl/algorithm

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ namespace std {
2525
poolstl::internal::get_futures(futures);
2626
}
2727

28+
/**
29+
* NOTE: Iterators are expected to be random access.
30+
* See std::copy_n https://en.cppreference.com/w/cpp/algorithm/copy_n
31+
*/
32+
template <class ExecPolicy, class RandIt1, class Size, class RandIt2>
33+
poolstl::internal::enable_if_poolstl_execution_policy<ExecPolicy, RandIt2>
34+
copy_n(ExecPolicy &&policy, RandIt1 first, Size n, RandIt2 dest) {
35+
if (n <= 0) {
36+
return dest;
37+
}
38+
RandIt1 last = poolstl::internal::advanced(first, n);
39+
std::copy(std::forward<ExecPolicy>(policy), first, last, dest);
40+
return poolstl::internal::advanced(dest, n);
41+
}
42+
2843
/**
2944
* NOTE: Iterators are expected to be random access.
3045
* See std::fill https://en.cppreference.com/w/cpp/algorithm/fill

tests/poolstl_test.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,25 @@ TEST_CASE("copy", "[alg][algorithm]") {
7272
}
7373
}
7474

75+
TEST_CASE("copy_n", "[alg][algorithm]") {
76+
for (auto num_threads : test_thread_counts) {
77+
ttp::task_thread_pool pool(num_threads);
78+
79+
auto vec_size = *std::max_element(test_arr_sizes.cbegin(), test_arr_sizes.cend());
80+
for (auto num_iters : test_arr_sizes) {
81+
auto source = iota_vector(num_iters);
82+
std::vector<int> dest1(vec_size);
83+
std::vector<int> dest2(vec_size);
84+
85+
std::copy_n(source.cbegin(), num_iters, dest1.begin());
86+
std::copy_n(poolstl::par_pool(pool), source.cbegin(), num_iters, dest2.begin());
87+
88+
REQUIRE(dest1 == dest2);
89+
}
90+
}
91+
}
92+
93+
7594
TEST_CASE("fill", "[alg][algorithm]") {
7695
for (auto num_threads : test_thread_counts) {
7796
ttp::task_thread_pool pool(num_threads);

0 commit comments

Comments
 (0)