From: Martin Kronbichler Date: Mon, 2 Mar 2020 13:31:42 +0000 (+0100) Subject: Remove duplicate code of invert_permutation X-Git-Tag: v9.2.0-rc1~482^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=212b4fd17c04474d5ecba24136045d9db230ec2b;p=dealii.git Remove duplicate code of invert_permutation --- diff --git a/source/base/utilities.cc b/source/base/utilities.cc index 1c1019a4e3..3db17cab85 100644 --- a/source/base/utilities.cc +++ b/source/base/utilities.cc @@ -868,13 +868,14 @@ namespace Utilities - std::vector - reverse_permutation(const std::vector &permutation) + template + std::vector + reverse_permutation(const std::vector &permutation) { - const unsigned int n = permutation.size(); + const std::size_t n = permutation.size(); - std::vector out(n); - for (unsigned int i = 0; i < n; ++i) + std::vector 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 - invert_permutation(const std::vector &permutation) + template + std::vector + invert_permutation(const std::vector &permutation) { - const unsigned int n = permutation.size(); + const std::size_t n = permutation.size(); - std::vector out(n, numbers::invalid_unsigned_int); + std::vector 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 - reverse_permutation(const std::vector &permutation) - { - const std::uint64_t n = permutation.size(); - std::vector out(n); - for (std::uint64_t i = 0; i < n; ++i) - out[i] = n - 1 - permutation[i]; - return out; + std::vector + reverse_permutation(const std::vector &permutation) + { + return reverse_permutation(permutation); } - std::vector - invert_permutation(const std::vector &permutation) + std::vector + invert_permutation(const std::vector &permutation) { - const std::uint64_t n = permutation.size(); - - std::vector 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(permutation); } - template - std::vector - reverse_permutation(const std::vector &permutation) - { - const std::size_t n = permutation.size(); - - std::vector out(n); - for (std::size_t i = 0; i < n; ++i) - out[i] = n - 1 - permutation[i]; - return out; + std::vector + reverse_permutation(const std::vector &permutation) + { + return reverse_permutation(permutation); } - template - std::vector - invert_permutation(const std::vector &permutation) + std::vector + invert_permutation(const std::vector &permutation) { - const std::size_t n = permutation.size(); - - std::vector 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(permutation); }