Stxxl  1.3.2
utils.h
1 /***************************************************************************
2  * include/stxxl/bits/common/utils.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2002-2006 Roman Dementiev <[email protected]>
7  * Copyright (C) 2007-2009 Andreas Beckmann <[email protected]>
8  * Copyright (C) 2008 Johannes Singler <[email protected]>
9  *
10  * Distributed under the Boost Software License, Version 1.0.
11  * (See accompanying file LICENSE_1_0.txt or copy at
12  * http://www.boost.org/LICENSE_1_0.txt)
13  **************************************************************************/
14 
15 #ifndef STXXL_UTILS_HEADER
16 #define STXXL_UTILS_HEADER
17 
18 #include <vector>
19 #include <string>
20 #include <cmath>
21 #include <cstdlib>
22 
23 #ifdef STXXL_BOOST_CONFIG
24  #include <boost/config.hpp>
25 #endif
26 
27 #include <stxxl/bits/namespace.h>
28 #include <stxxl/bits/common/types.h>
29 #include <stxxl/bits/compat/type_traits.h>
30 #include <stxxl/bits/msvc_compatibility.h>
31 
32 
33 __STXXL_BEGIN_NAMESPACE
34 
36 
37 #if defined(__GXX_EXPERIMENTAL_CXX0X__)
38 #define STXXL_STATIC_ASSERT(x) static_assert(x, #x)
39 #else
40 #define STXXL_STATIC_ASSERT(x) { typedef int static_assert_dummy_type[(x) ? 1 : -1]; }
41 #endif
42 
44 
45 inline std::vector<std::string>
46 split(const std::string & str, const std::string & sep)
47 {
48  std::vector<std::string> result;
49  if (str.empty())
50  return result;
51 
52  std::string::size_type CurPos(0), LastPos(0);
53  while (1)
54  {
55  CurPos = str.find(sep, LastPos);
56  if (CurPos == std::string::npos)
57  break;
58 
59  std::string sub =
60  str.substr(LastPos,
61  std::string::size_type(CurPos -
62  LastPos));
63  if (sub.size())
64  result.push_back(sub);
65 
66  LastPos = CurPos + sep.size();
67  }
68 
69  std::string sub = str.substr(LastPos);
70  if (sub.size())
71  result.push_back(sub);
72 
73  return result;
74 }
75 
77 
78 inline stxxl::int64 atoint64(const char * s)
79 {
80 #ifdef BOOST_MSVC
81  return _atoi64(s);
82 #else
83  return atoll(s);
84 #endif
85 }
86 
88 
89 template <typename Tp>
90 inline const Tp &
91 STXXL_MIN(const Tp & a, const Tp & b)
92 {
93  return std::min<Tp>(a, b);
94 }
95 
96 template <typename Tp>
97 inline const Tp &
98 STXXL_MAX(const Tp & a, const Tp & b)
99 {
100  return std::max<Tp>(a, b);
101 }
102 
104 
105 template <typename Integral>
106 inline Integral log2_ceil(Integral i)
107 {
108  return Integral(ceil(log2(i)));
109 }
110 
111 template <typename Integral>
112 inline Integral log2_floor(Integral i)
113 {
114  return Integral(log2(i));
115 }
116 
118 
119 template <typename Integral, typename Integral2>
120 inline
121 typename compat::remove_const<Integral>::type
122 div_ceil(Integral __n, Integral2 __d)
123 {
124 #if 0 // ambiguous overload for std::div(unsigned_anything, unsigned_anything)
125  typedef __typeof__(std::div(__n, __d)) div_type;
126  div_type result = std::div(__n, __d);
127  return result.quot + (result.rem != 0);
128 #else
129  return __n / __d + ((__n % __d) != 0);
130 #endif
131 }
132 
134 
135 #ifdef __GNUC__
136 #define HAVE_BUILTIN_EXPECT
137 #endif
138 
139 #ifdef HAVE_BUILTIN_EXPECT
140  #define LIKELY(c) __builtin_expect((c), 1)
141 #else
142  #define LIKELY(c) c
143 #endif
144 
145 #ifdef HAVE_BUILTIN_EXPECT
146  #define UNLIKELY(c) __builtin_expect((c), 0)
147 #else
148  #define UNLIKELY(c) c
149 #endif
150 
152 
153 inline uint64 longhash1(uint64 key_)
154 {
155  key_ += ~(key_ << 32);
156  key_ ^= (key_ >> 22);
157  key_ += ~(key_ << 13);
158  key_ ^= (key_ >> 8);
159  key_ += (key_ << 3);
160  key_ ^= (key_ >> 15);
161  key_ += ~(key_ << 27);
162  key_ ^= (key_ >> 31);
163  return key_;
164 }
165 
167 
168 template <class T>
169 inline void swap_1D_arrays(T * a, T * b, unsigned_type size)
170 {
171  for (unsigned_type i = 0; i < size; ++i)
172  std::swap(a[i], b[i]);
173 }
174 
176 
177 __STXXL_END_NAMESPACE
178 
179 #endif // !STXXL_UTILS_HEADER
180 // vim: et:ts=4:sw=4