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 "pw_polyfill/standard_library/namespace.h"
17
18_PW_POLYFILL_BEGIN_NAMESPACE_STD
19
20template <class InputIterator, class OutputIterator>
21constexpr OutputIterator copy(InputIterator first,
22                              InputIterator last,
23                              OutputIterator dest) {
24  while (first != last) {
25    *dest++ = *first++;
26  }
27  return dest;
28}
29
30template <typename T>
31constexpr const T& min(const T& lhs, const T& rhs) {
32  return (rhs < lhs) ? rhs : lhs;
33}
34
35template <typename T>
36constexpr const T& max(const T& lhs, const T& rhs) {
37  return (lhs < rhs) ? rhs : lhs;
38}
39
40template <class InputIterator, typename T>
41constexpr InputIterator find(InputIterator first,
42                             InputIterator last,
43                             const T& value) {
44  for (; first != last; ++first) {
45    if (*first == value) {
46      return first;
47    }
48  }
49  return last;
50}
51
52template <class LhsIterator, class RhsIterator>
53constexpr bool equal(LhsIterator first_l,
54                     LhsIterator last_l,
55                     RhsIterator first_r,
56                     RhsIterator last_r) {
57  while (first_l != last_l && first_r != last_r) {
58    if (*first_l != *first_r) {
59      return false;
60    }
61    ++first_l;
62    ++first_r;
63  }
64  return first_l == last_l && first_r == last_r;
65}
66
67template <class LhsIterator, class RhsIterator>
68constexpr bool lexicographical_compare(LhsIterator first_l,
69                                       LhsIterator last_l,
70                                       RhsIterator first_r,
71                                       RhsIterator last_r) {
72  while (first_l != last_l && first_r != last_r) {
73    if (*first_l < *first_r) {
74      return true;
75    }
76    if (*first_r < *first_l) {
77      return false;
78    }
79
80    ++first_l;
81    ++first_r;
82  }
83  return (first_l == last_l) && (first_r != last_r);
84}
85
86_PW_POLYFILL_END_NAMESPACE_STD
87