| 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___CXX03___ALGORITHM_FOR_EACH_SEGMENT_H |
| 10 |
#define _LIBCPP___CXX03___ALGORITHM_FOR_EACH_SEGMENT_H |
| 11 |
|
| 12 |
#include <__cxx03/__config> |
| 13 |
#include <__cxx03/__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 void __for_each_segment(_SegmentedIterator __first, _SegmentedIterator __last, _Functor __func) { |
| 27 |
using _Traits = __segmented_iterator_traits<_SegmentedIterator>; |
| 28 |
|
| 29 |
auto __sfirst = _Traits::__segment(__first); |
| 30 |
auto __slast = _Traits::__segment(__last); |
| 31 |
|
| 32 |
// We are in a single segment, so we might not be at the beginning or end |
| 33 |
if (__sfirst == __slast) { |
| 34 |
__func(_Traits::__local(__first), _Traits::__local(__last)); |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
// We have more than one segment. Iterate over the first segment, since we might not start at the beginning |
| 39 |
__func(_Traits::__local(__first), _Traits::__end(__sfirst)); |
| 40 |
++__sfirst; |
| 41 |
// iterate over the segments which are guaranteed to be completely in the range |
| 42 |
while (__sfirst != __slast) { |
| 43 |
__func(_Traits::__begin(__sfirst), _Traits::__end(__sfirst)); |
| 44 |
++__sfirst; |
| 45 |
} |
| 46 |
// iterate over the last segment |
| 47 |
__func(_Traits::__begin(__sfirst), _Traits::__local(__last)); |
| 48 |
} |
| 49 |
|
| 50 |
_LIBCPP_END_NAMESPACE_STD |
| 51 |
|
| 52 |
#endif // _LIBCPP___CXX03___ALGORITHM_FOR_EACH_SEGMENT_H |