19#ifndef LIBMESH_UTILITY_H
20#define LIBMESH_UTILITY_H
23#include "libmesh/libmesh_common.h"
39#define libmesh_map_find(map, key) libMesh::Utility::map_find((map), (key), __FILE__, __LINE__)
46#define libmesh_vector_at(vec, idx) libMesh::Utility::vector_at((vec), (idx), __FILE__, __LINE__)
68 static auto test(
const U* u) ->
decltype(std::cout << *u);
70 static auto test(...) -> std::false_type;
85#if defined(__GNUC__) && !defined(__INTEL_COMPILER) && !defined(__clang__)
87#pragma GCC diagnostic push
88#pragma GCC diagnostic ignored "-Wdangling-reference"
101template<
typename Map,
typename Key,
102 typename std::enable_if<!is_streamable<Key>::value, Key>::type* =
nullptr>
104typename Map::mapped_type &
107 const char * filename,
110 auto it = map.find(key);
111 libmesh_error_msg_if(it == map.end(),
112 "map_find() error: key not found in file "
113 << filename <<
" on line " << line_number);
120template<
typename Map,
typename Key,
121 typename std::enable_if<!is_streamable<Key>::value, Key>::type* =
nullptr>
123const typename Map::mapped_type &
126 const char * filename,
129 auto it = map.find(key);
130 libmesh_error_msg_if(it == map.end(),
131 "map_find() error: key not found in file "
132 << filename <<
" on line " << line_number);
140template<
typename Map,
typename Key,
141 typename std::enable_if<is_streamable<Key>::value, Key>::type* =
nullptr>
143typename Map::mapped_type &
146 const char * filename,
149 auto it = map.find(key);
150 libmesh_error_msg_if(it == map.end(),
151 "map_find() error: key \"" << key <<
"\" not found in file "
152 << filename <<
" on line " << line_number);
159template<
typename Map,
typename Key,
160 typename std::enable_if<is_streamable<Key>::value, Key>::type* =
nullptr>
162const typename Map::mapped_type &
165 const char * filename,
168 auto it = map.find(key);
169 libmesh_error_msg_if(it == map.end(),
170 "map_find() error: key \"" << key <<
"\" not found in file "
171 << filename <<
" on line " << line_number);
177#if defined(__GNUC__) && !defined(__INTEL_COMPILER) && !defined(__clang__)
179#pragma GCC diagnostic pop
189template<
typename Vector>
191typename Vector::reference &
193 typename Vector::size_type i,
194 const char * filename,
197 libmesh_error_msg_if(i >= vec.size(),
198 "vec_at() error: Index " << i <<
199 " past end of vector in file " << filename <<
200 " on line " << line_number);
207template<
typename Vector>
209typename Vector::const_reference &
211 typename Vector::size_type i,
212 const char * filename,
215 libmesh_error_msg_if(i >= vec.size(),
216 "vec_at() error: Index " << i <<
217 " past end of vector in file " << filename <<
218 " on line " << line_number);
232template<
class ForwardIterator,
class T>
235 ForwardIterator it = std::lower_bound(first, last,
value);
236 return (it == last ||
value < *it) ? last : it;
242template<
class ForwardIterator,
class T,
class Compare>
243ForwardIterator
binary_find(ForwardIterator first, ForwardIterator last,
const T &
value, Compare comp)
245 ForwardIterator it = std::lower_bound(first, last,
value, comp);
246 return (it == last || comp(
value,*it)) ? last : it;
254template <
int N,
typename T>
265 return xNover2*xNover2;
285 static inline T
apply (
const T & x) {
return x; }
290 static inline T
apply (
const T &) {
return 1; }
294template <
int N,
typename T>
308 unsigned int factorial_n = 1;
313 for (
unsigned int i=1; i<n; i++)
331 for (T i = 0; i < k; ++i)
347 std::vector<T>().swap(vec);
354std::string_view
basename_of(
const std::string & fullname);
360bool contains(std::string_view superstring,
361 std::string_view substring);
367bool ends_with(std::string_view superstring,
368 std::string_view suffix);
373#ifdef LIBMESH_USE_COMPLEX_NUMBERS
381 unsigned int r_o_c=0);
387 std::vector<Real> & real_part,
388 std::vector<Real> & imag_part);
396int mkdir(
const char* pathname);
406std::string
unzip_file (std::string_view name);
432 template <
typename T>
464 unsigned char *
b = (
unsigned char *) &data;
467 int j=(
sizeof(T) - 1);
471 std::swap (
b[i],
b[j]);
499 bool operator()(
const std::unique_ptr<T> & a,
const std::unique_ptr<T> &
b)
const
501 return a.get() <
b.get();
508 bool operator()(
const std::unique_ptr<T> & a,
const T *
const &
b)
const
517 bool operator()(
const T *
const & a,
const std::unique_ptr<T> &
b)
const
This Functor simply takes an object and reverses its byte representation.
T operator()(T &data) const
Functor.
const bool _do_reverse
flag
ReverseBytes(const bool dr)
Constructor.
Helper struct for enabling template metaprogramming/SFINAE.
static auto test(const U *u) -> decltype(std::cout<< *u)
static auto test(...) -> std::false_type
unsigned int factorial(unsigned int n)
A simple implementation of the factorial.
int mkdir(const char *pathname)
Create a directory.
ForwardIterator binary_find(ForwardIterator first, ForwardIterator last, const T &value)
The STL provides std::binary_search() which returns true or false depending on whether the searched-f...
Map::mapped_type & map_find(Map &map, const Key &key, const char *filename, int line_number)
-Wdangling-reference was nowhere near ready to add to -Wall in gcc 13.
Vector::reference & vector_at(Vector &vec, typename Vector::size_type i, const char *filename, int line_number)
A replacement for std::vector::at(i) which is meant to be used with a macro, and, unlike at(),...
void deallocate(std::vector< T > &vec)
A convenient method to truly empty a vector using the "swap trick".
bool contains(std::string_view superstring, std::string_view substring)
Look for a substring within a string.
std::string system_info()
std::string complex_filename(std::string basename, unsigned int r_o_c=0)
void prepare_complex_data(const std::vector< Complex > &source, std::vector< Real > &real_part, std::vector< Real > &imag_part)
Prepare complex data for writing.
bool ends_with(std::string_view superstring, std::string_view suffix)
Look for a substring at the very end of a string.
std::string_view basename_of(const std::string &fullname)
std::string unzip_file(std::string_view name)
Create an unzipped copy of a bz2 or xz file, returning the name of the now-unzipped file that can be ...
The libMesh namespace provides an interface to certain functionality in the library.
Struct which defines a custom comparison object that can be used with std::sets of std::unique_ptrs.
void is_transparent
As of C++14, std::set::find() can be a templated overload.
bool operator()(const std::unique_ptr< T > &a, const T *const &b) const
operator< comparison when rhs is a dumb pointer
bool operator()(const std::unique_ptr< T > &a, const std::unique_ptr< T > &b) const
This is already what the default operator< comparison for std::unique_ptrs does, we are not adding an...
bool operator()(const T *const &a, const std::unique_ptr< T > &b) const
operator< comparison when lhs is a dumb pointer
static T apply(const T &)
static T apply(const T &x)
static T apply(const T &x)
An efficient template instantiation for raising to an arbitrary integer power.
static T apply(const T &x)