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