libMesh::Utility Namespace Reference

Classes

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

Functions

std::string get_timestamp ()
 
uint32_t hashword (const uint32_t *k, size_t length, 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)
 
uint16_t hashword (const uint16_t *k, size_t length)
 
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 >
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 binary_search() which returns true/false depending on whether the searched-for value is found. Utility::binary_find() uses a binary search on a sorted range to return an iterator to the searched-for element, or "last" if the element is not found.

Definition at line 130 of file utility.h.

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

131  {
132  ForwardIterator it = std::lower_bound(first, last, value);
133  return (it == last || value < *it) ? last : it;
134  }
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 140 of file utility.h.

141  {
142  ForwardIterator it = std::lower_bound(first, last, value, comp);
143  return (it == last || comp(value,*it)) ? last : it;
144  }
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 libMesh::LegacyXdrIO::read_mgf_soln(), and libMesh::LegacyXdrIO::write_mgf_soln().

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 }
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 224 of file utility.h.

References swap().

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

225  {
226  std::vector<T>().swap(vec);
227  }
unsigned int libMesh::Utility::factorial ( unsigned int  n)
inline

A simple implementation of the factorial.

Definition at line 204 of file utility.h.

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

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  }
uint32_t libMesh::Utility::hashword ( const uint32_t *  k,
size_t  length,
uint32_t  initval = 0 
)
inline

Definition at line 92 of file hashword.h.

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

93  {
94  uint32_t a,b,c;
95 
96  // Set up the internal state
97  a = b = c = 0xdeadbeef + ((static_cast<uint32_t>(length))<<2) + initval;
98 
99  //------------------------------------------------- handle most of the key
100  while (length > 3)
101  {
102  a += k[0];
103  b += k[1];
104  c += k[2];
105  mix(a,b,c);
106  length -= 3;
107  k += 3;
108  }
109 
110  //------------------------------------------- handle the last 3 uint32_t's
111  switch(length) // all the case statements fall through
112  {
113  case 3 : c+=k[2];
114  case 2 : b+=k[1];
115  case 1 : a+=k[0];
116  final(a,b,c);
117  default: // case 0: nothing left to add
118  break;
119  }
120 
121  //------------------------------------------------------ report the result
122  return c;
123  }
uint64_t libMesh::Utility::hashword ( const uint64_t *  k,
size_t  length 
)
inline

Definition at line 165 of file hashword.h.

166  {
167  // big prime number
168  const unsigned int bp = 65449;
169 
170  uint64_t c = 0;
171  unsigned int shift=0;
172  for (size_t i=0; i != length; ++i)
173  {
174  c += (k[i] << shift) % bp;
175  shift += 5;
176  }
177 
178  return c;
179  }
uint16_t libMesh::Utility::hashword ( const uint16_t *  k,
size_t  length 
)
inline

Definition at line 183 of file hashword.h.

184  {
185  // "big" prime number
186  const uint16_t bp = 257;
187 
188  uint16_t c = 0;
189  uint16_t shift=0;
190  for (size_t i=0; i != length; ++i)
191  {
192  c = static_cast<uint16_t>
193  (c + static_cast<uint16_t>(k[i] << shift) % bp);
194  shift = static_cast<uint16_t>(shift+3);
195  }
196 
197  return c;
198  }
uint32_t libMesh::Utility::hashword2 ( const uint32_t &  first,
const uint32_t &  second,
uint32_t  initval = 0 
)
inline

Definition at line 130 of file hashword.h.

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

131  {
132  uint32_t a,b,c;
133 
134  // Set up the internal state
135  a = b = c = 0xdeadbeef + 8 + initval;
136 
137  b+=second;
138  a+=first;
139  final(a,b,c);
140 
141  return c;
142  }
uint64_t libMesh::Utility::hashword2 ( const uint64_t  first,
const uint64_t  second 
)
inline

Definition at line 146 of file hashword.h.

147  {
148  // big prime number
149  const unsigned int bp = 65449;
150 
151  return (first%bp + (second<<5)%bp);
152  }
uint16_t libMesh::Utility::hashword2 ( const uint16_t  first,
const uint16_t  second 
)
inline

Definition at line 155 of file hashword.h.

156  {
157  // "big" prime number
158  const uint16_t bp = 257;
159 
160  return static_cast<uint16_t>(first%bp + (second<<3)%bp);
161  }
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 58 of file utility.h.

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

59  {
60  while (first != last)
61  {
62  *first = value++;
63  ++first;
64  }
65  }
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 75 of file utility.h.

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

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  }
template<typename T >
T libMesh::Utility::string_to_enum ( const std::string &  s)

Takes the string s and returns the matching enumeration of type T.

std::string libMesh::Utility::system_info ( )

The system_info function returns 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 }

Site Created By: libMesh Developers
Last modified: February 07 2014 16:57:34 UTC

Hosted By:
SourceForge.net Logo