Commit bfb15475 by Praetorius, Simon

### add enumerate algorithm andremove exclusive_scan

parent f9a94a4e
 ... ... @@ -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
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!