/usr/include/libmesh/utility.h is in libmesh-dev 0.7.1-2ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | // $Id: utility.h 3937 2010-08-24 23:43:08Z roystgnr $
// The libMesh Finite Element Library.
// Copyright (C) 2002-2008 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#ifndef __utility_h__
#define __utility_h__
// System includes
#include <string>
#include <vector>
#include <algorithm> // for std::lower_bound
// Local includes
#include "libmesh_common.h" // for Real
namespace libMesh
{
// ------------------------------------------------------------
// The Utility namespace is for functions
// which are useful but don't necessarily belong anywhere else.
namespace Utility
{
//-------------------------------------------------------------------
/**
* The \p system_info function returns information about the system
* you are running on.
*/
std::string system_info();
//-------------------------------------------------------------------
/**
* \p Utility::iota is a duplication of the SGI STL extension
* \p std::iota. It simply assigns sequentially increasing values
* to a range. That is, it assigns \p value to \p *first, \p value + 1
* to \p *(first + 1) and so on. In general, each iterator \p i in the
* range [first, last) is assigned \p value + (i - \p first).
*/
template <typename ForwardIter, typename T>
void iota (ForwardIter first, ForwardIter last, T value)
{
while (first != last)
{
*first = value++;
++first;
}
}
/**
* 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).
*/
template< class InputIterator >
bool is_sorted(InputIterator first, InputIterator last)
{
if ( first == last )
return true;
// "prev" always points to the entry just to the left of "first"
// [- - - - - -]
// ^ ^
// prev first
//
// [- - - - - -]
// ^ ^
// prev first
//
// [- - - - - -]
// ^ ^
// prev first
InputIterator prev( first );
for ( ++first; first != last; ++prev, ++first )
if ( *first < *prev ) // Note: this is the same as *prev > *first,
return false; // but we only require op< to be defined.
// If we haven't returned yet, it's sorted!
return true;
// A one-liner version using adjacent_find. This doesn't work for
// C-style arrays, since their pointers do not have a value_type.
//
// Works by checking to see if adjacent entries satisfy *i >
// *(i+1) and returns the first one which does. If "last" is
// returned, no such pair was found, and therefore the range must
// be in non-decreasing order.
//
// return (last ==
// std::adjacent_find(first, last,
// std::greater< typename InputIterator::value_type >()));
// A second one-linear attempt. This one checks for a **strictly
// increasing** (no duplicate entries) range. Also doesn't work
// with C-style arrays.
//
// return (last ==
// std::adjacent_find(first, last,
// std::not2(std::less<typename InputIterator::value_type>())));
}
/**
* 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.
*/
template<class ForwardIterator, class T>
ForwardIterator binary_find(ForwardIterator first, ForwardIterator last, const T& value)
{
ForwardIterator it = std::lower_bound(first, last, value);
return (it == last || value < *it) ? last : it;
}
//-------------------------------------------------------------------
/**
* An efficient template instantiation for raising
* to an arbitrary integer power.
*/
template <int N>
inline
Real pow(const Real x)
{
libmesh_assert(N>1);
if (N%2) // odd exponent
return x * pow<N-1>(x);
const Real xNover2 = pow<N/2>(x);
return xNover2*xNover2;
}
template <>
inline
Real pow<8>(const Real x)
{
const Real
x2 = x*x,
x4 = x2*x2,
x8 = x4*x4;
return x8;
}
template <>
inline
Real pow<6>(const Real x)
{
const Real
x2 = x*x,
x4 = x2*x2,
x6 = x4*x2;
return x6;
}
template <>
inline
Real pow<4>(const Real x)
{
const Real
x2 = x*x,
x4 = x2*x2;
return x4;
}
template <>
inline
Real pow<3>(const Real x) { return x*x*x; }
template <>
inline
Real pow<2>(const Real x) { return x*x; }
template <>
inline
Real pow<1>(const Real x) { return x; }
template <>
inline
Real pow<0>(const Real) { return 1.; }
//-------------------------------------------------------------------
/**
* A simple implementation of the factorial.
*/
inline
unsigned int factorial(unsigned int n)
{
unsigned int factorial_n = 1;
if (n==0)
return factorial_n;
for (unsigned int i=1; i<n; i++)
factorial_n *= i+1;
return factorial_n;
}
//-------------------------------------------------------------------
/**
* A convenient method to truly empty a vector using the "swap trick"
*/
template <typename T>
void deallocate (std::vector<T> &vec)
{
std::vector<T>().swap(vec);
}
//-------------------------------------------------------------------
// Utility functions useful when dealing with complex numbers.
#ifdef LIBMESH_USE_COMPLEX_NUMBERS
/**
* @returns for \p r_o_c = 0 the filename for output of the real part
* of complex data, and for \p r_o_c = 1 the filename for the imaginary
* part.
*/
std::string complex_filename (const std::string& basename,
unsigned int r_o_c=0);
/**
* Prepare complex data for writing.
*/
void prepare_complex_data (const std::vector<Complex>& source,
std::vector<Real>& real_part,
std::vector<Real>& imag_part);
#endif // #ifdef LIBMESH_USE_COMPLEX_NUMBERS
//-------------------------------------------------------------------
/**
* This Functor simply takes an object and reverses its byte
* representation. This is useful for changing endian-ness
* for file IO. This class has been tested on x86 architectures
* with 4-byte words.
*
*
*/
class ReverseBytes
{
public:
/**
* Constructor. Takes a bool, determines if we will actually
* do byte reversing.
*/
ReverseBytes (const bool dr);
/**
* Functor. Takes the data to reverse and performs the
* byte-ordering reversal.
*/
template <typename T>
T operator () (T& data) const;
private:
/**
* Returns the value of the reverse flag.
*/
bool reverse () const { return _do_reverse; };
/**
* flag
*/
const bool _do_reverse;
};
//---------------------------------------------------------
// ReverseBytes inline members
inline
ReverseBytes::ReverseBytes (const bool rb) :
_do_reverse (rb)
{}
template <typename T>
inline
T ReverseBytes::operator() (T& data) const
{
// Possibly reverse the byte ordering
if (this->reverse())
{
unsigned char* b = (unsigned char*) &data;
register int i=0;
register int j=(sizeof(T) - 1);
while (i < j)
{
std::swap (b[i], b[j]);
i++; j--;
}
}
return data;
}
}
} // namespace libMesh
#endif // #define __utility_h__
|