21 #ifndef THUNDEREGG_VIEW_H
22 #define THUNDEREGG_VIEW_H
28 #include <ThunderEgg/Config.h>
39 template<
typename T,
int D>
43 using T_ptr =
typename std::add_pointer<T>::type;
53 std::array<int, D> strides;
57 std::array<int, D> ghost_start;
61 std::array<int, D> start;
65 std::array<int, D> end;
69 std::array<int, D> ghost_end;
77 void checkCoordIsInBounds(
const std::array<int, D>& coord)
const
79 bool is_valid_coord =
true;
80 for (
int i = 0; i < D; i++) {
81 is_valid_coord = is_valid_coord && (coord[i] >= ghost_start[i] && coord[i] <= ghost_end[i]);
83 if (!is_valid_coord) {
90 inline int getIndex(
const std::array<int, D>& coord)
const
93 Loop::Unroll<0, D - 1>([&](
int i) { idx += strides[i] * coord[i]; });
97 T_ptr getData()
const {
return data; }
121 const std::array<int, D>& strides,
122 const std::array<int, D>& ghost_start,
123 const std::array<int, D>& start,
124 const std::array<int, D>& end,
125 const std::array<int, D>& ghost_end)
127 , ghost_start(ghost_start)
130 , ghost_end(ghost_end)
132 this->data = data - this->getIndex(ghost_start);
143 if constexpr (ENABLE_DEBUG) {
144 this->checkCoordIsInBounds(coord);
146 return data[this->getIndex(coord)];
158 template<
class... Types>
161 static_assert(
sizeof...(args) == D,
"incorrect number of arguments");
162 if constexpr (ENABLE_DEBUG) {
163 this->checkCoordIsInBounds({ args... });
165 return data[this->getIndex({ args... })];
174 inline void set(
const std::array<int, D>& coord, T value)
const
176 if constexpr (std::is_const<T>::value) {
177 if constexpr (ENABLE_DEBUG) {
179 bool is_interior_coord =
true;
180 for (
int i = 0; i < D; i++) {
181 is_interior_coord = is_interior_coord && (coord[i] >= start[i] && coord[i] <= end[i]);
183 if (is_interior_coord) {
185 throw RuntimeError(
"interior value of const view is being modified");
187 checkCoordIsInBounds(coord);
190 Loop::Unroll<0, D - 1>([&](
int i) { idx += strides[i] * coord[i]; });
191 const_cast<double*
>(data)[idx] = value;
193 if constexpr (ENABLE_DEBUG) {
194 this->checkCoordIsInBounds(coord);
196 data[this->getIndex(coord)] = value;
203 const std::array<int, D>&
getStrides()
const {
return strides; }
207 const std::array<int, D>&
getStart()
const {
return start; }
211 const std::array<int, D>&
getEnd()
const {
return end; }
219 const std::array<int, D>&
getGhostEnd()
const {
return ghost_end; }
224 data + getIndex(
getGhostStart()), strides, ghost_start, start, end, ghost_end);
227 extern template class View<double, 1>;
228 extern template class View<double, 2>;
229 extern template class View<double, 3>;
230 extern template class View<double, 4>;
231 extern template class View<const double, 1>;
232 extern template class View<const double, 2>;
233 extern template class View<const double, 3>;
234 extern template class View<const double, 4>;