[CodeGenPrepare] Drop nsw flags in `optimizeLoadExt` (#118180)
[llvm-project.git] / libcxx / include / __algorithm / find_segment_if.h
blob9d6064f3e283a6953c03a983baa4ce467a7f1337
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 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___ALGORITHM_FIND_SEGMENT_IF_H
10 #define _LIBCPP___ALGORITHM_FIND_SEGMENT_IF_H
12 #include <__config>
13 #include <__iterator/segmented_iterator.h>
15 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
16 # pragma GCC system_header
17 #endif
19 _LIBCPP_BEGIN_NAMESPACE_STD
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.
27 template <class _SegmentedIterator, class _Pred, class _Proj>
28 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _SegmentedIterator
29 __find_segment_if(_SegmentedIterator __first, _SegmentedIterator __last, _Pred __pred, _Proj& __proj) {
30 using _Traits = __segmented_iterator_traits<_SegmentedIterator>;
32 auto __sfirst = _Traits::__segment(__first);
33 auto __slast = _Traits::__segment(__last);
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));
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);
45 ++__sfirst;
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;
56 // Iterate over the last segment
57 return _Traits::__compose(__sfirst, __pred(_Traits::__begin(__sfirst), _Traits::__local(__last), __proj));
60 _LIBCPP_END_NAMESPACE_STD
62 #endif // _LIBCPP___ALGORITHM_FIND_SEGMENT_IF_H