1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___ALGORITHM_FOR_EACH_SEGMENT_H
10 #define _LIBCPP___ALGORITHM_FOR_EACH_SEGMENT_H
12 #include <__cxx03/__config>
13 #include <__cxx03/__iterator/segmented_iterator.h>
15 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
16 # pragma GCC system_header
19 _LIBCPP_BEGIN_NAMESPACE_STD
21 // __for_each_segment is a utility function for optimizing iterating over segmented iterators linearly.
22 // __first and __last are expected to be a segmented range. __func is expected to take a range of local iterators.
23 // Anything that is returned from __func is ignored.
25 template <class _SegmentedIterator
, class _Functor
>
26 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
void
27 __for_each_segment(_SegmentedIterator __first
, _SegmentedIterator __last
, _Functor __func
) {
28 using _Traits
= __segmented_iterator_traits
<_SegmentedIterator
>;
30 auto __sfirst
= _Traits::__segment(__first
);
31 auto __slast
= _Traits::__segment(__last
);
33 // We are in a single segment, so we might not be at the beginning or end
34 if (__sfirst
== __slast
) {
35 __func(_Traits::__local(__first
), _Traits::__local(__last
));
39 // We have more than one segment. Iterate over the first segment, since we might not start at the beginning
40 __func(_Traits::__local(__first
), _Traits::__end(__sfirst
));
42 // iterate over the segments which are guaranteed to be completely in the range
43 while (__sfirst
!= __slast
) {
44 __func(_Traits::__begin(__sfirst
), _Traits::__end(__sfirst
));
47 // iterate over the last segment
48 __func(_Traits::__begin(__sfirst
), _Traits::__local(__last
));
51 _LIBCPP_END_NAMESPACE_STD
53 #endif // _LIBCPP___ALGORITHM_FOR_EACH_SEGMENT_H