1 //===----------------------------------------------------------------------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef _LIBCPP___ALGORITHM_FOR_EACH_SEGMENT_H 10 #define _LIBCPP___ALGORITHM_FOR_EACH_SEGMENT_H 11 12 #include <__config> 13 #include <__iterator/segmented_iterator.h> 14 15 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 16 # pragma GCC system_header 17 #endif 18 19 _LIBCPP_BEGIN_NAMESPACE_STD 20 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. 24 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>; 29 30 auto __sfirst = _Traits::__segment(__first); 31 auto __slast = _Traits::__segment(__last); 32 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)); 36 return; 37 } 38 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)); 41 ++__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)); 45 ++__sfirst; 46 } 47 // iterate over the last segment 48 __func(_Traits::__begin(__sfirst), _Traits::__local(__last)); 49 } 50 51 _LIBCPP_END_NAMESPACE_STD 52 53 #endif // _LIBCPP___ALGORITHM_FOR_EACH_SEGMENT_H 54