Skip to content

Commit 773ca2b

Browse files
committed
Add transform_convert_parallelly
1 parent 0584adb commit 773ca2b

File tree

7 files changed

+67
-2
lines changed

7 files changed

+67
-2
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cmake_minimum_required(VERSION 3.14)
22

3-
project(FunctionalPlus VERSION 0.2.26)
3+
project(FunctionalPlus VERSION 0.2.27)
44

55
# ---- Warning guard ----
66

INSTALL.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ Just add a *conanfile.txt* with FunctionalPlus as a requirement and chose the ge
154154

155155
```
156156
[requires]
157-
functionalplus/0.2.26
157+
functionalplus/0.2.27
158158
159159
[generators]
160160
cmake

include/fplus/curry_instances.autogenerated_defines

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ fplus_curry_define_fn_1(apply_functions)
421421
fplus_curry_define_fn_2(apply_function_n_times)
422422
fplus_curry_define_fn_1(transform_parallelly)
423423
fplus_curry_define_fn_2(transform_parallelly_n_threads)
424+
fplus_curry_define_fn_1(transform_convert_parallelly)
424425
fplus_curry_define_fn_2(reduce_parallelly)
425426
fplus_curry_define_fn_3(reduce_parallelly_n_threads)
426427
fplus_curry_define_fn_1(reduce_1_parallelly)

include/fplus/fwd_instances.autogenerated_defines

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ fplus_fwd_define_fn_1(apply_functions)
421421
fplus_fwd_define_fn_2(apply_function_n_times)
422422
fplus_fwd_define_fn_1(transform_parallelly)
423423
fplus_fwd_define_fn_2(transform_parallelly_n_threads)
424+
fplus_fwd_define_fn_1(transform_convert_parallelly)
424425
fplus_fwd_define_fn_2(reduce_parallelly)
425426
fplus_fwd_define_fn_3(reduce_parallelly_n_threads)
426427
fplus_fwd_define_fn_1(reduce_1_parallelly)
@@ -710,6 +711,7 @@ fplus_fwd_flip_define_fn_1(shuffle)
710711
fplus_fwd_flip_define_fn_1(random_element)
711712
fplus_fwd_flip_define_fn_1(apply_functions)
712713
fplus_fwd_flip_define_fn_1(transform_parallelly)
714+
fplus_fwd_flip_define_fn_1(transform_convert_parallelly)
713715
fplus_fwd_flip_define_fn_1(reduce_1_parallelly)
714716
fplus_fwd_flip_define_fn_1(keep_if_parallelly)
715717
fplus_fwd_flip_define_fn_1(show_cont_with)

include/fplus/transform.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,35 @@ auto transform_parallelly_n_threads(std::size_t n, F f, const ContainerIn& xs)
377377
thread_results);
378378
}
379379

380+
// API search type: transform_convert_parallelly : ((a -> b), [a]) -> [b]
381+
// fwd bind count: 1
382+
// transform_convert_parallelly((*2), [1, 3, 4]) == [2, 6, 8]
383+
// Same as transform_convert, but can utilize multiple CPUs by using std::launch::async.
384+
// Only makes sense if one run of the provided function
385+
// takes enough time to justify the synchronization overhead.
386+
// One thread per container element is spawned.
387+
template <typename ContainerOut, typename F, typename ContainerIn>
388+
ContainerOut transform_convert_parallelly(F f, const ContainerIn& xs)
389+
{
390+
using X = typename ContainerIn::value_type;
391+
internal::trigger_static_asserts<internal::unary_function_tag, F, X>();
392+
auto handles = transform([&f](const X& x) {
393+
return std::async(std::launch::async, [&x, &f]() {
394+
return internal::invoke(f, x);
395+
});
396+
},
397+
xs);
398+
399+
internal::trigger_static_asserts<internal::unary_function_tag, F, typename ContainerIn::value_type>();
400+
ContainerOut ys;
401+
internal::prepare_container(ys, size_of_cont(xs));
402+
auto it = internal::get_back_inserter<ContainerOut>(ys);
403+
for (auto& handle : handles) {
404+
*it = handle.get();
405+
}
406+
return ys;
407+
}
408+
380409
// API search type: reduce_parallelly : (((a, a) -> a), a, [a]) -> a
381410
// fwd bind count: 2
382411
// reduce_parallelly((+), 0, [1, 2, 3]) == (0+1+2+3) == 6

include_all_in_one/include/fplus/fplus.hpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11414,6 +11414,35 @@ auto transform_parallelly_n_threads(std::size_t n, F f, const ContainerIn& xs)
1141411414
thread_results);
1141511415
}
1141611416

11417+
// API search type: transform_convert_parallelly : ((a -> b), [a]) -> [b]
11418+
// fwd bind count: 1
11419+
// transform_convert_parallelly((*2), [1, 3, 4]) == [2, 6, 8]
11420+
// Same as transform_convert, but can utilize multiple CPUs by using std::launch::async.
11421+
// Only makes sense if one run of the provided function
11422+
// takes enough time to justify the synchronization overhead.
11423+
// One thread per container element is spawned.
11424+
template <typename ContainerOut, typename F, typename ContainerIn>
11425+
ContainerOut transform_convert_parallelly(F f, const ContainerIn& xs)
11426+
{
11427+
using X = typename ContainerIn::value_type;
11428+
internal::trigger_static_asserts<internal::unary_function_tag, F, X>();
11429+
auto handles = transform([&f](const X& x) {
11430+
return std::async(std::launch::async, [&x, &f]() {
11431+
return internal::invoke(f, x);
11432+
});
11433+
},
11434+
xs);
11435+
11436+
internal::trigger_static_asserts<internal::unary_function_tag, F, typename ContainerIn::value_type>();
11437+
ContainerOut ys;
11438+
internal::prepare_container(ys, size_of_cont(xs));
11439+
auto it = internal::get_back_inserter<ContainerOut>(ys);
11440+
for (auto& handle : handles) {
11441+
*it = handle.get();
11442+
}
11443+
return ys;
11444+
}
11445+
1141711446
// API search type: reduce_parallelly : (((a, a) -> a), a, [a]) -> a
1141811447
// fwd bind count: 2
1141911448
// reduce_parallelly((+), 0, [1, 2, 3]) == (0+1+2+3) == 6
@@ -15159,6 +15188,7 @@ fplus_curry_define_fn_1(apply_functions)
1515915188
fplus_curry_define_fn_2(apply_function_n_times)
1516015189
fplus_curry_define_fn_1(transform_parallelly)
1516115190
fplus_curry_define_fn_2(transform_parallelly_n_threads)
15191+
fplus_curry_define_fn_1(transform_convert_parallelly)
1516215192
fplus_curry_define_fn_2(reduce_parallelly)
1516315193
fplus_curry_define_fn_3(reduce_parallelly_n_threads)
1516415194
fplus_curry_define_fn_1(reduce_1_parallelly)
@@ -15768,6 +15798,7 @@ fplus_fwd_define_fn_1(apply_functions)
1576815798
fplus_fwd_define_fn_2(apply_function_n_times)
1576915799
fplus_fwd_define_fn_1(transform_parallelly)
1577015800
fplus_fwd_define_fn_2(transform_parallelly_n_threads)
15801+
fplus_fwd_define_fn_1(transform_convert_parallelly)
1577115802
fplus_fwd_define_fn_2(reduce_parallelly)
1577215803
fplus_fwd_define_fn_3(reduce_parallelly_n_threads)
1577315804
fplus_fwd_define_fn_1(reduce_1_parallelly)
@@ -16057,6 +16088,7 @@ fplus_fwd_flip_define_fn_1(shuffle)
1605716088
fplus_fwd_flip_define_fn_1(random_element)
1605816089
fplus_fwd_flip_define_fn_1(apply_functions)
1605916090
fplus_fwd_flip_define_fn_1(transform_parallelly)
16091+
fplus_fwd_flip_define_fn_1(transform_convert_parallelly)
1606016092
fplus_fwd_flip_define_fn_1(reduce_1_parallelly)
1606116093
fplus_fwd_flip_define_fn_1(keep_if_parallelly)
1606216094
fplus_fwd_flip_define_fn_1(show_cont_with)

test/transform_test.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ TEST_CASE("transform_test - transform")
126126
REQUIRE_EQ(transform_parallelly(squareLambda, intList), IntList({ 1, 4, 4, 9, 4 }));
127127
REQUIRE_EQ(transform_parallelly_n_threads(3, squareLambda, intList), IntList({ 1, 4, 4, 9, 4 }));
128128
REQUIRE_EQ(transform_convert<IntList>(squareLambda, xs), IntList({ 1, 4, 4, 9, 4 }));
129+
REQUIRE_EQ(transform_convert_parallelly<IntList>(squareLambda, xs), IntList({ 1, 4, 4, 9, 4 }));
129130

130131
REQUIRE_EQ(transform(squareLambda, std::set<int>({ 1, 2, 3, -3 })), std::set<int>({ 1, 4, 9 }));
131132
REQUIRE_EQ(transform_inner(intTimes2, IntVectors({ { 1, 3, 4 }, { 1, 2 } })), IntVectors({ { 2, 6, 8 }, { 2, 4 } }));

0 commit comments

Comments
 (0)