#pragma once #include #include #include 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) { if (first == last) return; 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; 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) { while (first != last) { auto v = init; init = binary_op(init, *first); ++first; *result++ = std::move(v); } return result; } /// \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) { return AMDiS::exclusive_scan(first, last, result, std::move(init), std::plus<>()); } }