Skip to content

Commit c113266

Browse files
committed
Address ldionne's review comments
1 parent 1541163 commit c113266

File tree

7 files changed

+23
-11
lines changed

7 files changed

+23
-11
lines changed

libcxx/include/__algorithm/for_each.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#define _LIBCPP___ALGORITHM_FOR_EACH_H
1212

1313
#include <__algorithm/for_each_segment.h>
14+
#include <__algorithm/iterator_operations.h>
1415
#include <__config>
1516
#include <__iterator/segmented_iterator.h>
1617
#include <__ranges/movable_box.h>
@@ -26,20 +27,20 @@ _LIBCPP_PUSH_MACROS
2627

2728
_LIBCPP_BEGIN_NAMESPACE_STD
2829

29-
template <class _InputIterator, class _Function>
30+
template <class, class _InputIterator, class _Function>
3031
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Function
31-
for_each(_InputIterator __first, _InputIterator __last, _Function __f) {
32+
__for_each(_InputIterator __first, _InputIterator __last, _Function& __f) {
3233
for (; __first != __last; ++__first)
3334
__f(*__first);
34-
return __f;
35+
return std::move(__f);
3536
}
3637

3738
// __movable_box is available in C++20, but is actually a copyable-box, so optimization is only correct in C++23
3839
#if _LIBCPP_STD_VER >= 23
39-
template <class _SegmentedIterator, class _Function>
40+
template <class, class _SegmentedIterator, class _Function>
4041
requires __is_segmented_iterator<_SegmentedIterator>::value
4142
_LIBCPP_HIDE_FROM_ABI constexpr _Function
42-
for_each(_SegmentedIterator __first, _SegmentedIterator __last, _Function __func) {
43+
for_each(_SegmentedIterator __first, _SegmentedIterator __last, _Function& __func) {
4344
ranges::__movable_box<_Function> __wrapped_func(in_place, std::move(__func));
4445
std::__for_each_segment(__first, __last, [&](auto __lfirst, auto __llast) {
4546
__wrapped_func =
@@ -49,6 +50,12 @@ for_each(_SegmentedIterator __first, _SegmentedIterator __last, _Function __func
4950
}
5051
#endif // _LIBCPP_STD_VER >= 23
5152

53+
template <class _InputIterator, class _Function>
54+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Function
55+
for_each(_InputIterator __first, _InputIterator __last, _Function __f) {
56+
return __for_each<_ClassicAlgPolicy>(__first, __last, __f);
57+
}
58+
5259
_LIBCPP_END_NAMESPACE_STD
5360

5461
_LIBCPP_POP_MACROS

libcxx/include/__algorithm/for_each_n.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#define _LIBCPP___ALGORITHM_FOR_EACH_N_H
1212

1313
#include <__algorithm/for_each.h>
14+
#include <__algorithm/iterator_operations.h>
1415
#include <__config>
1516
#include <__iterator/iterator_traits.h>
1617
#include <__iterator/segmented_iterator.h>
@@ -53,7 +54,7 @@ template <class _InputIterator,
5354
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _InputIterator
5455
for_each_n(_InputIterator __first, _Size __orig_n, _Function __f) {
5556
_InputIterator __last = __first + __orig_n;
56-
std::for_each(__first, __last, __f);
57+
std::__for_each<_ClassicAlgPolicy>(__first, __last, __f);
5758
return __last;
5859
}
5960

libcxx/include/__algorithm/ranges_for_each.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <__algorithm/for_each.h>
1313
#include <__algorithm/in_fun_result.h>
14+
#include <__algorithm/iterator_operations.h>
1415
#include <__config>
1516
#include <__functional/identity.h>
1617
#include <__functional/invoke.h>
@@ -45,7 +46,8 @@ struct __for_each {
4546
if constexpr (random_access_iterator<_Iter> && sized_sentinel_for<_Sent, _Iter>) {
4647
auto __n = __last - __first;
4748
auto __end = __first + __n;
48-
std::for_each(__first, __end, [&](auto&& __val) { std::invoke(__func, std::invoke(__proj, __val)); });
49+
auto __f = [&](auto&& __val) { std::invoke(__func, std::invoke(__proj, __val)); };
50+
std::__for_each<_RangeAlgPolicy>(__first, __end, __f);
4951
return {std::move(__end), std::move(__func)};
5052
} else {
5153
for (; __first != __last; ++__first)

libcxx/include/__algorithm/ranges_for_each_n.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <__algorithm/for_each.h>
1313
#include <__algorithm/in_fun_result.h>
14+
#include <__algorithm/iterator_operations.h>
1415
#include <__config>
1516
#include <__functional/identity.h>
1617
#include <__functional/invoke.h>
@@ -43,7 +44,8 @@ struct __for_each_n {
4344
operator()(_Iter __first, iter_difference_t<_Iter> __count, _Func __func, _Proj __proj = {}) const {
4445
if constexpr (random_access_iterator<_Iter>) {
4546
auto __last = __first + __count;
46-
std::for_each(__first, __last, [&](auto&& __val) { std::invoke(__func, std::invoke(__proj, __val)); });
47+
auto __f = [&](auto&& __val) { std::invoke(__func, std::invoke(__proj, __val)); };
48+
std::__for_each<_RangeAlgPolicy>(__first, __last, __f);
4749
return {std::move(__last), std::move(__func)};
4850
} else {
4951
while (__count-- > 0) {

libcxx/test/std/algorithms/alg.nonmodifying/alg.foreach/for_each_n.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ struct deque_test {
4747

4848
/*TEST_CONSTEXPR_CXX23*/
4949
void test_segmented_deque_iterator() { // TODO: Mark as TEST_CONSTEXPR_CXX23 once std::deque is constexpr
50-
// check that segmented iterators work properly
50+
// check that segmented deque iterators work properly
5151
int sizes[] = {0, 1, 2, 1023, 1024, 1025, 2047, 2048, 2049};
5252
for (const int size : sizes) {
5353
std::deque<int> d(size);

libcxx/test/std/algorithms/alg.nonmodifying/alg.foreach/ranges.for_each.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ struct deque_test {
127127

128128
/*TEST_CONSTEXPR_CXX23*/
129129
void test_segmented_deque_iterator() { // TODO: Mark as TEST_CONSTEXPR_CXX23 once std::deque is constexpr
130-
// check that segmented iterators work properly
130+
// check that segmented deque iterators work properly
131131
int sizes[] = {0, 1, 2, 1023, 1024, 1025, 2047, 2048, 2049};
132132
for (const int size : sizes) {
133133
std::deque<int> d(size);

libcxx/test/std/algorithms/alg.nonmodifying/alg.foreach/ranges.for_each_n.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ struct deque_test {
8383

8484
/*TEST_CONSTEXPR_CXX23*/
8585
void test_segmented_deque_iterator() { // TODO: Mark as TEST_CONSTEXPR_CXX23 once std::deque is constexpr
86-
// check that segmented iterators work properly
86+
// check that segmented deque iterators work properly
8787
int sizes[] = {0, 1, 2, 1023, 1024, 1025, 2047, 2048, 2049};
8888
for (const int size : sizes) {
8989
std::deque<int> d(size);

0 commit comments

Comments
 (0)