libMesh::Utility Namespace Reference

Classes

struct  do_pow
 
struct  do_pow< 0, T >
 
struct  do_pow< 1, T >
 
struct  do_pow< 6, T >
 
class  ReverseBytes
 

Functions

std::string get_timestamp ()
 
uint32_t hashword (const uint32_t *k, size_t length, uint32_t initval=0)
 
uint32_t hashword (const std::vector< uint32_t > &keys, uint32_t initval=0)
 
uint32_t hashword2 (const uint32_t &first, const uint32_t &second, uint32_t initval=0)
 
uint64_t hashword2 (const uint64_t first, const uint64_t second)
 
uint16_t hashword2 (const uint16_t first, const uint16_t second)
 
uint64_t hashword (const uint64_t *k, size_t length)
 
uint64_t hashword (const std::vector< uint64_t > &keys)
 
uint16_t hashword (const uint16_t *k, size_t length)
 
uint16_t hashword (const std::vector< uint16_t > &keys)
 
template<typename T >
string_to_enum (const std::string &s)
 
template<typename T >
std::string enum_to_string (const T e)
 
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)
 
template<class InputIterator >
bool is_sorted (InputIterator first, InputIterator last)
 
template<class ForwardIterator , class T >
ForwardIterator binary_find (ForwardIterator first, ForwardIterator last, const T &value)
 
template<class ForwardIterator , class T , class Compare >
ForwardIterator binary_find (ForwardIterator first, ForwardIterator last, const T &value, Compare comp)
 
template<int N, typename T >
pow (const T &x)
 
unsigned int factorial (unsigned int n)
 
template<typename T >
binomial (T n, T k)
 
template<typename T >
void deallocate (std::vector< T > &vec)
 
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)
 

Function Documentation

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.

Referenced by libMesh::ReplicatedMesh::stitching_helper(), and libMesh::TetGenMeshInterface::triangulate_conformingDelaunayMesh_carvehole().

133 {
134  ForwardIterator it = std::lower_bound(first, last, value);
135  return (it == last || value < *it) ? last : it;
136 }
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 }
template<typename T >
T libMesh::Utility::binomial ( n,
k 
)

Definition at line 221 of file utility.h.

Referenced by libMesh::FE< Dim, T >::shape(), and libMesh::FE< Dim, T >::shape_deriv().

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 }
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.

Definition at line 83 of file utility.C.

References libMesh::Quality::name().

Referenced by deallocate().

85 {
86  std::string name(basename);
87 
88  if (r_o_c == 0)
89  name.append(".real");
90 
91  else
92  name.append(".imag");
93 
94  return name;
95 }
std::string name(const ElemQuality q)
Definition: elem_quality.C:39
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 complex_filename(), prepare_complex_data(), and libMesh::swap().

Referenced by libMesh::Nemesis_IO::read().

245 {
246  std::vector<T>().swap(vec);
247 }
void swap(Iterator &lhs, Iterator &rhs)
unsigned int libMesh::Utility::factorial ( unsigned int  n)
inline

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 }
std::string libMesh::Utility::get_timestamp ( )

Definition at line 37 of file timestamp.C.

References libMesh::out.

Referenced by libMesh::PerfLog::get_info_header(), print_timestamp(), and system_info().

38 {
39 #ifdef LIBMESH_HAVE_LOCALE
40  // Create time_put "facet"
41  std::locale loc;
42  const std::time_put<char> & tp = std::use_facet <std::time_put<char> > (loc);
43 
44  // Call C-style time getting functions
45  time_t now = time(NULL);
46  tm * tm_struct = localtime(&now);
47 
48  // Date will eventually be stored in this ostringstream's string
49  std::ostringstream date_stream;
50 
51  // See below for documentation on the use of the
52  // std::time_put::put() function
53  tp.put(date_stream, /*s*/
54  date_stream, /*str*/
55  date_stream.fill(), /*fill*/
56  tm_struct, /*tm*/
57  'c'); /*format*/
58 
59  // Another way to use it is to totally customize the format...
60  // char pattern[]="%d %B %Y %I:%M:%S %p";
61  // tp.put(date_stream, /*s*/
62  // date_stream, /*str*/
63  // date_stream.fill(), /*fill*/
64  // tm_struct, /*tm*/
65  // pattern, /*format begin*/
66  // pattern+sizeof(pattern)-1); /*format end */
67 
68  return date_stream.str();
69 #else
70  // C-stye code originally found here:
71  // http://people.sc.fsu.edu/~burkardt/cpp_src/timestamp/timestamp.C
72  // Author: John Burkardt, 24 September 2003
73  const unsigned int time_size = 40;
74  char time_buffer[time_size];
75 
76  time_t now = time (NULL);
77  tm * tm_struct = localtime (&now);
78 
79  // No more than time_size characters will be placed into the array. If the
80  // total number of resulting characters, including the terminating
81  // NUL character, is not more than time_size, strftime() returns the
82  // number of characters in the array, not counting the terminating
83  // NUL. Otherwise, zero is returned and the buffer contents are
84  // indeterminate.
85  size_t len = strftime ( time_buffer, time_size, "%c", tm_struct );
86 
87  if (len != 0)
88  return std::string(time_buffer);
89  else
90  {
91  libMesh::out << "Error formatting time buffer, returning empty string!" << std::endl;
92  return std::string("");
93  }
94 
95 #endif // LIBMESH_HAVE_LOCALE
96 }
OStreamProxy out(std::cout)
uint32_t libMesh::Utility::hashword ( const uint32_t *  k,
size_t  length,
uint32_t  initval = 0 
)
inline

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(), hashword(), and libMesh::Elem::key().

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  case 2 : b+=k[1];
176  case 1 : a+=k[0];
177  final(a,b,c);
178  default: // case 0: nothing left to add
179  break;
180  }
181 
182  //------------------------------------------------------ report the result
183  return c;
184 }
uint32_t libMesh::Utility::hashword ( const std::vector< uint32_t > &  keys,
uint32_t  initval = 0 
)
inline

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

Definition at line 192 of file hashword.h.

References hashword().

193 {
194  return hashword(&keys[0], keys.size(), initval);
195 }
uint16_t hashword(const std::vector< uint16_t > &keys)
Definition: hashword.h:308
uint64_t libMesh::Utility::hashword ( const uint64_t *  k,
size_t  length 
)
inline

Call the 64-bit FNV hash function.

Definition at line 246 of file hashword.h.

247 {
248  return fnv_64_buf(k, 8*length);
249 }
uint64_t libMesh::Utility::hashword ( const std::vector< uint64_t > &  keys)
inline

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

Definition at line 257 of file hashword.h.

References hashword().

258 {
259  return hashword(&keys[0], keys.size());
260 }
uint16_t hashword(const std::vector< uint16_t > &keys)
Definition: hashword.h:308
uint16_t libMesh::Utility::hashword ( const uint16_t *  k,
size_t  length 
)
inline

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 269 of file hashword.h.

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

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

Definition at line 308 of file hashword.h.

References hashword().

309 {
310  return hashword(&keys[0], keys.size());
311 }
uint16_t hashword(const std::vector< uint16_t > &keys)
Definition: hashword.h:308
uint32_t libMesh::Utility::hashword2 ( const uint32_t &  first,
const uint32_t &  second,
uint32_t  initval = 0 
)
inline

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

Author
Bob Jenkins
Date
May 2006

Definition at line 207 of file hashword.h.

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

208 {
209  uint32_t a,b,c;
210 
211  // Set up the internal state
212  a = b = c = 0xdeadbeef + 8 + initval;
213 
214  b+=second;
215  a+=first;
216  final(a,b,c);
217 
218  return c;
219 }
uint64_t libMesh::Utility::hashword2 ( const uint64_t  first,
const uint64_t  second 
)
inline

Call the 64-bit FNV hash function.

Definition at line 225 of file hashword.h.

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

Definition at line 237 of file hashword.h.

238 {
239  return static_cast<uint16_t>(first%65449 + (second<<5)%65449);
240 }
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.

Referenced by libMesh::PetscVector< T >::create_subvector(), and libMesh::PetscVector< T >::localize().

58 {
59  while (first != last)
60  {
61  *first = value++;
62  ++first;
63  }
64 }
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]
template<int N, typename T >
T libMesh::Utility::pow ( const T &  x)
inline
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.

Definition at line 99 of file utility.C.

Referenced by deallocate().

102 {
103  const unsigned int len = source.size();
104 
105  real_part.resize(len);
106  imag_part.resize(len);
107 
108  for (unsigned int i=0; i<len; i++)
109  {
110  real_part[i] = source[i].real();
111  imag_part[i] = source[i].imag();
112  }
113 }
void libMesh::Utility::print_timestamp ( std::ostream &  target = std::cout)
inline

Definition at line 37 of file timestamp.h.

References get_timestamp().

38 {
39  target << get_timestamp() << std::endl;
40 }
std::string get_timestamp()
Definition: timestamp.C:37
template<typename T >
T libMesh::Utility::string_to_enum ( const std::string &  s)
Returns
the enumeration of type T which matches the string s.
std::string libMesh::Utility::system_info ( )
Returns
A string containing information about the system you are running on.

Definition at line 45 of file utility.C.

References get_timestamp().

46 {
47  std::ostringstream oss;
48 
49  std::string date = Utility::get_timestamp();
50 
51  // Get system information
52  struct utsname sysInfo;
53  uname(&sysInfo);
54 
55  // Get user information
56 #ifdef LIBMESH_HAVE_GETPWUID
57  struct passwd * p = getpwuid(getuid());
58 #endif
59 
60 
61  oss << '\n'
62  << " ---------------------------------------------------------------------\n"
63  << "| Time: " << date << '\n'
64  << "| OS: " << sysInfo.sysname << '\n'
65  << "| HostName: " << sysInfo.nodename << '\n'
66  << "| OS Release " << sysInfo.release << '\n'
67  << "| OS Version: " << sysInfo.version << '\n'
68  << "| Machine: " << sysInfo.machine << '\n'
69 #ifdef LIBMESH_HAVE_GETPWUID
70  << "| Username: " << p->pw_name << '\n'
71 #else
72  << "| Username: " << "Unknown" << '\n'
73 #endif
74  << " ---------------------------------------------------------------------\n";
75 
76  return oss.str();
77 }
std::string get_timestamp()
Definition: timestamp.C:37