parallel_conversion_utils.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 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 
26 #ifdef LIBMESH_HAVE_LIBHILBERT
27 #include "hilbert.h"
28 #endif
29 
30 // C++ includes
31 #include <vector>
32 
33 namespace libMesh
34 {
35 
36 
37 
38 //--------------------------------------------------------------------------
39 namespace Parallel {
40 namespace Utils {
41 
49 template <typename KeyType>
50 inline
51 bool is_sorted (const std::vector<KeyType> & v)
52 {
53  if (v.empty())
54  return true;
55 
56  for (std::size_t i=1; i<v.size(); i++)
57  if (v[i] < v[i-1])
58  return false;
59 
60  return true;
61 }
62 
67 template <typename KeyType>
68 inline
69 double to_double (const KeyType & k)
70 {
71  return static_cast<double>(k);
72 }
73 
81 template <typename KeyType>
82 struct Convert {
83  inline static
84  KeyType to_key_type (const double f)
85  {
86  return static_cast<KeyType>(f);
87  }
88 };
89 
93 template <typename FirstKeyType, typename SecondKeyType>
94 struct Convert<std::pair<FirstKeyType, SecondKeyType>> {
95  inline static
96  std::pair<FirstKeyType,SecondKeyType> to_key_type (const double f)
97  {
98  return std::make_pair
99  (Convert<FirstKeyType>::to_key_type(f),SecondKeyType());
100  }
101 };
102 
103 
104 
109 template <typename FirstKeyType, typename SecondKeyType>
110 inline
111 double to_double (const std::pair<FirstKeyType,SecondKeyType> &k)
112 {
113  return to_double(k.first);
114 }
115 
116 
117 #ifdef LIBMESH_HAVE_LIBHILBERT
118 
119 template <>
120 inline
121 double to_double (const Hilbert::HilbertIndices & bvt)
122 {
123  return static_cast<double>(bvt.rack2);
124 }
125 
126 template <>
127 struct Convert<Hilbert::HilbertIndices> {
128  inline static
129  Hilbert::HilbertIndices
130  to_key_type (const double f)
131  {
132  Hilbert::HilbertIndices bvt;
133 
134  bvt.rack0 = 0;
135  bvt.rack1 = 0;
136  bvt.rack2 = static_cast<Hilbert::inttype>(f);
137 
138  return bvt;
139  }
140 };
141 #endif // LIBMESH_HAVE_LIBHILBERT
142 }
143 }
144 
145 } // namespace libMesh
146 
147 #endif // LIBMESH_PARALLEL_CONVERSION_UTILS_H
static Hilbert::HilbertIndices to_key_type(const double f)
bool is_sorted(const std::vector< KeyType > &v)
static std::pair< FirstKeyType, SecondKeyType > to_key_type(const double f)
static KeyType to_key_type(const double f)
double to_double(const KeyType &k)