Stxxl  1.4.0
include/stxxl/bits/common/utils.h
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  include/stxxl/bits/common/utils.h
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2002-2006 Roman Dementiev <dementiev@mpi-sb.mpg.de>
00007  *  Copyright (C) 2007-2009 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
00008  *  Copyright (C) 2008 Johannes Singler <singler@ira.uka.de>
00009  *
00010  *  Distributed under the Boost Software License, Version 1.0.
00011  *  (See accompanying file LICENSE_1_0.txt or copy at
00012  *  http://www.boost.org/LICENSE_1_0.txt)
00013  **************************************************************************/
00014 
00015 #ifndef STXXL_UTILS_HEADER
00016 #define STXXL_UTILS_HEADER
00017 
00018 #include <vector>
00019 #include <string>
00020 #include <cmath>
00021 #include <cstdlib>
00022 
00023 #ifdef STXXL_BOOST_CONFIG
00024  #include <boost/config.hpp>
00025 #endif
00026 
00027 #include <stxxl/bits/namespace.h>
00028 #include <stxxl/bits/common/types.h>
00029 #include <stxxl/bits/compat/type_traits.h>
00030 #include <stxxl/bits/msvc_compatibility.h>
00031 
00032 
00033 __STXXL_BEGIN_NAMESPACE
00034 
00035 ////////////////////////////////////////////////////////////////////////////
00036 
00037 #if defined(__GXX_EXPERIMENTAL_CXX0X__)
00038 #define STXXL_STATIC_ASSERT(x) static_assert(x, #x)
00039 #else
00040 #define STXXL_STATIC_ASSERT(x) { typedef int static_assert_dummy_type[(x) ? 1 : -1]; }
00041 #endif
00042 
00043 ////////////////////////////////////////////////////////////////////////////
00044 
00045 inline std::vector<std::string>
00046 split(const std::string & str, const std::string & sep)
00047 {
00048     std::vector<std::string> result;
00049     if (str.empty())
00050         return result;
00051 
00052     std::string::size_type CurPos(0), LastPos(0);
00053     while (1)
00054     {
00055         CurPos = str.find(sep, LastPos);
00056         if (CurPos == std::string::npos)
00057             break;
00058 
00059         std::string sub =
00060             str.substr(LastPos,
00061                        std::string::size_type(CurPos -
00062                                               LastPos));
00063         if (sub.size())
00064             result.push_back(sub);
00065 
00066         LastPos = CurPos + sep.size();
00067     }
00068 
00069     std::string sub = str.substr(LastPos);
00070     if (sub.size())
00071         result.push_back(sub);
00072 
00073     return result;
00074 }
00075 
00076 ////////////////////////////////////////////////////////////////////////////
00077 
00078 inline stxxl::int64 atoint64(const char * s)
00079 {
00080 #ifdef BOOST_MSVC
00081     return _atoi64(s);
00082 #else
00083     return atoll(s);
00084 #endif
00085 }
00086 
00087 ////////////////////////////////////////////////////////////////////////////
00088 
00089 template <typename Tp>
00090 inline const Tp &
00091 STXXL_MIN(const Tp & a, const Tp & b)
00092 {
00093     return std::min<Tp>(a, b);
00094 }
00095 
00096 template <typename Tp>
00097 inline const Tp &
00098 STXXL_MAX(const Tp & a, const Tp & b)
00099 {
00100     return std::max<Tp>(a, b);
00101 }
00102 
00103 ////////////////////////////////////////////////////////////////////////////
00104 
00105 template <typename Integral>
00106 inline Integral log2_ceil(Integral i)
00107 {
00108     return Integral(ceil(log2(i)));
00109 }
00110 
00111 template <typename Integral>
00112 inline Integral log2_floor(Integral i)
00113 {
00114     return Integral(log2(i));
00115 }
00116 
00117 ////////////////////////////////////////////////////////////////////////////
00118 
00119 template <typename Integral, typename Integral2>
00120 inline
00121 typename compat::remove_const<Integral>::type
00122 div_ceil(Integral __n, Integral2 __d)
00123 {
00124 #if 0  // ambiguous overload for std::div(unsigned_anything, unsigned_anything)
00125     typedef __typeof__(std::div(__n, __d)) div_type;
00126     div_type result = std::div(__n, __d);
00127     return result.quot + (result.rem != 0);
00128 #else
00129     return __n / __d + ((__n % __d) != 0);
00130 #endif
00131 }
00132 
00133 ////////////////////////////////////////////////////////////////////////////
00134 
00135 #ifdef __GNUC__
00136 #define HAVE_BUILTIN_EXPECT
00137 #endif
00138 
00139 #ifdef HAVE_BUILTIN_EXPECT
00140  #define LIKELY(c)   __builtin_expect((c), 1)
00141 #else
00142  #define LIKELY(c)   c
00143 #endif
00144 
00145 #ifdef HAVE_BUILTIN_EXPECT
00146  #define UNLIKELY(c)   __builtin_expect((c), 0)
00147 #else
00148  #define UNLIKELY(c)   c
00149 #endif
00150 
00151 ////////////////////////////////////////////////////////////////////////////
00152 
00153 inline uint64 longhash1(uint64 key_)
00154 {
00155     key_ += ~(key_ << 32);
00156     key_ ^= (key_ >> 22);
00157     key_ += ~(key_ << 13);
00158     key_ ^= (key_ >> 8);
00159     key_ += (key_ << 3);
00160     key_ ^= (key_ >> 15);
00161     key_ += ~(key_ << 27);
00162     key_ ^= (key_ >> 31);
00163     return key_;
00164 }
00165 
00166 ////////////////////////////////////////////////////////////////////////////
00167 
00168 template <class T>
00169 inline void swap_1D_arrays(T * a, T * b, unsigned_type size)
00170 {
00171     for (unsigned_type i = 0; i < size; ++i)
00172         std::swap(a[i], b[i]);
00173 }
00174 
00175 ////////////////////////////////////////////////////////////////////////////
00176 
00177 template <typename Integral>
00178 inline Integral round_up_to_power_of_two(Integral n, unsigned_type power)
00179 {
00180     Integral pot = 1 << power, // = 0..0 1 0^power
00181              mask = pot - 1;   // = 0..0 0 1^power
00182     if (n & mask)  // n not divisible by pot
00183         return (n & ~mask) + pot;
00184     else
00185         return n;
00186 }
00187 
00188 ////////////////////////////////////////////////////////////////////////////
00189 
00190 template <class Container>
00191 inline typename Container::value_type pop(Container & c)
00192 {
00193     typename Container::value_type r = c.top();
00194     c.pop();
00195     return r;
00196 }
00197 
00198 template <class Container>
00199 inline typename Container::value_type pop_front(Container & c)
00200 {
00201     typename Container::value_type r = c.front();
00202     c.pop_front();
00203     return r;
00204 }
00205 
00206 template <class Container>
00207 inline typename Container::value_type pop_back(Container & c)
00208 {
00209     typename Container::value_type r = c.back();
00210     c.pop_back();
00211     return r;
00212 }
00213 
00214 template <class Container>
00215 inline typename Container::value_type pop_begin(Container & c)
00216 {
00217     typename Container::value_type r = *c.begin();
00218     c.erase(c.begin());
00219     return r;
00220 }
00221 
00222 ////////////////////////////////////////////////////////////////////////////
00223 
00224 __STXXL_END_NAMESPACE
00225 
00226 #endif // !STXXL_UTILS_HEADER
00227 // vim: et:ts=4:sw=4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines