reduce redundancy

This commit is contained in:
Francesco Rizzi 2023-06-28 18:45:13 +02:00
parent 17912ba667
commit 136c664b5a
1 changed files with 10 additions and 12 deletions

View File

@ -1093,18 +1093,14 @@ public:
// Reference to element at a given index // Reference to element at a given index
template <typename... Ix> template <typename... Ix>
const T &at(Ix... index) const { const T &at(Ix... index) const {
if ((ssize_t) sizeof...(index) != ndim()) { check_dim_precondition(sizeof...(index));
fail_dim_check(sizeof...(index), "index dimension mismatch");
}
return const_reference(index...); return const_reference(index...);
} }
// Mutable reference to element at a given index // Mutable reference to element at a given index
template <typename... Ix> template <typename... Ix>
T &mutable_at(Ix... index) { T &mutable_at(Ix... index) {
if ((ssize_t) sizeof...(index) != ndim()) { check_dim_precondition(sizeof...(index));
fail_dim_check(sizeof...(index), "index dimension mismatch");
}
return mutable_reference(index...); return mutable_reference(index...);
} }
@ -1112,9 +1108,7 @@ public:
template <typename... Ix> template <typename... Ix>
const T &operator()(Ix... index) const { const T &operator()(Ix... index) const {
#if !defined(NDEBUG) #if !defined(NDEBUG)
if ((ssize_t) sizeof...(index) != ndim()) { check_dim_precondition(sizeof...(index));
fail_dim_check(sizeof...(index), "index dimension mismatch");
}
#endif #endif
return const_reference(index...); return const_reference(index...);
} }
@ -1123,9 +1117,7 @@ public:
template <typename... Ix> template <typename... Ix>
T &operator()(Ix... index) { T &operator()(Ix... index) {
#if !defined(NDEBUG) #if !defined(NDEBUG)
if ((ssize_t) sizeof...(index) != ndim()) { check_dim_precondition(sizeof...(index));
fail_dim_check(sizeof...(index), "index dimension mismatch");
}
#endif #endif
return mutable_reference(index...); return mutable_reference(index...);
} }
@ -1199,6 +1191,12 @@ private:
return *(static_cast<T *>(array::mutable_data()) return *(static_cast<T *>(array::mutable_data())
+ byte_offset(ssize_t(index)...) / itemsize()); + byte_offset(ssize_t(index)...) / itemsize());
} }
void check_dim_precondition(ssize_t dim) const {
if (dim != ndim()) {
fail_dim_check(dim, "index dimension mismatch");
}
}
}; };
template <typename T> template <typename T>