libMesh
parallel_conversion_utils.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 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 
50 template <typename KeyType>
51 inline
52 bool is_sorted (const std::vector<KeyType> & v)
53 {
54  if (v.empty())
55  return true;
56 
57  for (auto i : IntRange<std::size_t>(1, v.size()))
58  if (v[i] < v[i-1])
59  return false;
60 
61  return true;
62 }
63 
68 template <typename KeyType>
69 inline
70 double to_double (const KeyType & k)
71 {
72  return static_cast<double>(k);
73 }
74 
82 template <typename KeyType>
83 struct Convert {
84  inline static
85  KeyType to_key_type (const double f)
86  {
87  return static_cast<KeyType>(f);
88  }
89 };
90 
94 template <typename FirstKeyType, typename SecondKeyType>
95 struct Convert<std::pair<FirstKeyType, SecondKeyType>> {
96  inline static
97  std::pair<FirstKeyType,SecondKeyType> to_key_type (const double f)
98  {
99  return std::make_pair
100  (Convert<FirstKeyType>::to_key_type(f),SecondKeyType());
101  }
102 };
103 
104 
105 
110 template <typename FirstKeyType, typename SecondKeyType>
111 inline
112 double to_double (const std::pair<FirstKeyType,SecondKeyType> &k)
113 {
114  return to_double(k.first);
115 }
116 
117 
118 #ifdef LIBMESH_HAVE_LIBHILBERT
119 
120 template <>
121 inline
122 double to_double (const Hilbert::HilbertIndices & bvt)
123 {
124  return static_cast<double>(bvt.rack2);
125 }
126 
127 template <>
128 struct Convert<Hilbert::HilbertIndices> {
129  inline static
130  Hilbert::HilbertIndices
131  to_key_type (const double f)
132  {
133  Hilbert::HilbertIndices bvt;
134 
135  bvt.rack0 = 0;
136  bvt.rack1 = 0;
137  bvt.rack2 = static_cast<Hilbert::inttype>(f);
138 
139  return bvt;
140  }
141 };
142 #endif // LIBMESH_HAVE_LIBHILBERT
143 }
144 }
145 
146 } // namespace libMesh
147 
148 #endif // LIBMESH_PARALLEL_CONVERSION_UTILS_H
libMesh::Parallel::Utils::Convert< std::pair< FirstKeyType, SecondKeyType > >::to_key_type
static std::pair< FirstKeyType, SecondKeyType > to_key_type(const double f)
Definition: parallel_conversion_utils.h:97
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::Parallel::Utils::Convert::to_key_type
static KeyType to_key_type(const double f)
Definition: parallel_conversion_utils.h:85
libMesh::Parallel::Utils::is_sorted
bool is_sorted(const std::vector< KeyType > &v)
Definition: parallel_conversion_utils.h:52
libMesh::IntRange
The IntRange templated class is intended to make it easy to loop over integers which are indices of a...
Definition: int_range.h:53
libMesh::Parallel::Utils::to_double
double to_double(const KeyType &k)
A utility function which converts whatever KeyType is to a double for the histogram bounds.
Definition: parallel_conversion_utils.h:70
std
Definition: float128_shims.h:27
libMesh::Parallel::Utils::Convert
A utility to convert a double to some sort of KeyType, for interpreting how histogram bounds relate t...
Definition: parallel_conversion_utils.h:83
Hilbert
Definition: parallel_hilbert.h:87
libMesh::Parallel::Utils::Convert< Hilbert::HilbertIndices >::to_key_type
static Hilbert::HilbertIndices to_key_type(const double f)
Definition: parallel_conversion_utils.h:131