Warning: This file is not a C or C++ file. It does not have highlighting.
| 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_FIND_SEGMENT_IF_H |
| 10 | #define _LIBCPP___CXX03___ALGORITHM_FIND_SEGMENT_IF_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 | // __find_segment_if is a utility function for optimizing iteration over segmented iterators linearly. |
| 22 | // [__first, __last) has to be a segmented range. __pred is expected to take a range of local iterators and the __proj. |
| 23 | // It returns an iterator to the first element that satisfies the predicate, or a one-past-the-end iterator if there was |
| 24 | // no match. __proj may be anything that should be passed to __pred, but is expected to be a projection to support |
| 25 | // ranges algorithms, or __identity for classic algorithms. |
| 26 | |
| 27 | template <class _SegmentedIterator, class _Pred, class _Proj> |
| 28 | _LIBCPP_HIDE_FROM_ABI _SegmentedIterator |
| 29 | __find_segment_if(_SegmentedIterator __first, _SegmentedIterator __last, _Pred __pred, _Proj& __proj) { |
| 30 | using _Traits = __segmented_iterator_traits<_SegmentedIterator>; |
| 31 | |
| 32 | auto __sfirst = _Traits::__segment(__first); |
| 33 | auto __slast = _Traits::__segment(__last); |
| 34 | |
| 35 | // We are in a single segment, so we might not be at the beginning or end |
| 36 | if (__sfirst == __slast) |
| 37 | return _Traits::__compose(__sfirst, __pred(_Traits::__local(__first), _Traits::__local(__last), __proj)); |
| 38 | |
| 39 | { // We have more than one segment. Iterate over the first segment, since we might not start at the beginning |
| 40 | auto __llast = _Traits::__end(__sfirst); |
| 41 | auto __liter = __pred(_Traits::__local(__first), __llast, __proj); |
| 42 | if (__liter != __llast) |
| 43 | return _Traits::__compose(__sfirst, __liter); |
| 44 | } |
| 45 | ++__sfirst; |
| 46 | |
| 47 | // Iterate over the segments which are guaranteed to be completely in the range |
| 48 | while (__sfirst != __slast) { |
| 49 | auto __llast = _Traits::__end(__sfirst); |
| 50 | auto __liter = __pred(_Traits::__begin(__sfirst), _Traits::__end(__sfirst), __proj); |
| 51 | if (__liter != __llast) |
| 52 | return _Traits::__compose(__sfirst, __liter); |
| 53 | ++__sfirst; |
| 54 | } |
| 55 | |
| 56 | // Iterate over the last segment |
| 57 | return _Traits::__compose(__sfirst, __pred(_Traits::__begin(__sfirst), _Traits::__local(__last), __proj)); |
| 58 | } |
| 59 | |
| 60 | _LIBCPP_END_NAMESPACE_STD |
| 61 | |
| 62 | #endif // _LIBCPP___CXX03___ALGORITHM_FIND_SEGMENT_IF_H |
| 63 |
Warning: This file is not a C or C++ file. It does not have highlighting.
