]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Remove duplicate code of invert_permutation 9590/head
authorMartin Kronbichler <kronbichler@lnm.mw.tum.de>
Mon, 2 Mar 2020 13:31:42 +0000 (14:31 +0100)
committerMartin Kronbichler <kronbichler@lnm.mw.tum.de>
Mon, 2 Mar 2020 15:49:14 +0000 (16:49 +0100)
source/base/utilities.cc

index 1c1019a4e3c7a45d6d5a6ab26b68bb591e30dc2d..3db17cab856295bf6db2afd6ecb4dee88d43efff 100644 (file)
@@ -868,13 +868,14 @@ namespace Utilities
 
 
 
-  std::vector<unsigned int>
-  reverse_permutation(const std::vector<unsigned int> &permutation)
+  template <typename Integer>
+  std::vector<Integer>
+  reverse_permutation(const std::vector<Integer> &permutation)
   {
-    const unsigned int n = permutation.size();
+    const std::size_t n = permutation.size();
 
-    std::vector<unsigned int> out(n);
-    for (unsigned int i = 0; i < n; ++i)
+    std::vector<Integer> out(n);
+    for (std::size_t i = 0; i < n; ++i)
       out[i] = n - 1 - permutation[i];
 
     return out;
@@ -882,14 +883,15 @@ namespace Utilities
 
 
 
-  std::vector<unsigned int>
-  invert_permutation(const std::vector<unsigned int> &permutation)
+  template <typename Integer>
+  std::vector<Integer>
+  invert_permutation(const std::vector<Integer> &permutation)
   {
-    const unsigned int n = permutation.size();
+    const std::size_t n = permutation.size();
 
-    std::vector<unsigned int> out(n, numbers::invalid_unsigned_int);
+    std::vector<Integer> out(n, numbers::invalid_unsigned_int);
 
-    for (unsigned int i = 0; i < n; ++i)
+    for (std::size_t i = 0; i < n; ++i)
       {
         AssertIndexRange(permutation[i], n);
         out[permutation[i]] = i;
@@ -904,79 +906,36 @@ namespace Utilities
     return out;
   }
 
-  std::vector<std::uint64_t>
-  reverse_permutation(const std::vector<std::uint64_t> &permutation)
-  {
-    const std::uint64_t n = permutation.size();
 
-    std::vector<std::uint64_t> out(n);
-    for (std::uint64_t i = 0; i < n; ++i)
-      out[i] = n - 1 - permutation[i];
 
-    return out;
+  std::vector<unsigned int>
+  reverse_permutation(const std::vector<unsigned int> &permutation)
+  {
+    return reverse_permutation<unsigned int>(permutation);
   }
 
 
 
-  std::vector<std::uint64_t>
-  invert_permutation(const std::vector<std::uint64_t> &permutation)
+  std::vector<unsigned int>
+  invert_permutation(const std::vector<unsigned int> &permutation)
   {
-    const std::uint64_t n = permutation.size();
-
-    std::vector<std::uint64_t> out(n, numbers::invalid_dof_index);
-
-    for (std::uint64_t i = 0; i < n; ++i)
-      {
-        AssertIndexRange(permutation[i], n);
-        out[permutation[i]] = i;
-      }
-
-    // check that we have actually reached
-    // all indices
-    for (std::uint64_t i = 0; i < n; ++i)
-      Assert(out[i] != numbers::invalid_unsigned_int,
-             ExcMessage("The given input permutation had duplicate entries!"));
-
-    return out;
+    return invert_permutation<unsigned int>(permutation);
   }
 
 
-  template <typename Integer>
-  std::vector<Integer>
-  reverse_permutation(const std::vector<Integer> &permutation)
-  {
-    const std::size_t n = permutation.size();
-
-    std::vector<Integer> out(n);
-    for (std::size_t i = 0; i < n; ++i)
-      out[i] = n - 1 - permutation[i];
 
-    return out;
+  std::vector<std::uint64_t>
+  reverse_permutation(const std::vector<std::uint64_t> &permutation)
+  {
+    return reverse_permutation<std::uint64_t>(permutation);
   }
 
 
 
-  template <typename Integer>
-  std::vector<Integer>
-  invert_permutation(const std::vector<Integer> &permutation)
+  std::vector<std::uint64_t>
+  invert_permutation(const std::vector<std::uint64_t> &permutation)
   {
-    const std::size_t n = permutation.size();
-
-    std::vector<Integer> out(n, numbers::invalid_unsigned_int);
-
-    for (std::size_t i = 0; i < n; ++i)
-      {
-        AssertIndexRange(permutation[i], n);
-        out[permutation[i]] = i;
-      }
-
-    // check that we have actually reached
-    // all indices
-    for (unsigned int i = 0; i < n; ++i)
-      Assert(out[i] != numbers::invalid_unsigned_int,
-             ExcMessage("The given input permutation had duplicate entries!"));
-
-    return out;
+    return invert_permutation<std::uint64_t>(permutation);
   }
 
 

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.