Line data Source code
1 : // The libMesh Finite Element Library. 2 : // Copyright (C) 2002-2025 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner 3 : 4 : // This library is free software; you can redistribute it and/or 5 : // modify it under the terms of the GNU Lesser General Public 6 : // License as published by the Free Software Foundation; either 7 : // version 2.1 of the License, or (at your option) any later version. 8 : 9 : // This library is distributed in the hope that it will be useful, 10 : // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 : // Lesser General Public License for more details. 13 : 14 : // You should have received a copy of the GNU Lesser General Public 15 : // License along with this library; if not, write to the Free Software 16 : // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 : 18 : 19 : 20 : #ifndef LIBMESH_PARALLEL_CONVERSION_UTILS_H 21 : #define LIBMESH_PARALLEL_CONVERSION_UTILS_H 22 : 23 : // Local includes 24 : #include "libmesh/libmesh_common.h" 25 : #include "libmesh/int_range.h" 26 : 27 : #ifdef LIBMESH_HAVE_LIBHILBERT 28 : #include "hilbert.h" 29 : #endif 30 : 31 : // C++ includes 32 : #include <vector> 33 : 34 : namespace libMesh 35 : { 36 : 37 : 38 : 39 : //-------------------------------------------------------------------------- 40 : namespace Parallel { 41 : namespace Utils { 42 : 43 : /** 44 : * \returns \p true if the vector \p v is sorted, \p false otherwise. 45 : * 46 : * This was implemented because std::is_sorted() was an STL extension 47 : * at the time. 48 : */ 49 : #ifdef LIBMESH_ENABLE_DEPRECATED 50 : template <typename KeyType> 51 : inline 52 : bool is_sorted (const std::vector<KeyType> & v) 53 : { 54 : libmesh_deprecated(); 55 : return std::is_sorted(v.begin(), v.end()); 56 : } 57 : #endif // LIBMESH_ENABLE_DEPRECATED 58 : 59 : /** 60 : * A utility function which converts whatever \p KeyType is to 61 : * a \p double for the histogram bounds 62 : */ 63 : template <typename KeyType> 64 : inline 65 208 : double to_double (const KeyType & k) 66 : { 67 342 : return static_cast<double>(k); 68 : } 69 : 70 : /** 71 : * A utility to convert a \p double to some sort of \p KeyType, for 72 : * interpreting how histogram bounds relate to \p KeyType positions. 73 : * 74 : * This is a class to allow partial template specialization for the 75 : * std::pair case without adding a "dummy" variable. 76 : */ 77 : template <typename KeyType> 78 : struct Convert { 79 : inline static 80 202 : KeyType to_key_type (const double f) 81 : { 82 35172 : return static_cast<KeyType>(f); 83 : } 84 : }; 85 : 86 : /** 87 : * A pseudoinverse for converting bounds back to pairs of key types. 88 : */ 89 : template <typename FirstKeyType, typename SecondKeyType> 90 : struct Convert<std::pair<FirstKeyType, SecondKeyType>> { 91 : inline static 92 1538028 : std::pair<FirstKeyType,SecondKeyType> to_key_type (const double f) 93 : { 94 : return std::make_pair 95 1538028 : (Convert<FirstKeyType>::to_key_type(f),SecondKeyType()); 96 : } 97 : }; 98 : 99 : 100 : 101 : /** 102 : * A utility function for pairs of key types. When finding bounds, 103 : * the second entry of the pair is effectively "rounded away". 104 : */ 105 : template <typename FirstKeyType, typename SecondKeyType> 106 : inline 107 1583712 : double to_double (const std::pair<FirstKeyType,SecondKeyType> &k) 108 : { 109 1598940 : return to_double(k.first); 110 : } 111 : 112 : 113 : #ifdef LIBMESH_HAVE_LIBHILBERT 114 : 115 : template <> 116 : inline 117 1583712 : double to_double (const Hilbert::HilbertIndices & bvt) 118 : { 119 2949000 : return static_cast<double>(bvt.rack2); 120 : } 121 : 122 : template <> 123 : struct Convert<Hilbert::HilbertIndices> { 124 : inline static 125 : Hilbert::HilbertIndices 126 1538028 : to_key_type (const double f) 127 : { 128 1538028 : Hilbert::HilbertIndices bvt; 129 : 130 1538028 : bvt.rack0 = 0; 131 1538028 : bvt.rack1 = 0; 132 365802408 : bvt.rack2 = static_cast<Hilbert::inttype>(f); 133 : 134 1538028 : return bvt; 135 : } 136 : }; 137 : #endif // LIBMESH_HAVE_LIBHILBERT 138 : } 139 : } 140 : 141 : } // namespace libMesh 142 : 143 : #endif // LIBMESH_PARALLEL_CONVERSION_UTILS_H