Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
utils.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * foxxll/common/utils.hpp
3  *
4  * Part of FOXXLL. See http://foxxll.org
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 FOXXLL_COMMON_UTILS_HEADER
17 #define FOXXLL_COMMON_UTILS_HEADER
18 
19 #include <algorithm>
20 #include <cmath>
21 #include <cstdlib>
22 #include <limits>
23 #include <sstream>
24 #include <string>
25 #include <type_traits>
26 #include <utility>
27 #include <vector>
28 
29 #include <foxxll/common/types.hpp>
30 #include <foxxll/config.hpp>
31 
32 namespace foxxll {
33 
34 //! Format any ostream-able type into a string
35 template <typename Type>
37 {
38  std::ostringstream oss;
39  oss << t;
40  return oss.str();
41 }
42 
43 inline int64_t atoi64(const char* s)
44 {
45 #if FOXXLL_MSVC
46  return _atoi64(s);
47 #else
48  return atoll(s);
49 #endif
50 }
51 
52 inline uint64_t atouint64(const char* s)
53 {
54 #if FOXXLL_MSVC
55  return _strtoui64(s, nullptr, 10);
56 #else
57  return strtoull(s, nullptr, 10);
58 #endif
59 }
60 
61 template <typename Integral, typename Integral2>
62 inline
63 typename std::remove_const<Integral>::type
64 div_ceil(Integral n, Integral2 d)
65 {
66 #if 0 // ambiguous overload for std::div(unsigned_anything, unsigned_anything)
67  using div_type = __typeof__(std::div(n, d));
68  div_type result = std::div(n, d);
69  return result.quot + (result.rem != 0);
70 #else
71  return n / d + ((n % d) != 0);
72 #endif
73 }
74 
75 #ifdef __GNUC__
76 #define HAVE_BUILTIN_EXPECT
77 #endif
78 
79 #ifdef HAVE_BUILTIN_EXPECT
80  #define LIKELY(c) __builtin_expect((c), 1)
81 #else
82  #define LIKELY(c) c
83 #endif
84 
85 #ifdef HAVE_BUILTIN_EXPECT
86  #define UNLIKELY(c) __builtin_expect((c), 0)
87 #else
88  #define UNLIKELY(c) c
89 #endif
90 
91 inline size_t longhash1(uint64_t key_)
92 {
93  key_ += ~(key_ << 32);
94  key_ ^= (key_ >> 22);
95  key_ += ~(key_ << 13);
96  key_ ^= (key_ >> 8);
97  key_ += (key_ << 3);
98  key_ ^= (key_ >> 15);
99  key_ += ~(key_ << 27);
100  key_ ^= (key_ >> 31);
101  return static_cast<size_t>(key_);
102 }
103 
104 template <class Type>
105 inline void swap_1D_arrays(Type* a, Type* b, size_t size)
106 {
107  for (size_t i = 0; i < size; ++i)
108  std::swap(a[i], b[i]);
109 }
110 
111 //! round n up to next larger multiple of 2^power. example: (48,4) = 64, (48,3) = 48.
112 template <typename Integral>
113 inline Integral round_up_to_power_of_two(Integral n, unsigned power)
114 {
115  auto pot = Integral(1) << power; // = 0..0 1 0^power
116  auto mask = pot - 1; // = 0..0 0 1^power
117 
118  if (n & mask) // n not divisible by pot
119  return (n & ~mask) + pot;
120  else
121  return n;
122 }
123 
124 template <class Container>
125 inline typename Container::value_type pop(Container& c)
126 {
127  const auto r = c.top();
128  c.pop();
129  return r;
130 }
131 
132 template <class Container>
133 inline typename Container::value_type pop_front(Container& c)
134 {
135  const auto r = c.front();
136  c.pop_front();
137  return r;
138 }
139 
140 template <class Container>
141 inline typename Container::value_type pop_back(Container& c)
142 {
143  const auto r = c.back();
144  c.pop_back();
145  return r;
146 }
147 
148 template <class Container>
149 inline typename Container::value_type pop_begin(Container& c)
150 {
151  const auto r = *c.begin();
152  c.erase(c.begin());
153  return r;
154 }
155 
156 } // namespace foxxll
157 
158 #endif // !FOXXLL_COMMON_UTILS_HEADER
159 
160 /**************************************************************************/
void swap_1D_arrays(Type *a, Type *b, size_t size)
Definition: utils.hpp:105
size_t longhash1(uint64_t key_)
Definition: utils.hpp:91
Type
VFS object type.
Definition: file_io.hpp:52
Container::value_type pop_begin(Container &c)
Definition: utils.hpp:149
std::string to_str(const Type &t)
Format any ostream-able type into a string.
Definition: utils.hpp:36
Container::value_type pop_back(Container &c)
Definition: utils.hpp:141
void swap(CountingPtr< A, D > &a1, CountingPtr< A, D > &a2) noexcept
Container::value_type pop(Container &c)
Definition: utils.hpp:125
std::remove_const< Integral >::type div_ceil(Integral n, Integral2 d)
Definition: utils.hpp:64
uint64_t atouint64(const char *s)
Definition: utils.hpp:52
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking
Definition: allocator.hpp:220
Container::value_type pop_front(Container &c)
Definition: utils.hpp:133
Integral round_up_to_power_of_two(Integral n, unsigned power)
round n up to next larger multiple of 2^power. example: (48,4) = 64, (48,3) = 48. ...
Definition: utils.hpp:113
int64_t atoi64(const char *s)
Definition: utils.hpp:43