xref: /llvm-project/libc/src/__support/CPP/algorithm.h (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
1bc2b1193SGuillaume Chatelet //===-- A self contained equivalent of <algorithm> --------------*- C++ -*-===//
2bc2b1193SGuillaume Chatelet //
3bc2b1193SGuillaume Chatelet // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4bc2b1193SGuillaume Chatelet // See https://llvm.org/LICENSE.txt for license information.
5bc2b1193SGuillaume Chatelet // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6bc2b1193SGuillaume Chatelet //
7bc2b1193SGuillaume Chatelet //===----------------------------------------------------------------------===//
8bc2b1193SGuillaume Chatelet // This file is minimalist on purpose but can receive a few more function if
9bc2b1193SGuillaume Chatelet // they prove useful.
10bc2b1193SGuillaume Chatelet //===----------------------------------------------------------------------===//
11bc2b1193SGuillaume Chatelet 
12270547f3SGuillaume Chatelet #ifndef LLVM_LIBC_SRC___SUPPORT_CPP_ALGORITHM_H
13270547f3SGuillaume Chatelet #define LLVM_LIBC_SRC___SUPPORT_CPP_ALGORITHM_H
14bc2b1193SGuillaume Chatelet 
15bc2b1193SGuillaume Chatelet #include "src/__support/macros/attributes.h" // LIBC_INLINE
16*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
17bc2b1193SGuillaume Chatelet 
18*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
19bc2b1193SGuillaume Chatelet namespace cpp {
20bc2b1193SGuillaume Chatelet 
21bc2b1193SGuillaume Chatelet template <class T> LIBC_INLINE constexpr const T &max(const T &a, const T &b) {
22bc2b1193SGuillaume Chatelet   return (a < b) ? b : a;
23bc2b1193SGuillaume Chatelet }
24bc2b1193SGuillaume Chatelet 
25bc2b1193SGuillaume Chatelet template <class T> LIBC_INLINE constexpr const T &min(const T &a, const T &b) {
26bc2b1193SGuillaume Chatelet   return (a < b) ? a : b;
27bc2b1193SGuillaume Chatelet }
28bc2b1193SGuillaume Chatelet 
29e44cea59SPiJoules template <class InputIt, class UnaryPred>
30e44cea59SPiJoules LIBC_INLINE constexpr InputIt find_if_not(InputIt first, InputIt last,
31e44cea59SPiJoules                                           UnaryPred q) {
32e44cea59SPiJoules   for (; first != last; ++first)
33e44cea59SPiJoules     if (!q(*first))
34e44cea59SPiJoules       return first;
35e44cea59SPiJoules 
36e44cea59SPiJoules   return last;
37e44cea59SPiJoules }
38e44cea59SPiJoules 
39e44cea59SPiJoules template <class InputIt, class UnaryPred>
40e44cea59SPiJoules LIBC_INLINE constexpr bool all_of(InputIt first, InputIt last, UnaryPred p) {
41e44cea59SPiJoules   return find_if_not(first, last, p) == last;
42e44cea59SPiJoules }
43e44cea59SPiJoules 
44bc2b1193SGuillaume Chatelet } // namespace cpp
45*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
46bc2b1193SGuillaume Chatelet 
47270547f3SGuillaume Chatelet #endif // LLVM_LIBC_SRC___SUPPORT_CPP_ALGORITHM_H
48