AMDiS 2.10
The Adaptive Multi-Dimensional Simulation Toolbox
Algorithm.hpp
1#pragma once
2
3#include <algorithm>
4#include <functional>
5#include <iterator>
6#include <tuple>
7#include <type_traits>
8#include <utility>
9
10namespace AMDiS {
11
14template <class InputIter, class Tp, class BinaryFunc>
15void split(InputIter first, InputIter last, Tp sep, BinaryFunc f)
16{
17 if (first == last)
18 return;
19
20 while (true) {
21 InputIter found = std::find(first, last, sep);
22 f(first, found);
23 if (found == last)
24 break;
25 first = ++found;
26 }
27}
28
31template <class InputIter, class SeparaterIter, class BinaryFunc>
32void split(InputIter first, InputIter last, SeparaterIter s_first, SeparaterIter s_last, BinaryFunc f)
33{
34 if (first == last)
35 return;
36
37 while (true) {
38 InputIter found = std::find_first_of(first, last, s_first, s_last);
39 f(first, found);
40 if (found == last)
41 break;
42 first = ++found;
43 }
44}
45
47// see: http://reedbeta.com/blog/python-like-enumerate-in-cpp17
48template <class Range,
49 class Iter = decltype(std::begin(std::declval<Range>())),
50 class = decltype(std::end(std::declval<Range>()))>
51constexpr auto enumerate(Range&& range)
52{
53 struct iterator
54 {
55 typename std::iterator_traits<Iter>::difference_type i;
56 Iter iter;
57 bool operator!=(iterator const& other) const { return iter != other.iter; }
58 void operator++() { ++i; ++iter; }
59 auto operator*() const { return std::tie(i, *iter); }
60 };
61
62 struct range_wrapper
63 {
64 Range range;
65 auto begin() { return iterator{0, std::begin(range)}; }
66 auto end() { return iterator{0, std::end(range)}; }
67 };
68
69 return range_wrapper{std::forward<Range>(range)};
70}
71
72} // end namepace AMDiS