xref: /aosp_15_r20/external/llvm-libc/src/__support/CPP/algorithm.h (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
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 //===----------------------------------------------------------------------===//
11 
12 #ifndef LLVM_LIBC_SRC___SUPPORT_CPP_ALGORITHM_H
13 #define LLVM_LIBC_SRC___SUPPORT_CPP_ALGORITHM_H
14 
15 #include "src/__support/macros/attributes.h" // LIBC_INLINE
16 #include "src/__support/macros/config.h"
17 
18 namespace LIBC_NAMESPACE_DECL {
19 namespace cpp {
20 
max(const T & a,const T & b)21 template <class T> LIBC_INLINE constexpr const T &max(const T &a, const T &b) {
22   return (a < b) ? b : a;
23 }
24 
min(const T & a,const T & b)25 template <class T> LIBC_INLINE constexpr const T &min(const T &a, const T &b) {
26   return (a < b) ? a : b;
27 }
28 
29 template <class InputIt, class UnaryPred>
find_if_not(InputIt first,InputIt last,UnaryPred q)30 LIBC_INLINE constexpr InputIt find_if_not(InputIt first, InputIt last,
31                                           UnaryPred q) {
32   for (; first != last; ++first)
33     if (!q(*first))
34       return first;
35 
36   return last;
37 }
38 
39 template <class InputIt, class UnaryPred>
all_of(InputIt first,InputIt last,UnaryPred p)40 LIBC_INLINE constexpr bool all_of(InputIt first, InputIt last, UnaryPred p) {
41   return find_if_not(first, last, p) == last;
42 }
43 
44 } // namespace cpp
45 } // namespace LIBC_NAMESPACE_DECL
46 
47 #endif // LLVM_LIBC_SRC___SUPPORT_CPP_ALGORITHM_H
48