Wyatt Hepler | 0412a7d | 2020-01-28 16:27:32 -0800 | [diff] [blame] | 1 | // Copyright 2020 The Pigweed Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | // use this file except in compliance with the License. You may obtain a copy of |
| 5 | // the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | // License for the specific language governing permissions and limitations under |
| 13 | // the License. |
| 14 | #pragma once |
| 15 | |
| 16 | #include <type_traits> |
| 17 | |
Wyatt Hepler | 023f35b | 2020-07-01 09:40:50 -0700 | [diff] [blame] | 18 | #include "pw_polyfill/standard_library/namespace.h" |
| 19 | |
| 20 | _PW_POLYFILL_BEGIN_NAMESPACE_STD |
Wyatt Hepler | 0412a7d | 2020-01-28 16:27:32 -0800 | [diff] [blame] | 21 | |
Armando Montanez | 187389d | 2020-04-06 17:00:13 -0700 | [diff] [blame] | 22 | template <class InputIterator, class OutputIterator> |
| 23 | constexpr OutputIterator copy(InputIterator first, |
| 24 | InputIterator last, |
| 25 | OutputIterator dest) { |
| 26 | while (first != last) { |
| 27 | *dest++ = *first++; |
| 28 | } |
| 29 | return dest; |
| 30 | } |
| 31 | |
Wyatt Hepler | 0412a7d | 2020-01-28 16:27:32 -0800 | [diff] [blame] | 32 | template <typename T> |
| 33 | constexpr const T& min(const T& lhs, const T& rhs) { |
| 34 | return (rhs < lhs) ? rhs : lhs; |
| 35 | } |
| 36 | |
| 37 | template <typename T> |
| 38 | constexpr const T& max(const T& lhs, const T& rhs) { |
| 39 | return (lhs < rhs) ? rhs : lhs; |
| 40 | } |
| 41 | |
Armando Montanez | 187389d | 2020-04-06 17:00:13 -0700 | [diff] [blame] | 42 | template <class InputIterator, typename T> |
| 43 | constexpr InputIterator find(InputIterator first, |
| 44 | InputIterator last, |
| 45 | const T& value) { |
| 46 | for (; first != last; ++first) { |
| 47 | if (*first == value) { |
| 48 | return first; |
| 49 | } |
| 50 | } |
| 51 | return last; |
| 52 | } |
| 53 | |
Wyatt Hepler | 0412a7d | 2020-01-28 16:27:32 -0800 | [diff] [blame] | 54 | template <typename T> |
| 55 | constexpr T&& forward(remove_reference_t<T>& value) { |
| 56 | return static_cast<T&&>(value); |
| 57 | } |
| 58 | |
| 59 | template <typename T> |
| 60 | constexpr T&& forward(remove_reference_t<T>&& value) { |
| 61 | return static_cast<T&&>(value); |
| 62 | } |
| 63 | |
Wyatt Hepler | bc6332c | 2020-02-10 16:34:19 -0800 | [diff] [blame] | 64 | template <class LhsIterator, class RhsIterator> |
| 65 | constexpr bool equal(LhsIterator first_l, |
| 66 | LhsIterator last_l, |
| 67 | RhsIterator first_r, |
| 68 | RhsIterator last_r) { |
| 69 | while (first_l != last_l && first_r != last_r) { |
| 70 | if (*first_l != *first_r) { |
| 71 | return false; |
| 72 | } |
| 73 | ++first_l; |
| 74 | ++first_r; |
| 75 | } |
| 76 | return first_l == last_l && first_r == last_r; |
| 77 | } |
| 78 | |
| 79 | template <class LhsIterator, class RhsIterator> |
| 80 | constexpr bool lexicographical_compare(LhsIterator first_l, |
| 81 | LhsIterator last_l, |
| 82 | RhsIterator first_r, |
| 83 | RhsIterator last_r) { |
| 84 | while (first_l != last_l && first_r != last_r) { |
| 85 | if (*first_l < *first_r) { |
| 86 | return true; |
| 87 | } |
| 88 | if (*first_r < *first_l) { |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | ++first_l; |
| 93 | ++first_r; |
| 94 | } |
| 95 | return (first_l == last_l) && (first_r != last_r); |
| 96 | } |
| 97 | |
Wyatt Hepler | 023f35b | 2020-07-01 09:40:50 -0700 | [diff] [blame] | 98 | _PW_POLYFILL_END_NAMESPACE_STD |