• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 template <class InputIterator, class OutputIterator>
copy(InputIterator first,InputIterator last,OutputIterator dest)21 constexpr OutputIterator copy(InputIterator first,
22                               InputIterator last,
23                               OutputIterator dest) {
24   while (first != last) {
25     *dest++ = *first++;
26   }
27   return dest;
28 }
29 
30 template <typename T>
min(const T & lhs,const T & rhs)31 constexpr const T& min(const T& lhs, const T& rhs) {
32   return (rhs < lhs) ? rhs : lhs;
33 }
34 
35 template <typename T>
max(const T & lhs,const T & rhs)36 constexpr const T& max(const T& lhs, const T& rhs) {
37   return (lhs < rhs) ? rhs : lhs;
38 }
39 
40 template <class InputIterator, typename T>
find(InputIterator first,InputIterator last,const T & value)41 constexpr 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 
52 template <class LhsIterator, class RhsIterator>
equal(LhsIterator first_l,LhsIterator last_l,RhsIterator first_r,RhsIterator last_r)53 constexpr 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 
67 template <class LhsIterator, class RhsIterator>
lexicographical_compare(LhsIterator first_l,LhsIterator last_l,RhsIterator first_r,RhsIterator last_r)68 constexpr 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