Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:08

0001 //===----------------------------------------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef _LIBCPP___ALGORITHM_FIND_SEGMENT_IF_H
0010 #define _LIBCPP___ALGORITHM_FIND_SEGMENT_IF_H
0011 
0012 #include <__config>
0013 #include <__iterator/segmented_iterator.h>
0014 
0015 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0016 #  pragma GCC system_header
0017 #endif
0018 
0019 _LIBCPP_BEGIN_NAMESPACE_STD
0020 
0021 // __find_segment_if is a utility function for optimizing iteration over segmented iterators linearly.
0022 // [__first, __last) has to be a segmented range. __pred is expected to take a range of local iterators and the __proj.
0023 // It returns an iterator to the first element that satisfies the predicate, or a one-past-the-end iterator if there was
0024 // no match. __proj may be anything that should be passed to __pred, but is expected to be a projection to support
0025 // ranges algorithms, or __identity for classic algorithms.
0026 
0027 template <class _SegmentedIterator, class _Pred, class _Proj>
0028 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _SegmentedIterator
0029 __find_segment_if(_SegmentedIterator __first, _SegmentedIterator __last, _Pred __pred, _Proj& __proj) {
0030   using _Traits = __segmented_iterator_traits<_SegmentedIterator>;
0031 
0032   auto __sfirst = _Traits::__segment(__first);
0033   auto __slast  = _Traits::__segment(__last);
0034 
0035   // We are in a single segment, so we might not be at the beginning or end
0036   if (__sfirst == __slast)
0037     return _Traits::__compose(__sfirst, __pred(_Traits::__local(__first), _Traits::__local(__last), __proj));
0038 
0039   { // We have more than one segment. Iterate over the first segment, since we might not start at the beginning
0040     auto __llast = _Traits::__end(__sfirst);
0041     auto __liter = __pred(_Traits::__local(__first), __llast, __proj);
0042     if (__liter != __llast)
0043       return _Traits::__compose(__sfirst, __liter);
0044   }
0045   ++__sfirst;
0046 
0047   // Iterate over the segments which are guaranteed to be completely in the range
0048   while (__sfirst != __slast) {
0049     auto __llast = _Traits::__end(__sfirst);
0050     auto __liter = __pred(_Traits::__begin(__sfirst), _Traits::__end(__sfirst), __proj);
0051     if (__liter != __llast)
0052       return _Traits::__compose(__sfirst, __liter);
0053     ++__sfirst;
0054   }
0055 
0056   // Iterate over the last segment
0057   return _Traits::__compose(__sfirst, __pred(_Traits::__begin(__sfirst), _Traits::__local(__last), __proj));
0058 }
0059 
0060 _LIBCPP_END_NAMESPACE_STD
0061 
0062 #endif // _LIBCPP___ALGORITHM_FIND_SEGMENT_IF_H