]> https://gitweb.dealii.org/ - dealii.git/commitdiff
More Tensor simplifications with if constexpr. 16473/head
authorWolfgang Bangerth <bangerth@colostate.edu>
Mon, 15 Jan 2024 15:52:18 +0000 (08:52 -0700)
committerWolfgang Bangerth <bangerth@colostate.edu>
Mon, 15 Jan 2024 15:52:18 +0000 (08:52 -0700)
include/deal.II/base/tensor.h

index 0ff7e0fdeb5af122074ae8151ca0366761482f6f..03c49559b7c87c80e52965bb9d0a76b96938e7ff 100644 (file)
@@ -1757,45 +1757,6 @@ Tensor<rank_, dim, Number>::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 <int dim>
-  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 <int dim>
-  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 <int rank_, int dim, typename Number>
 constexpr inline TableIndices<rank_>
 Tensor<rank_, dim, Number>::unrolled_to_component_indices(const unsigned int i)
@@ -1805,17 +1766,28 @@ Tensor<rank_, dim, Number>::unrolled_to_component_indices(const unsigned int i)
   AssertIndexRange(i, dummy);
   (void)dummy;
 
-  TableIndices<rank_> indices;
-
-  unsigned int remainder = i;
-  for (int r = rank_ - 1; r >= 0; --r)
+  if constexpr (dim == 0)
     {
-      indices[r] = internal::mod<dim>(remainder);
-      remainder  = internal::div<dim>(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<rank_> 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;
+    }
 }
 
 

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.