From bfb15475525462a4ecc30c5309ab28dd153be3c3 Mon Sep 17 00:00:00 2001 From: Simon Praetorius Date: Sun, 29 Nov 2020 23:58:44 +0100 Subject: [PATCH] add enumerate algorithm andremove exclusive_scan --- amdis/common/Algorithm.hpp | 105 +++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 50 deletions(-) diff --git a/amdis/common/Algorithm.hpp b/amdis/common/Algorithm.hpp index 7805a66f..c7d209dd 100644 --- a/amdis/common/Algorithm.hpp +++ b/amdis/common/Algorithm.hpp @@ -2,66 +2,71 @@ #include #include +#include +#include +#include #include -namespace AMDiS +namespace AMDiS { + +/// \brief Split a sequence `[first,last)` by the separators `sep` and pass the tokens as +/// begin-end iterator pair to the provided functor `f = void(InputIterator, InputIterator)` +template +void split(InputIter first, InputIter last, Tp sep, BinaryFunc f) { - /// \brief Split a sequence `[first,last)` by the separators `sep` and pass the tokens as - /// begin-end iterator pair to the provided functor `f = void(InputIterator, InputIterator)` - template - void split(InputIter first, InputIter last, Tp sep, BinaryFunc f) - { - if (first == last) - return; + if (first == last) + return; - while (true) { - InputIter found = std::find(first, last, sep); - f(first, found); - if (found == last) - break; - first = ++found; - } + while (true) { + InputIter found = std::find(first, last, sep); + f(first, found); + if (found == last) + break; + first = ++found; } +} - /// \brief Split a sequence `[first,last)` by any of the separators `[s_first, s_last)` and pass the tokens as - /// begin-end iterator pair to the provided functor `f = void(InputIterator, InputIterator)` - template - void split(InputIter first, InputIter last, SeparaterIter s_first, SeparaterIter s_last, BinaryFunc f) - { - if (first == last) - return; +/// \brief Split a sequence `[first,last)` by any of the separators `[s_first, s_last)` and pass the tokens as +/// begin-end iterator pair to the provided functor `f = void(InputIterator, InputIterator)` +template +void split(InputIter first, InputIter last, SeparaterIter s_first, SeparaterIter s_last, BinaryFunc f) +{ + if (first == last) + return; - while (true) { - InputIter found = std::find_first_of(first, last, s_first, s_last); - f(first, found); - if (found == last) - break; - first = ++found; - } + while (true) { + InputIter found = std::find_first_of(first, last, s_first, s_last); + f(first, found); + if (found == last) + break; + first = ++found; } +} - /// \brief Output the cumulative sum of one range to a second range - // NOTE: backport of std::exclusive_scan from c++17 - template - OutputIter exclusive_scan(InputIter first, InputIter last, - OutputIter result, Tp init, BinaryOperation binary_op) +/// \brief Provide python-like indexing iterator that returns a pair of [i,element] +// see: http://reedbeta.com/blog/python-like-enumerate-in-cpp17 +template ())), + class = decltype(std::end(std::declval()))> +constexpr auto enumerate(Range&& range) +{ + struct iterator { - while (first != last) { - auto v = init; - init = binary_op(init, *first); - ++first; - *result++ = std::move(v); - } - return result; - } + typename std::iterator_traits::difference_type i; + Iter iter; + bool operator!=(iterator const& other) const { return iter != other.iter; } + void operator++() { ++i; ++iter; } + auto operator*() const { return std::tie(i, *iter); } + }; - /// \brief Output the cumulative sum of one range to a second range - // NOTE: backport of std::exclusive_scan from c++17 - template - inline OutputIter exclusive_scan(InputIter first, InputIter last, - OutputIter result, Tp init) + struct range_wrapper { - return AMDiS::exclusive_scan(first, last, result, std::move(init), std::plus<>()); - } + Range range; + auto begin() { return iterator{0, std::begin(range)}; } + auto end() { return iterator{0, std::end(range)}; } + }; + + return range_wrapper{std::forward(range)}; +} -} \ No newline at end of file +} // end namepace AMDiS -- GitLab