[Flang] Optionally do not compile the runtime in-tree (#122336)
[llvm-project.git] / libc / src / __support / CPP / algorithm.h
blob7704b3fa81f0c1c61528f29de7233b211cccd3a3
1 //===-- A self contained equivalent of <algorithm> --------------*- C++ -*-===//
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 // This file is minimalist on purpose but can receive a few more function if
9 // they prove useful.
10 //===----------------------------------------------------------------------===//
12 #ifndef LLVM_LIBC_SRC___SUPPORT_CPP_ALGORITHM_H
13 #define LLVM_LIBC_SRC___SUPPORT_CPP_ALGORITHM_H
15 #include "src/__support/macros/attributes.h" // LIBC_INLINE
16 #include "src/__support/macros/config.h"
18 namespace LIBC_NAMESPACE_DECL {
19 namespace cpp {
21 template <class T> LIBC_INLINE constexpr const T &max(const T &a, const T &b) {
22 return (a < b) ? b : a;
25 template <class T> LIBC_INLINE constexpr const T &min(const T &a, const T &b) {
26 return (a < b) ? a : b;
29 template <class T> LIBC_INLINE constexpr T abs(T a) { return a < 0 ? -a : a; }
31 template <class InputIt, class UnaryPred>
32 LIBC_INLINE constexpr InputIt find_if_not(InputIt first, InputIt last,
33 UnaryPred q) {
34 for (; first != last; ++first)
35 if (!q(*first))
36 return first;
38 return last;
41 template <class InputIt, class UnaryPred>
42 LIBC_INLINE constexpr bool all_of(InputIt first, InputIt last, UnaryPred p) {
43 return find_if_not(first, last, p) == last;
46 } // namespace cpp
47 } // namespace LIBC_NAMESPACE_DECL
49 #endif // LLVM_LIBC_SRC___SUPPORT_CPP_ALGORITHM_H