[3f5b7126] | 1 | /*
|
---|
| 2 | * Copyright (c) 2017 Jaroslav Jindrak
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #ifndef LIBCPP_NUMERIC
|
---|
[fff05d3] | 30 | #define LIBCPP_NUMERIC
|
---|
[3f5b7126] | 31 |
|
---|
| 32 | #include <utility>
|
---|
| 33 |
|
---|
| 34 | namespace std
|
---|
| 35 | {
|
---|
| 36 |
|
---|
| 37 | /**
|
---|
| 38 | * 26.7.2, accumulate:
|
---|
| 39 | */
|
---|
| 40 |
|
---|
| 41 | template<class InputIterator, class T>
|
---|
| 42 | T accumulate(InputIterator first, InputIterator last, T init)
|
---|
| 43 | {
|
---|
| 44 | auto acc{init};
|
---|
| 45 | while (first != last)
|
---|
| 46 | acc += *first++;
|
---|
| 47 |
|
---|
| 48 | return acc;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | template<class InputIterator, class T, class BinaryOperation>
|
---|
| 52 | T accumulate(InputIterator first, InputIterator last, T init,
|
---|
| 53 | BinaryOperation op)
|
---|
| 54 | {
|
---|
| 55 | auto acc{init};
|
---|
| 56 | while (first != last)
|
---|
| 57 | acc = op(acc, *first++);
|
---|
| 58 |
|
---|
| 59 | return acc;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | /**
|
---|
| 63 | * 26.7.3, inner product:
|
---|
| 64 | */
|
---|
| 65 |
|
---|
| 66 | template<class InputIterator1, class InputIterator2, class T>
|
---|
| 67 | T inner_product(InputIterator1 first1, InputIterator1 last1,
|
---|
| 68 | InputIterator2 first2, T init)
|
---|
| 69 | {
|
---|
| 70 | auto res{init};
|
---|
| 71 | while (first1 != last1)
|
---|
| 72 | res += (*first1++) * (*first2++);
|
---|
| 73 |
|
---|
| 74 | return res;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | template<class InputIterator1, class InputIterator2, class T,
|
---|
| 78 | class BinaryOperation1, class BinaryOperation2>
|
---|
| 79 | T inner_product(InputIterator1 first1, InputIterator1 last1,
|
---|
| 80 | InputIterator2 first2, T init,
|
---|
| 81 | BinaryOperation1 op1, BinaryOperation2 op2)
|
---|
| 82 | {
|
---|
| 83 | auto res{init};
|
---|
| 84 | while (first1 != last1)
|
---|
| 85 | res = op1(res, op2(*first1++, *first2++));
|
---|
| 86 |
|
---|
| 87 | return res;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | /**
|
---|
| 91 | * 26.7.4, partial sum:
|
---|
| 92 | */
|
---|
| 93 |
|
---|
| 94 | template<class InputIterator, class OutputIterator>
|
---|
| 95 | OutputIterator partial_sum(InputIterator first, InputIterator last,
|
---|
| 96 | OutputIterator result)
|
---|
| 97 | {
|
---|
| 98 | if (first == last)
|
---|
[43ba118] | 99 | return result;
|
---|
[3f5b7126] | 100 |
|
---|
| 101 | auto acc{*first++};
|
---|
| 102 | *result++ = acc;
|
---|
| 103 |
|
---|
| 104 | while (first != last)
|
---|
| 105 | *result++ = acc = acc + *first++;
|
---|
| 106 |
|
---|
| 107 | return result;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | template<class InputIterator, class OutputIterator, class BinaryOperation>
|
---|
| 111 | OutputIterator partial_sum(InputIterator first, InputIterator last,
|
---|
| 112 | OutputIterator result, BinaryOperation op)
|
---|
| 113 | {
|
---|
| 114 | if (first == last)
|
---|
[43ba118] | 115 | return result;
|
---|
[3f5b7126] | 116 |
|
---|
| 117 | auto acc{*first++};
|
---|
| 118 | *result++ = acc;
|
---|
| 119 |
|
---|
| 120 | while (first != last)
|
---|
| 121 | *result++ = acc = op(acc, *first++);
|
---|
| 122 |
|
---|
| 123 | return result;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | /**
|
---|
| 127 | * 26.7.5, adjacent difference:
|
---|
| 128 | */
|
---|
| 129 |
|
---|
| 130 | template<class InputIterator, class OutputIterator>
|
---|
| 131 | OutputIterator adjacent_difference(InputIterator first, InputIterator last,
|
---|
| 132 | OutputIterator result)
|
---|
| 133 | {
|
---|
| 134 | if (first == last)
|
---|
[43ba118] | 135 | return result;
|
---|
[3f5b7126] | 136 |
|
---|
| 137 | auto acc{*first++};
|
---|
| 138 | *result++ = acc;
|
---|
| 139 |
|
---|
| 140 | while (first != last)
|
---|
| 141 | {
|
---|
| 142 | auto val = *first++;
|
---|
| 143 | *result++ = val - acc;
|
---|
| 144 | acc = move(val);
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | return result;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | template<class InputIterator, class OutputIterator, class BinaryOperation>
|
---|
| 151 | OutputIterator adjacent_difference(InputIterator first, InputIterator last,
|
---|
| 152 | OutputIterator result, BinaryOperation op)
|
---|
| 153 | {
|
---|
| 154 | if (first == last)
|
---|
[43ba118] | 155 | return result;
|
---|
[3f5b7126] | 156 |
|
---|
| 157 | auto acc{*first++};
|
---|
| 158 | *result++ = acc;
|
---|
| 159 |
|
---|
| 160 | while (first != last)
|
---|
| 161 | {
|
---|
| 162 | auto val = *first++;
|
---|
| 163 | *result++ = op(val, acc);
|
---|
| 164 | acc = move(val);
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | return result;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | /**
|
---|
| 171 | * 26.7.6, iota:
|
---|
| 172 | */
|
---|
| 173 |
|
---|
| 174 | template<class ForwardIterator, class T>
|
---|
| 175 | void iota(ForwardIterator first, ForwardIterator last, T value)
|
---|
| 176 | {
|
---|
| 177 | while (first != last)
|
---|
| 178 | *first++ = value++;
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | #endif
|
---|