AMDiS  0.3
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 
10 namespace AMDiS {
11 
14 template <class InputIter, class Tp, class BinaryFunc>
15 void 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 
31 template <class InputIter, class SeparaterIter, class BinaryFunc>
32 void 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
48 template <class Range,
49  class Iter = decltype(std::begin(std::declval<Range>())),
50  class = decltype(std::end(std::declval<Range>()))>
51 constexpr 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
Contains all classes needed for solving linear and non linear equation systems.
Definition: AdaptBase.hpp:6
constexpr auto enumerate(Range &&range)
Provide python-like indexing iterator that returns a pair of [i,element].
Definition: Algorithm.hpp:51
void split(InputIter first, InputIter last, Tp sep, BinaryFunc f)
Split a sequence [first,last) by the separators sep and pass the tokens as begin-end iterator pair to...
Definition: Algorithm.hpp:15