Thrill  0.1
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 inline size_t longhash1(uint64_t key_)
76 {
77  key_ += ~(key_ << 32);
78  key_ ^= (key_ >> 22);
79  key_ += ~(key_ << 13);
80  key_ ^= (key_ >> 8);
81  key_ += (key_ << 3);
82  key_ ^= (key_ >> 15);
83  key_ += ~(key_ << 27);
84  key_ ^= (key_ >> 31);
85  return static_cast<size_t>(key_);
86 }
87 
88 template <class Type>
89 inline void swap_1D_arrays(Type* a, Type* b, size_t size)
90 {
91  for (size_t i = 0; i < size; ++i)
92  std::swap(a[i], b[i]);
93 }
94 
95 //! round n up to next larger multiple of 2^power. example: (48,4) = 64, (48,3) = 48.
96 template <typename Integral>
97 inline Integral round_up_to_power_of_two(Integral n, unsigned power)
98 {
99  auto pot = Integral(1) << power; // = 0..0 1 0^power
100  auto mask = pot - 1; // = 0..0 0 1^power
101 
102  if (n & mask) // n not divisible by pot
103  return (n & ~mask) + pot;
104  else
105  return n;
106 }
107 
108 template <class Container>
109 inline typename Container::value_type pop(Container& c)
110 {
111  const auto r = c.top();
112  c.pop();
113  return r;
114 }
115 
116 template <class Container>
117 inline typename Container::value_type pop_front(Container& c)
118 {
119  const auto r = c.front();
120  c.pop_front();
121  return r;
122 }
123 
124 template <class Container>
125 inline typename Container::value_type pop_back(Container& c)
126 {
127  const auto r = c.back();
128  c.pop_back();
129  return r;
130 }
131 
132 template <class Container>
133 inline typename Container::value_type pop_begin(Container& c)
134 {
135  const auto r = *c.begin();
136  c.erase(c.begin());
137  return r;
138 }
139 
140 } // namespace foxxll
141 
142 #endif // !FOXXLL_COMMON_UTILS_HEADER
143 
144 /**************************************************************************/
void swap_1D_arrays(Type *a, Type *b, size_t size)
Definition: utils.hpp:89
size_t longhash1(uint64_t key_)
Definition: utils.hpp:75
Type
VFS object type.
Definition: file_io.hpp:52
Container::value_type pop_begin(Container &c)
Definition: utils.hpp:133
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:125
void swap(CountingPtr< A, D > &a1, CountingPtr< A, D > &a2) noexcept
Container::value_type pop(Container &c)
Definition: utils.hpp:109
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
FOXXLL library namespace
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:117
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:97
int64_t atoi64(const char *s)
Definition: utils.hpp:43