STXXL  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
utils.h
Go to the documentation of this file.
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  * Copyright (C) 2013 Timo Bingmann <[email protected]>
10  *
11  * Distributed under the Boost Software License, Version 1.0.
12  * (See accompanying file LICENSE_1_0.txt or copy at
13  * http://www.boost.org/LICENSE_1_0.txt)
14  **************************************************************************/
15 
16 #ifndef STXXL_COMMON_UTILS_HEADER
17 #define STXXL_COMMON_UTILS_HEADER
18 
19 #include <vector>
20 #include <string>
21 #include <cmath>
22 #include <cstdlib>
23 #include <algorithm>
24 #include <sstream>
25 #include <limits>
26 
27 #include <stxxl/bits/config.h>
28 #include <stxxl/bits/namespace.h>
32 
33 
35 
36 ////////////////////////////////////////////////////////////////////////////
37 
38 #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 7)))
39 # define STXXL_ATTRIBUTE_UNUSED __attribute__ ((unused))
40 #else
41 # define STXXL_ATTRIBUTE_UNUSED
42 #endif
43 
44 ////////////////////////////////////////////////////////////////////////////
45 
46 #if defined(__GXX_EXPERIMENTAL_CXX0X__)
47 #define STXXL_STATIC_ASSERT(x) static_assert(x, #x)
48 #else
49 #define STXXL_STATIC_ASSERT(x) { typedef int static_assert_dummy_type[(x) ? 1 : -1] STXXL_ATTRIBUTE_UNUSED; }
50 #endif
51 
52 ////////////////////////////////////////////////////////////////////////////
53 
54 //! Split a string by given separator string. Returns a vector of strings with
55 //! at least min_fields and at most limit_fields
56 static inline std::vector<std::string>
57 split(const std::string& str, const std::string& sep,
58  unsigned int min_fields = 0,
59  unsigned int limit_fields = std::numeric_limits<unsigned int>::max())
60 {
61  std::vector<std::string> result;
62  if (str.empty()) {
63  result.resize(min_fields);
64  return result;
65  }
66 
67  std::string::size_type CurPos(0), LastPos(0);
68  while (1)
69  {
70  if (result.size() + 1 == limit_fields)
71  break;
72 
73  CurPos = str.find(sep, LastPos);
74  if (CurPos == std::string::npos)
75  break;
76 
77  result.push_back(
78  str.substr(LastPos,
79  std::string::size_type(CurPos - LastPos))
80  );
81 
82  LastPos = CurPos + sep.size();
83  }
84 
85  std::string sub = str.substr(LastPos);
86  result.push_back(sub);
87 
88  if (result.size() < min_fields)
89  result.resize(min_fields);
90 
91  return result;
92 }
93 
94 ////////////////////////////////////////////////////////////////////////////
95 
96 //! Format any ostream-able type into a string
97 template <typename Type>
98 std::string to_str(const Type& t)
99 {
100  std::ostringstream oss;
101  oss << t;
102  return oss.str();
103 }
104 
105 ////////////////////////////////////////////////////////////////////////////
106 
107 //! Parse a string like "343KB" or "44 GiB" into the corresponding size in
108 //! bytes. Returns the number of bytes and sets ok = true if the string could
109 //! be parsed correctly. If no units indicator is given, use def_unit in
110 //! k/m/g/t/p (powers of ten) or in K/M/G/T/P (power of two).
111 bool parse_SI_IEC_size(const std::string& str, uint64& size, char def_unit = 0);
112 
113 //! Format a byte size using SI (K, M, G, T) suffixes (powers of ten). Returns
114 //! "123 M" or similar.
115 std::string format_SI_size(uint64 number);
116 
117 //! Format a byte size using IEC (Ki, Mi, Gi, Ti) suffixes (powers of
118 //! two). Returns "123 Ki" or similar.
119 std::string format_IEC_size(uint64 number);
120 
121 ////////////////////////////////////////////////////////////////////////////
122 
123 inline stxxl::int64 atoi64(const char* s)
124 {
125 #if STXXL_MSVC
126  return _atoi64(s);
127 #else
128  return atoll(s);
129 #endif
130 }
131 
132 ////////////////////////////////////////////////////////////////////////////
133 
134 inline stxxl::uint64 atouint64(const char* s)
135 {
136 #if STXXL_MSVC
137  return _strtoui64(s, NULL, 10);
138 #else
139  return strtoull(s, NULL, 10);
140 #endif
141 }
142 
143 ////////////////////////////////////////////////////////////////////////////
144 
145 template <typename Tp>
146 inline const Tp&
147 STXXL_MIN(const Tp& a, const Tp& b)
148 {
149  return std::min<Tp>(a, b);
150 }
151 
152 template <typename Tp>
153 inline const Tp&
154 STXXL_MAX(const Tp& a, const Tp& b)
155 {
156  return std::max<Tp>(a, b);
157 }
158 
159 ////////////////////////////////////////////////////////////////////////////
160 
161 //! calculate the log2 floor of an integral type using math.h
162 template <typename Integral>
163 inline Integral log2_ceil(Integral i)
164 {
165  return Integral(ceil(log2(i)));
166 }
167 
168 //! calculate the log2 ceiling of an integral type using math.h
169 template <typename Integral>
170 inline Integral log2_floor(Integral i)
171 {
172  return Integral(log2(i));
173 }
174 
175 ////////////////////////////////////////////////////////////////////////////
176 
177 //! calculate the log2 floor of an integer type (by repeated bit shifts)
178 template <typename IntegerType>
179 unsigned int ilog2_floor(IntegerType i)
180 {
181  unsigned int p = 0;
182  while (i >= 256) i >>= 8, p += 8;
183  while (i >>= 1) ++p;
184  return p;
185 }
186 
187 //! calculate the log2 ceiling of an integer type (by repeated bit shifts)
188 template <typename IntegerType>
189 unsigned int ilog2_ceil(const IntegerType& i)
190 {
191  if (i <= 1) return 0;
192  return ilog2_floor(i - 1) + 1;
193 }
194 
195 ////////////////////////////////////////////////////////////////////////////
196 
197 template <typename Integral, typename Integral2>
198 inline
199 typename compat::remove_const<Integral>::type
200 div_ceil(Integral __n, Integral2 __d)
201 {
202 #if 0 // ambiguous overload for std::div(unsigned_anything, unsigned_anything)
203  typedef __typeof__ (std::div(__n, __d)) div_type;
204  div_type result = std::div(__n, __d);
205  return result.quot + (result.rem != 0);
206 #else
207  return __n / __d + ((__n % __d) != 0);
208 #endif
209 }
210 
211 ////////////////////////////////////////////////////////////////////////////
212 
213 #ifdef __GNUC__
214 #define HAVE_BUILTIN_EXPECT
215 #endif
216 
217 #ifdef HAVE_BUILTIN_EXPECT
218  #define LIKELY(c) __builtin_expect((c), 1)
219 #else
220  #define LIKELY(c) c
221 #endif
222 
223 #ifdef HAVE_BUILTIN_EXPECT
224  #define UNLIKELY(c) __builtin_expect((c), 0)
225 #else
226  #define UNLIKELY(c) c
227 #endif
228 
229 ////////////////////////////////////////////////////////////////////////////
230 
231 inline uint64 longhash1(uint64 key_)
232 {
233  key_ += ~(key_ << 32);
234  key_ ^= (key_ >> 22);
235  key_ += ~(key_ << 13);
236  key_ ^= (key_ >> 8);
237  key_ += (key_ << 3);
238  key_ ^= (key_ >> 15);
239  key_ += ~(key_ << 27);
240  key_ ^= (key_ >> 31);
241  return key_;
242 }
243 
244 ////////////////////////////////////////////////////////////////////////////
245 
246 template <class T>
247 inline void swap_1D_arrays(T* a, T* b, unsigned_type size)
248 {
249  for (unsigned_type i = 0; i < size; ++i)
250  std::swap(a[i], b[i]);
251 }
252 
253 ////////////////////////////////////////////////////////////////////////////
254 
255 //! round n up to next larger multiple of 2^power. example: (48,4) = 64, (48,3) = 48.
256 template <typename Integral>
257 inline Integral round_up_to_power_of_two(Integral n, unsigned_type power)
258 {
259  Integral pot = Integral(1) << power, // = 0..0 1 0^power
260  mask = pot - 1; // = 0..0 0 1^power
261  if (n & mask) // n not divisible by pot
262  return (n & ~mask) + pot;
263  else
264  return n;
265 }
266 
267 ////////////////////////////////////////////////////////////////////////////
268 
269 template <class Container>
270 inline typename Container::value_type pop(Container& c)
271 {
272  typename Container::value_type r = c.top();
273  c.pop();
274  return r;
275 }
276 
277 template <class Container>
278 inline typename Container::value_type pop_front(Container& c)
279 {
280  typename Container::value_type r = c.front();
281  c.pop_front();
282  return r;
283 }
284 
285 template <class Container>
286 inline typename Container::value_type pop_back(Container& c)
287 {
288  typename Container::value_type r = c.back();
289  c.pop_back();
290  return r;
291 }
292 
293 template <class Container>
294 inline typename Container::value_type pop_begin(Container& c)
295 {
296  typename Container::value_type r = *c.begin();
297  c.erase(c.begin());
298  return r;
299 }
300 
301 ////////////////////////////////////////////////////////////////////////////
302 
304 
305 #endif // !STXXL_COMMON_UTILS_HEADER
306 // vim: et:ts=4:sw=4
const Tp & STXXL_MIN(const Tp &a, const Tp &b)
Definition: utils.h:147
Integral log2_floor(Integral i)
calculate the log2 ceiling of an integral type using math.h
Definition: utils.h:170
long long int int64
Definition: types.h:40
void swap_1D_arrays(T *a, T *b, unsigned_type size)
Definition: utils.h:247
unsigned long long int uint64
Definition: types.h:41
std::string format_SI_size(uint64 number)
Format a byte size using SI (K, M, G, T) suffixes (powers of ten). Returns &quot;123 M&quot; or similar...
Definition: utils.cpp:94
Container::value_type pop_begin(Container &c)
Definition: utils.h:294
uint64 longhash1(uint64 key_)
Definition: utils.h:231
stxxl::uint64 atouint64(const char *s)
Definition: utils.h:134
stxxl::int64 atoi64(const char *s)
Definition: utils.h:123
Integral log2_ceil(Integral i)
calculate the log2 floor of an integral type using math.h
Definition: utils.h:163
const Tp & STXXL_MAX(const Tp &a, const Tp &b)
Definition: utils.h:154
Container::value_type pop_back(Container &c)
Definition: utils.h:286
Container::value_type pop(Container &c)
Definition: utils.h:270
static std::vector< std::string > split(const std::string &str, const std::string &sep, unsigned int min_fields=0, unsigned int limit_fields=std::numeric_limits< unsigned int >::max())
Split a string by given separator string. Returns a vector of strings with at least min_fields and at...
Definition: utils.h:57
std::string to_str(const Type &t)
Format any ostream-able type into a string.
Definition: utils.h:98
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
static uint_pair max()
return an uint_pair instance containing the largest value possible
Definition: uint_types.h:244
bool parse_SI_IEC_size(const std::string &str, uint64 &size, char def_unit=0)
Parse a string like &quot;343KB&quot; or &quot;44 GiB&quot; into the corresponding size in bytes. Returns the number of b...
Definition: utils.cpp:24
unsigned int ilog2_floor(IntegerType i)
calculate the log2 floor of an integer type (by repeated bit shifts)
Definition: utils.h:179
std::string format_IEC_size(uint64 number)
Format a byte size using IEC (Ki, Mi, Gi, Ti) suffixes (powers of two). Returns &quot;123 Ki&quot; or similar...
Definition: utils.cpp:111
Container::value_type pop_front(Container &c)
Definition: utils.h:278
choose_int_types< my_pointer_size >::unsigned_type unsigned_type
Definition: types.h:67
unsigned int ilog2_ceil(const IntegerType &i)
calculate the log2 ceiling of an integer type (by repeated bit shifts)
Definition: utils.h:189
compat::remove_const< Integral >::type div_ceil(Integral __n, Integral2 __d)
Definition: utils.h:200
Integral round_up_to_power_of_two(Integral n, unsigned_type power)
round n up to next larger multiple of 2^power. example: (48,4) = 64, (48,3) = 48. ...
Definition: utils.h:257
#define STXXL_END_NAMESPACE
Definition: namespace.h:17