1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP___ALGORITHM_FIND_IF_H
11#define _LIBCPP___ALGORITHM_FIND_IF_H
12
13#include <__config>
14#include <__functional/identity.h>
15#include <__memory/valid_range.h>
16#include <__type_traits/invoke.h>
17
18#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19# pragma GCC system_header
20#endif
21
22_LIBCPP_BEGIN_NAMESPACE_STD
23
24template <class _Iter, class _Sent, class _Pred, class _Proj>
25_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 _Iter
26__find_if(_Iter __first, _Sent __last, _Pred&& __pred, _Proj&& __proj) {
27 // Don't make the valid range assumption for empty ranges since using unaligned sentinel
28 // values to implement iterators of empty ranges is a common occurence.
29 if (__first == __last)
30 return __first;
31
32 std::__assume_valid_range(__first, __last);
33
34 do {
35 if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
36 break;
37 ++__first;
38 } while (__first != __last);
39 return __first;
40}
41
42template <class _InputIterator, class _Predicate>
43[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _InputIterator
44find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred) {
45 return std::__find_if(__first, __last, __pred, __identity());
46}
47
48_LIBCPP_END_NAMESPACE_STD
49
50#endif // _LIBCPP___ALGORITHM_FIND_IF_H
51