libMesh
Classes | Functions
libMesh::Utility Namespace Reference

Classes

struct  do_pow
 An efficient template instantiation for raising to an arbitrary integer power. More...
 
struct  do_pow< 0, T >
 
struct  do_pow< 1, T >
 
struct  do_pow< 6, T >
 
class  ReverseBytes
 This Functor simply takes an object and reverses its byte representation. More...
 

Functions

uint32_t hashword (const uint32_t *k, size_t length, uint32_t initval=0)
 The hashword function takes an array of uint32_t's of length 'length' and computes a single key from it. More...
 
uint32_t hashword (const std::vector< uint32_t > &keys, uint32_t initval=0)
 Calls function above with slightly more convenient std::vector interface. More...
 
uint32_t hashword2 (const uint32_t &first, const uint32_t &second, uint32_t initval=0)
 This is a hard-coded version of hashword for hashing exactly 2 numbers. More...
 
uint64_t hashword2 (const uint64_t first, const uint64_t second)
 Call the 64-bit FNV hash function. More...
 
uint16_t hashword2 (const uint16_t first, const uint16_t second)
 
uint64_t hashword (const uint64_t *k, size_t length)
 Call the 64-bit FNV hash function. More...
 
uint16_t hashword (const uint16_t *k, size_t length)
 In a personal communication from Bob Jenkins, he recommended using "Probably final() from lookup3.c... More...
 
template<typename Container >
Container::value_type hashword (const Container &keys)
 Calls functions above with slightly more convenient std::vector/array compatible interface. More...
 
template<typename T >
string_to_enum (const std::string &s)
 
template<typename T >
std::string enum_to_string (const T e)
 
std::string get_timestamp ()
 
void print_timestamp (std::ostream &target=std::cout)
 
std::string system_info ()
 
template<typename ForwardIter , typename T >
void iota (ForwardIter first, ForwardIter last, T value)
 Utility::iota is a duplication of the SGI STL extension std::iota. More...
 
template<class InputIterator >
bool is_sorted (InputIterator first, InputIterator last)
 Utility::is_sorted mimics the behavior of the SGI STL extension std::is_sorted. More...
 
template<class ForwardIterator , class T >
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-for value is found. More...
 
template<class ForwardIterator , class T , class Compare >
ForwardIterator binary_find (ForwardIterator first, ForwardIterator last, const T &value, Compare comp)
 As above, but takes a custom comparison object. More...
 
template<int N, typename T >
pow (const T &x)
 
unsigned int factorial (unsigned int n)
 A simple implementation of the factorial. More...
 
template<typename T >
binomial (T n, T k)
 
template<typename T >
void deallocate (std::vector< T > &vec)
 A convenient method to truly empty a vector using the "swap trick". More...
 
std::string complex_filename (const 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. More...
 
int mkdir (const char *pathname)
 Create a directory. More...
 

Function Documentation

◆ binary_find() [1/2]

template<class ForwardIterator , class T >
ForwardIterator libMesh::Utility::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-for value is found.

In contrast, Utility::binary_find() uses a std::lower_bound() based search on a sorted range to find the required value.

Returns
An iterator to the searched-for element, or "last" if the element is not found.

Definition at line 132 of file utility.h.

133 {
134  ForwardIterator it = std::lower_bound(first, last, value);
135  return (it == last || value < *it) ? last : it;
136 }

◆ binary_find() [2/2]

template<class ForwardIterator , class T , class Compare >
ForwardIterator libMesh::Utility::binary_find ( ForwardIterator  first,
ForwardIterator  last,
const T &  value,
Compare  comp 
)

As above, but takes a custom comparison object.

Definition at line 142 of file utility.h.

143 {
144  ForwardIterator it = std::lower_bound(first, last, value, comp);
145  return (it == last || comp(value,*it)) ? last : it;
146 }

◆ binomial()

template<typename T >
T libMesh::Utility::binomial ( n,
k 
)

Definition at line 221 of file utility.h.

222 {
223  T ret = 1;
224 
225  // Binomial function is "symmetric" in k, C(n, k) = C(n, n-k).
226  if (k > n - k)
227  k = n - k;
228 
229  // Compute n * (n-1) * ... * (n-k+1) / (k * (k-1) * ... * 1)
230  for (T i = 0; i < k; ++i)
231  {
232  ret *= (n - i);
233  ret /= (i + 1);
234  }
235 
236  return ret;
237 }

◆ complex_filename()

std::string libMesh::Utility::complex_filename ( const std::string &  basename,
unsigned int  r_o_c = 0 
)
Returns
For r_o_c = 0 the filename for output of the real part of complex data, and for r_o_c = 1 the filename for the imaginary part.

◆ deallocate()

template<typename T >
void libMesh::Utility::deallocate ( std::vector< T > &  vec)

A convenient method to truly empty a vector using the "swap trick".

Definition at line 244 of file utility.h.

References swap().

245 {
246  std::vector<T>().swap(vec);
247 }
void swap(Iterator &lhs, Iterator &rhs)
swap, used to implement op=

◆ enum_to_string()

template<typename T >
std::string libMesh::Utility::enum_to_string ( const T  e)
Returns
the string which matches the enumeration e of type T.

◆ factorial()

unsigned int libMesh::Utility::factorial ( unsigned int  n)

A simple implementation of the factorial.

Definition at line 204 of file utility.h.

205 {
206 
207  unsigned int factorial_n = 1;
208 
209  if (n==0)
210  return factorial_n;
211 
212  for (unsigned int i=1; i<n; i++)
213  factorial_n *= i+1;
214 
215  return factorial_n;
216 }

◆ get_timestamp()

std::string libMesh::Utility::get_timestamp ( )

Referenced by print_timestamp().

◆ hashword() [1/5]

uint32_t libMesh::Utility::hashword ( const uint32_t *  k,
size_t  length,
uint32_t  initval = 0 
)

The hashword function takes an array of uint32_t's of length 'length' and computes a single key from it.

Author
Bob Jenkins
Date
May 2006

Definition at line 153 of file hashword.h.

Referenced by libMesh::Elem::compute_key(), and hashword().

154 {
155  uint32_t a,b,c;
156 
157  // Set up the internal state
158  a = b = c = 0xdeadbeef + ((static_cast<uint32_t>(length))<<2) + initval;
159 
160  //------------------------------------------------- handle most of the key
161  while (length > 3)
162  {
163  a += k[0];
164  b += k[1];
165  c += k[2];
166  mix(a,b,c);
167  length -= 3;
168  k += 3;
169  }
170 
171  //------------------------------------------- handle the last 3 uint32_t's
172  switch(length) // all the case statements fall through
173  {
174  case 3 : c+=k[2];
175  libmesh_fallthrough();
176  case 2 : b+=k[1];
177  libmesh_fallthrough();
178  case 1 : a+=k[0];
179  final(a,b,c);
180  libmesh_fallthrough();
181  default: // case 0: nothing left to add
182  break;
183  }
184 
185  //------------------------------------------------------ report the result
186  return c;
187 }

◆ hashword() [2/5]

uint32_t libMesh::Utility::hashword ( const std::vector< uint32_t > &  keys,
uint32_t  initval = 0 
)

Calls function above with slightly more convenient std::vector interface.

Definition at line 195 of file hashword.h.

References hashword().

196 {
197  return hashword(&keys[0], keys.size(), initval);
198 }
Container::value_type hashword(const Container &keys)
Calls functions above with slightly more convenient std::vector/array compatible interface.
Definition: hashword.h:304

◆ hashword() [3/5]

uint64_t libMesh::Utility::hashword ( const uint64_t *  k,
size_t  length 
)

Call the 64-bit FNV hash function.

Definition at line 249 of file hashword.h.

250 {
251  return fnv_64_buf(k, 8*length);
252 }

◆ hashword() [4/5]

uint16_t libMesh::Utility::hashword ( const uint16_t *  k,
size_t  length 
)

In a personal communication from Bob Jenkins, he recommended using "Probably final() from lookup3.c...

You could hash up to 6 16-bit integers that way. The output is c, or the top or bottom 16 bits of c if you only need 16 bit hash values." [JWP]

Definition at line 263 of file hashword.h.

264 {
265  // Three values that will be passed to final() after they are initialized.
266  uint32_t a = 0;
267  uint32_t b = 0;
268  uint32_t c = 0;
269 
270  switch (length)
271  {
272  case 3:
273  {
274  // Cast the inputs to 32 bit integers and call final().
275  a = k[0];
276  b = k[1];
277  c = k[2];
278  break;
279  }
280  case 4:
281  {
282  // Combine the 4 16-bit inputs, "w, x, y, z" into two 32-bit
283  // inputs "wx" and "yz" using bit operations and call final.
284  a = ((k[0]<<16) | (k[1] & 0xffff)); // wx
285  b = ((k[2]<<16) | (k[3] & 0xffff)); // yz
286  break;
287  }
288  default:
289  libmesh_error_msg("Unsupported length: " << length);
290  }
291 
292  // Result is returned in c
293  final(a,b,c);
294  return static_cast<uint16_t>(c);
295 }

◆ hashword() [5/5]

template<typename Container >
Container::value_type libMesh::Utility::hashword ( const Container &  keys)

Calls functions above with slightly more convenient std::vector/array compatible interface.

Definition at line 304 of file hashword.h.

References hashword().

305 {
306  return hashword(keys.data(), keys.size());
307 }
Container::value_type hashword(const Container &keys)
Calls functions above with slightly more convenient std::vector/array compatible interface.
Definition: hashword.h:304

◆ hashword2() [1/3]

uint32_t libMesh::Utility::hashword2 ( const uint32_t &  first,
const uint32_t &  second,
uint32_t  initval = 0 
)

This is a hard-coded version of hashword for hashing exactly 2 numbers.

Author
Bob Jenkins
Date
May 2006

Definition at line 210 of file hashword.h.

Referenced by libMesh::Elem::compute_key().

211 {
212  uint32_t a,b,c;
213 
214  // Set up the internal state
215  a = b = c = 0xdeadbeef + 8 + initval;
216 
217  b+=second;
218  a+=first;
219  final(a,b,c);
220 
221  return c;
222 }

◆ hashword2() [2/3]

uint64_t libMesh::Utility::hashword2 ( const uint64_t  first,
const uint64_t  second 
)

Call the 64-bit FNV hash function.

Definition at line 228 of file hashword.h.

229 {
230  // This isn't optimal (it would be nice to avoid this packing step)
231  // but we are going to go ahead and conform to the 32-bit
232  // hashword2() interface.
233  uint64_t k[2] = {first, second};
234 
235  // len is the total number of bytes in two 64-bit ints
236  return fnv_64_buf(k, /*len=*/8*2);
237 }

◆ hashword2() [3/3]

uint16_t libMesh::Utility::hashword2 ( const uint16_t  first,
const uint16_t  second 
)

Definition at line 240 of file hashword.h.

241 {
242  return static_cast<uint16_t>(first%65449 + (second<<5)%65449);
243 }

◆ iota()

template<typename ForwardIter , typename T >
void libMesh::Utility::iota ( ForwardIter  first,
ForwardIter  last,
value 
)

Utility::iota is a duplication of the SGI STL extension std::iota.

It simply assigns sequentially increasing values to a range. That is, it assigns value to *first, value + 1 to *(first + 1) and so on. In general, each iterator i in the range [first, last) is assigned value + (i - first).

Definition at line 57 of file utility.h.

58 {
59  while (first != last)
60  {
61  *first = value++;
62  ++first;
63  }
64 }

◆ is_sorted()

template<class InputIterator >
bool libMesh::Utility::is_sorted ( InputIterator  first,
InputIterator  last 
)

Utility::is_sorted mimics the behavior of the SGI STL extension std::is_sorted.

Checks to see if the range [first,last) is sorted in non-decreasing order, ie. for each "i" in [first,last) *i <= *(i+1).

Definition at line 74 of file utility.h.

References libMesh::MeshTools::Subdivision::prev.

75 {
76  if (first == last)
77  return true;
78 
79  // "prev" always points to the entry just to the left of "first"
80  // [- - - - - -]
81  // ^ ^
82  // prev first
83  //
84  // [- - - - - -]
85  // ^ ^
86  // prev first
87  //
88  // [- - - - - -]
89  // ^ ^
90  // prev first
91  InputIterator prev( first );
92  for (++first; first != last; ++prev, ++first)
93  if (*first < *prev) // Note: this is the same as *prev > *first,
94  return false; // but we only require op< to be defined.
95 
96  // If we haven't returned yet, it's sorted!
97  return true;
98 
99 
100  // A one-liner version using adjacent_find. This doesn't work for
101  // C-style arrays, since their pointers do not have a value_type.
102  //
103  // Works by checking to see if adjacent entries satisfy *i >
104  // *(i+1) and returns the first one which does. If "last" is
105  // returned, no such pair was found, and therefore the range must
106  // be in non-decreasing order.
107  //
108  // return (last ==
109  // std::adjacent_find(first, last,
110  // std::greater<typename InputIterator::value_type >()));
111 
112  // A second one-linear attempt. This one checks for a **strictly
113  // increasing** (no duplicate entries) range. Also doesn't work
114  // with C-style arrays.
115  //
116  // return (last ==
117  // std::adjacent_find(first, last,
118  // std::not2(std::less<typename InputIterator::value_type>())));
119 }
static const unsigned int prev[3]
A lookup table for the decrement modulo 3 operation, for iterating through the three nodes per elemen...

◆ mkdir()

int libMesh::Utility::mkdir ( const char *  pathname)

Create a directory.

◆ pow()

template<int N, typename T >
T libMesh::Utility::pow ( const T &  x)

Definition at line 195 of file utility.h.

References libMesh::Utility::do_pow< N, T >::apply().

196 {
197  return do_pow<N,T>::apply(x);
198 }

◆ prepare_complex_data()

void libMesh::Utility::prepare_complex_data ( const std::vector< Complex > &  source,
std::vector< Real > &  real_part,
std::vector< Real > &  imag_part 
)

Prepare complex data for writing.

◆ print_timestamp()

void libMesh::Utility::print_timestamp ( std::ostream &  target = std::cout)

Definition at line 37 of file timestamp.h.

References get_timestamp().

38 {
39  target << get_timestamp() << std::endl;
40 }
std::string get_timestamp()

◆ string_to_enum()

template<typename T >
T libMesh::Utility::string_to_enum ( const std::string &  s)
Returns
the enumeration of type T which matches the string s.

◆ system_info()

std::string libMesh::Utility::system_info ( )
Returns
A string containing information about the system you are running on.