From: Wolfgang Bangerth Date: Mon, 15 Jan 2024 15:52:18 +0000 (-0700) Subject: More Tensor simplifications with if constexpr. X-Git-Tag: relicensing~159^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=32c6cc7c0f50ecb63014217b1d41de911ea64017;p=dealii.git More Tensor simplifications with if constexpr. --- diff --git a/include/deal.II/base/tensor.h b/include/deal.II/base/tensor.h index 0ff7e0fdeb..03c49559b7 100644 --- a/include/deal.II/base/tensor.h +++ b/include/deal.II/base/tensor.h @@ -1757,45 +1757,6 @@ Tensor::component_to_unrolled_index( -namespace internal -{ - // unrolled_to_component_indices is instantiated from DataOut for dim==0 - // and rank=2. Make sure we don't have compiler warnings. - - template - DEAL_II_HOST_DEVICE inline constexpr unsigned int - mod(const unsigned int x) - { - return x % dim; - } - - template <> - DEAL_II_HOST_DEVICE inline unsigned int - mod<0>(const unsigned int x) - { - Assert(false, ExcInternalError()); - return x; - } - - template - DEAL_II_HOST_DEVICE inline constexpr unsigned int - div(const unsigned int x) - { - return x / dim; - } - - template <> - DEAL_II_HOST_DEVICE inline unsigned int - div<0>(const unsigned int x) - { - Assert(false, ExcInternalError()); - return x; - } - -} // namespace internal - - - template constexpr inline TableIndices Tensor::unrolled_to_component_indices(const unsigned int i) @@ -1805,17 +1766,28 @@ Tensor::unrolled_to_component_indices(const unsigned int i) AssertIndexRange(i, dummy); (void)dummy; - TableIndices indices; - - unsigned int remainder = i; - for (int r = rank_ - 1; r >= 0; --r) + if constexpr (dim == 0) { - indices[r] = internal::mod(remainder); - remainder = internal::div(remainder); + Assert(false, + ExcMessage( + "A tensor with dimension 0 does not store any elements. " + "There is no indexing that can address its elements.")); + return {}; } - Assert(remainder == 0, ExcInternalError()); + else + { + TableIndices indices; - return indices; + unsigned int remainder = i; + for (int r = rank_ - 1; r >= 0; --r) + { + indices[r] = remainder % dim; + remainder = remainder / dim; + } + Assert(remainder == 0, ExcInternalError()); + + return indices; + } }