00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
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
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
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
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
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
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
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
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
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
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
00176
00177 __STXXL_END_NAMESPACE
00178
00179 #endif // !STXXL_UTILS_HEADER
00180