From: Daniel Arndt Date: Mon, 2 Mar 2020 19:55:24 +0000 (-0500) Subject: Use a template for Utilities::reverse_permutation and Utilities::invert_permutation X-Git-Tag: v9.2.0-rc1~478^2~1 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=56bb8618700200f2051f68bb787afdfda0bd8621;p=dealii.git Use a template for Utilities::reverse_permutation and Utilities::invert_permutation --- diff --git a/include/deal.II/base/utilities.h b/include/deal.II/base/utilities.h index 096f93dcab..887576437c 100644 --- a/include/deal.II/base/utilities.h +++ b/include/deal.II/base/utilities.h @@ -562,32 +562,18 @@ namespace Utilities * $p_i\in [0,N)$ and $p_i\neq p_j$ for $i\neq j$), produce the reverse * permutation $q_i=N-1-p_i$. */ - std::vector - reverse_permutation(const std::vector &permutation); + template + std::vector + reverse_permutation(const std::vector &permutation); /** * Given a permutation vector (i.e. a vector $p_0\ldots p_{N-1}$ where each * $p_i\in [0,N)$ and $p_i\neq p_j$ for $i\neq j$), produce the inverse * permutation $q_0\ldots q_{N-1}$ so that $q_{p_i}=p_{q_i}=i$. */ - std::vector - invert_permutation(const std::vector &permutation); - - /** - * Given a permutation vector (i.e. a vector $p_0\ldots p_{N-1}$ where each - * $p_i\in [0,N)$ and $p_i\neq p_j$ for $i\neq j$), produce the reverse - * permutation $q_i=N-1-p_i$. - */ - std::vector - reverse_permutation(const std::vector &permutation); - - /** - * Given a permutation vector (i.e. a vector $p_0\ldots p_{N-1}$ where each - * $p_i\in [0,N)$ and $p_i\neq p_j$ for $i\neq j$), produce the inverse - * permutation $q_0\ldots q_{N-1}$ so that $q_{p_i}=p_{q_i}=i$. - */ - std::vector - invert_permutation(const std::vector &permutation); + template + std::vector + invert_permutation(const std::vector &permutation); /** * Given an arbitrary object of type T, use boost::serialization utilities @@ -1442,6 +1428,45 @@ namespace Utilities allow_compression); } + + + 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; + } + + + + template + 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; + } } // namespace Utilities diff --git a/source/base/utilities.cc b/source/base/utilities.cc index 3db17cab85..7f918ad5e4 100644 --- a/source/base/utilities.cc +++ b/source/base/utilities.cc @@ -868,78 +868,6 @@ namespace Utilities - 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; - } - - - - template - 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; - } - - - - std::vector - reverse_permutation(const std::vector &permutation) - { - return reverse_permutation(permutation); - } - - - - std::vector - invert_permutation(const std::vector &permutation) - { - return invert_permutation(permutation); - } - - - - std::vector - reverse_permutation(const std::vector &permutation) - { - return reverse_permutation(permutation); - } - - - - std::vector - invert_permutation(const std::vector &permutation) - { - return invert_permutation(permutation); - } - - - namespace System { #if defined(__linux__)