00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef STXXL_UTILS_HEADER
00015 #define STXXL_UTILS_HEADER
00016
00017 #include <vector>
00018 #include <string>
00019 #include <cmath>
00020 #include <cstdlib>
00021
00022 #ifdef STXXL_BOOST_CONFIG
00023 #include <boost/config.hpp>
00024 #endif
00025
00026 #include <stxxl/bits/namespace.h>
00027 #include <stxxl/bits/common/types.h>
00028 #include <stxxl/bits/compat_type_traits.h>
00029 #include <stxxl/bits/msvc_compatibility.h>
00030
00031
00032 __STXXL_BEGIN_NAMESPACE
00033
00034 #ifdef BOOST_MSVC
00035 #define _STXXL_DEPRECATED(x) __declspec(deprecated) x
00036 #elif defined(__GNUG__) && ((__GNUC__ * 10000 + __GNUC_MINOR__ * 100) < 30400)
00037
00038 #define _STXXL_DEPRECATED(x) x
00039 #else
00040 #define _STXXL_DEPRECATED(x) x __attribute__ ((__deprecated__))
00041 #endif
00042
00044
00045 #if defined(__GXX_EXPERIMENTAL_CXX0X__)
00046 #define STXXL_STATIC_ASSERT(x) static_assert(x, #x)
00047 #else
00048 #define STXXL_STATIC_ASSERT(x) { typedef int static_assert_dummy_type[(x) ? 1 : -1]; }
00049 #endif
00050
00052
00053 inline std::vector<std::string>
00054 split(const std::string & str, const std::string & sep)
00055 {
00056 std::vector<std::string> result;
00057 if (str.empty())
00058 return result;
00059
00060 std::string::size_type CurPos(0), LastPos(0);
00061 while (1)
00062 {
00063 CurPos = str.find(sep, LastPos);
00064 if (CurPos == std::string::npos)
00065 break;
00066
00067 std::string sub =
00068 str.substr(LastPos,
00069 std::string::size_type(CurPos -
00070 LastPos));
00071 if (sub.size())
00072 result.push_back(sub);
00073
00074 LastPos = CurPos + sep.size();
00075 }
00076
00077 std::string sub = str.substr(LastPos);
00078 if (sub.size())
00079 result.push_back(sub);
00080
00081 return result;
00082 }
00083
00085
00086 inline stxxl::int64 atoint64(const char * s)
00087 {
00088 #ifdef BOOST_MSVC
00089 return _atoi64(s);
00090 #else
00091 return atoll(s);
00092 #endif
00093 }
00094
00096
00097 template <typename Tp>
00098 inline const Tp &
00099 STXXL_MIN(const Tp & a, const Tp & b)
00100 {
00101 return std::min<Tp>(a, b);
00102 }
00103
00104 template <typename Tp>
00105 inline const Tp &
00106 STXXL_MAX(const Tp & a, const Tp & b)
00107 {
00108 return std::max<Tp>(a, b);
00109 }
00110
00112
00113 template <typename Integral>
00114 inline Integral log2_ceil(Integral i)
00115 {
00116 return Integral(ceil(log2(i)));
00117 }
00118
00119 template <typename Integral>
00120 inline Integral log2_floor(Integral i)
00121 {
00122 return Integral(log2(i));
00123 }
00124
00126
00127 template <typename Integral, typename Integral2>
00128 inline
00129 typename remove_const<Integral>::type
00130 div_ceil(Integral __n, Integral2 __d)
00131 {
00132 #if 0 // ambiguous overload for std::div(unsigned_anything, unsigned_anything)
00133 typedef __typeof__(std::div(__n, __d)) div_type;
00134 div_type result = std::div(__n, __d);
00135 return result.quot + (result.rem != 0);
00136 #else
00137 return __n / __d + ((__n % __d) != 0);
00138 #endif
00139 }
00140
00142
00143 #ifdef __GNUC__
00144 #define HAVE_BUILTIN_EXPECT
00145 #endif
00146
00147 #ifdef HAVE_BUILTIN_EXPECT
00148 #define LIKELY(c) __builtin_expect((c), 1)
00149 #else
00150 #define LIKELY(c) c
00151 #endif
00152
00153 #ifdef HAVE_BUILTIN_EXPECT
00154 #define UNLIKELY(c) __builtin_expect((c), 0)
00155 #else
00156 #define UNLIKELY(c) c
00157 #endif
00158
00160
00161 inline uint64 longhash1(uint64 key_)
00162 {
00163 key_ += ~(key_ << 32);
00164 key_ ^= (key_ >> 22);
00165 key_ += ~(key_ << 13);
00166 key_ ^= (key_ >> 8);
00167 key_ += (key_ << 3);
00168 key_ ^= (key_ >> 15);
00169 key_ += ~(key_ << 27);
00170 key_ ^= (key_ >> 31);
00171 return key_;
00172 }
00173
00175
00176 template <class T>
00177 inline void swap_1D_arrays(T * a, T * b, unsigned_type size)
00178 {
00179 for (unsigned_type i = 0; i < size; ++i)
00180 std::swap(a[i], b[i]);
00181 }
00182
00184
00185 __STXXL_END_NAMESPACE
00186
00187 #endif // !STXXL_UTILS_HEADER
00188