Skip to content

Commit

Permalink
Adding accumulate and plus
Browse files Browse the repository at this point in the history
  • Loading branch information
BrendanKKrueger committed Dec 9, 2024
1 parent 40510f6 commit c33cf67
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
19 changes: 19 additions & 0 deletions doc/sphinx/src/using.rst
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,25 @@ For sufficiently large integer powers, ``std::pow`` may be faster, but testing
indicates ``int_power`` is significantly faster (roughly a factor of two or
better) up to power of at least 100.

The function

.. code-block:: cpp
template <
typename IterB,
typename IterE,
typename Value,
typename Op = singe::util::plus<Value>>
PORTABLE_FUNCTION constexpr Value accumulate(
IterB begin,
IterE end,
Value accum,
Op && op = singe::util::plus<Value>{})
is a simple ``constexpr`` implementation of ``std::accumulate`` from the STL.
Ports-of-Call also provides a ``constexpr`` implementation of ``std::plus``
(which is the default operator for ``accumulate``).

macros_arrays.hpp
^^^^^^^^^^^^^^^^^^^

Expand Down
17 changes: 17 additions & 0 deletions ports-of-call/math_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ template <typename base_t, typename exp_t>
return result;
}

template <typename Value>
struct plus {
PORTABLE_FUNCTION constexpr Value operator()(Value const accum, Value const current) {
return accum + current;
}
};

template <typename IterB, typename IterE, typename Value,
typename Op = singe::util::plus<Value>>
PORTABLE_FUNCTION constexpr Value accumulate(IterB begin, IterE end, Value accum,
Op && op = singe::util::plus<Value>{}) {
for (auto iter = begin; iter != end; ++iter) {
accum = op(accum, *iter);
}
return accum;
}

} // namespace Math
} // namespace PortsOfCall

Expand Down

0 comments on commit c33cf67

Please sign in to comment.