Schnek
range-iteration.hpp
1 /*
2  * range-iteration.hpp
3  *
4  * Created on: 1 Dec 2022
5  * Author: Holger Schmitz
6  * Email: holger@notjustphysics.com
7  *
8  * Copyright 2012-2022 Holger Schmitz
9  *
10  * This file is part of Schnek.
11  *
12  * Schnek is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * Schnek is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with Schnek. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 #ifndef SCHNEK_GRID_ITERATION_RANGEITERATION_HPP_
27 #define SCHNEK_GRID_ITERATION_RANGEITERATION_HPP_
28 
29 #include "../../config.hpp"
30 #include "../array.hpp"
31 
32 namespace schnek {
33 
39  template<size_t rank>
52  template<
53  class RangeType,
54  typename Func
55  >
56  static void forEach(const RangeType& range, Func func);
57  };
58 
64  template<size_t rank>
77  template<
78  class RangeType,
79  typename Func
80  >
81  static void forEach(const RangeType& range, Func func);
82  };
83 
84  //=================================================================
85  //==================== RangeCIterationPolicy ======================
86  //=================================================================
87 
88  namespace internal {
89  template<size_t rank, size_t dim>
91 
92  template<size_t rank>
93  struct RangeCIterationPolicyImpl<rank, 0> {
94  template<
95  class RangeType,
96  class IndexType,
97  typename Func
98  >
99  static void forEach(
100  const RangeType&,
101  IndexType& pos,
102  Func func
103  )
104  {
105  func(pos);
106  }
107  };
108 
109  template<size_t rank, size_t dim>
111  template<
112  class RangeType,
113  class IndexType,
114  typename Func
115  >
116  static void forEach(
117  const RangeType& range,
118  IndexType& pos,
119  Func func
120  )
121  {
122  constexpr size_t idim = rank - dim;
123  auto lo = range.getLo()[idim];
124  auto hi = range.getHi()[idim];
125  for (pos[idim]=lo; pos[idim]<=hi; ++pos[idim])
126  {
128  }
129  }
130  };
131  }
132 
133  template<size_t rank>
134  template<
135  class RangeType,
136  typename Func
137  >
138  inline void RangeCIterationPolicy<rank>::forEach(const RangeType& range, Func func)
139  {
140  auto pos = range.getLo();
142  }
143 
144  //=================================================================
145  //==================== RangeFortranIterationPolicy ================
146  //=================================================================
147 
148  namespace internal
149  {
150  template<size_t rank>
152 
153  template<>
155  template<
156  class RangeType,
157  class IndexType,
158  typename Func
159  >
160  static void forEach(
161  const RangeType&,
162  IndexType& pos,
163  Func func
164  )
165  {
166  func(pos);
167  }
168  };
169 
170  template<size_t rank>
172  template<
173  class RangeType,
174  class IndexType,
175  typename Func
176  >
177  static void forEach(
178  const RangeType& range,
179  IndexType& pos,
180  Func func
181  )
182  {
183  constexpr size_t dim = rank - 1;
184  auto lo = range.getLo()[dim];
185  auto hi = range.getHi()[dim];
186  for (pos[dim]=lo; pos[dim]<=hi; ++pos[dim])
187  {
189  }
190  }
191  };
192 
193  }
194 
195  template<size_t rank>
196  template<
197  class RangeType,
198  typename Func
199  >
200  inline void RangeFortranIterationPolicy<rank>::forEach(const RangeType& range, Func func)
201  {
202  auto pos = range.getLo();
204  }
205 
206 } // namespace schnek
207 
208 #endif // SCHNEK_GRID_ITERATION_RANGEITERATION_HPP_
Definition: range-iteration.hpp:151
Definition: algo.hpp:30
Iteration policy that iterates over a domain in C-order.
Definition: range-iteration.hpp:40
static void forEach(const RangeType &range, Func func)
Call a function for each index in the range.
Definition: range-iteration.hpp:138
static void forEach(const RangeType &range, Func func)
Call a function for each index in the range.
Definition: range-iteration.hpp:200
Definition: range-iteration.hpp:90
Iteration policy that iterates over a domain in Fortran-order.
Definition: range-iteration.hpp:65