Algorithm.hpp 2.11 KB
 Praetorius, Simon committed Feb 28, 2019 1 2 3 ``````#pragma once #include `````` Praetorius, Simon committed Jan 03, 2020 4 5 ``````#include #include `````` Praetorius, Simon committed Feb 28, 2019 6 7 8 `````` namespace AMDiS { `````` Praetorius, Simon committed Jan 03, 2020 9 10 11 12 `````` /// \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) `````` Praetorius, Simon committed Feb 28, 2019 13 `````` { `````` Praetorius, Simon committed Jan 03, 2020 14 `````` if (first == last) `````` Praetorius, Simon committed Feb 28, 2019 15 16 17 `````` return; while (true) { `````` Praetorius, Simon committed Jan 03, 2020 18 `````` InputIter found = std::find(first, last, sep); `````` Praetorius, Simon committed Feb 28, 2019 19 `````` f(first, found); `````` Praetorius, Simon committed Jan 03, 2020 20 `````` if (found == last) `````` Praetorius, Simon committed Feb 28, 2019 21 22 23 24 25 `````` break; first = ++found; } } `````` Praetorius, Simon committed Jan 03, 2020 26 27 28 29 `````` /// \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) `````` Praetorius, Simon committed Feb 28, 2019 30 `````` { `````` Praetorius, Simon committed Jan 03, 2020 31 `````` if (first == last) `````` Praetorius, Simon committed Feb 28, 2019 32 33 34 `````` return; while (true) { `````` Praetorius, Simon committed Jan 03, 2020 35 `````` InputIter found = std::find_first_of(first, last, s_first, s_last); `````` Praetorius, Simon committed Feb 28, 2019 36 `````` f(first, found); `````` Praetorius, Simon committed Jan 03, 2020 37 `````` if (found == last) `````` Praetorius, Simon committed Feb 28, 2019 38 39 40 41 `````` break; first = ++found; } } `````` Praetorius, Simon committed Jan 03, 2020 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 `````` /// \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<>()); } `````` Praetorius, Simon committed Feb 28, 2019 67 ``}``