Skip to content

Commit ff5366b

Browse files
committed
Slightly improve __for_each_n_segment
1 parent 5a7b6eb commit ff5366b

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

libcxx/include/__algorithm/for_each_n_segment.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121

2222
_LIBCPP_BEGIN_NAMESPACE_STD
2323

24-
// __for_each_n_segment is a utility function for optimizing iterating over segmented iterators linearly.
25-
// __first and __orig_n are represent the begining and size of a segmented range. __func is expected to
26-
// take a range of local iterators. Anything that is returned from __func is ignored.
24+
// __for_each_n_segment optimizes linear iteration over segmented iterators. It processes a segmented
25+
// input range defined by (__first, __orig_n), where __first is the starting segmented iterator and
26+
// __orig_n is the number of elements to process. The functor __func is applied to each segment using
27+
// local iterator pairs for that segment. The return value of __func is ignored, and the function
28+
// returns an iterator pointing to one past the last processed element in the input range.
2729

2830
template <class _SegmentedIterator, class _Size, class _Functor>
2931
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _SegmentedIterator
@@ -40,32 +42,33 @@ __for_each_n_segment(_SegmentedIterator __first, _Size __orig_n, _Functor __func
4042
auto __lfirst = _Traits::__local(__first);
4143
auto __seg_size = static_cast<_IntegralSize>(std::distance(__lfirst, __slast));
4244

43-
// We have only one single segment, which might not start or end at the boundaries of the segment
45+
// Single-segment case: input range fits within a single segment (may not align with segment boundaries)
4446
if (__n <= __seg_size) {
4547
auto __llast = std::next(__lfirst, __n);
4648
__func(__lfirst, __llast);
4749
return _Traits::__compose(__seg, __llast);
4850
}
4951

50-
// We have more than one segment. Iterate over the first segment which might not start at the beginning
51-
__func(__lfirst, std::next(__lfirst, __seg_size));
52+
// Multi-segment case: input range spans multiple segments.
53+
// Process the first segment which might not start at the beginning of the segment
54+
__func(__lfirst, __slast);
5255
++__seg;
5356
__n -= __seg_size;
5457

55-
// Iterate over the 2nd to last segments which are guaranteed to start at the beginning of each segment
58+
// Process the 2nd to last segments guaranteed to start at the beginning of each segment
5659
while (true) {
5760
__sfirst = _Traits::__begin(__seg);
5861
__slast = _Traits::__end(__seg);
5962
__seg_size = std::distance(__sfirst, __slast);
6063

61-
// We are in the last segment
64+
// The last (potentially partial) segment
6265
if (__n <= __seg_size) {
6366
auto __llast = std::next(__sfirst, __n);
6467
__func(__sfirst, __llast);
6568
return _Traits::__compose(__seg, __llast);
6669
}
6770

68-
// We are in middle segments that are completely in the range
71+
// Middle whole segments that are completely in the range
6972
__func(__sfirst, __slast);
7073
++__seg;
7174
__n -= __seg_size;

0 commit comments

Comments
 (0)