vSMC  v3.0.0
Scalable Monte Carlo
basic.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/internal/basic.hpp
3 //----------------------------------------------------------------------------
4 // vSMC: Scalable Monte Carlo
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013-2016, Yan Zhou
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 //============================================================================
31 
32 #ifndef VSMC_INTERNAL_BASIC_HPP
33 #define VSMC_INTERNAL_BASIC_HPP
34 
35 #include <vsmc/internal/config.h>
36 
37 #include <vsmc/internal/assert.hpp>
40 #include <vsmc/internal/traits.hpp>
41 
42 #include <algorithm>
43 #include <array>
44 #include <atomic>
45 #include <cassert>
46 #include <cmath>
47 #include <cstddef>
48 #include <cstdint>
49 #include <cstdio>
50 #include <cstdlib>
51 #include <cstring>
52 #include <exception>
53 #include <fstream>
54 #include <functional>
55 #include <future>
56 #include <initializer_list>
57 #include <iomanip>
58 #include <iostream>
59 #include <iterator>
60 #include <limits>
61 #include <list>
62 #include <map>
63 #include <memory>
64 #include <mutex>
65 #include <new>
66 #include <numeric>
67 #include <random>
68 #include <sstream>
69 #include <stdexcept>
70 #include <string>
71 #include <thread>
72 #include <type_traits>
73 #include <utility>
74 #include <vector>
75 
76 namespace vsmc
77 {
78 
79 namespace internal
80 {
81 
82 template <typename T>
83 inline bool is_equal(const T &a, const T &b, std::true_type)
84 {
85  return !(a < b || a > b);
86 }
87 
88 template <typename T>
89 inline bool is_equal(const T &a, const T &b, std::false_type)
90 {
91  return a == b;
92 }
93 
94 template <typename T>
95 inline bool is_equal(const T &a, const T &b)
96 {
97  return is_equal(a, b, std::is_floating_point<T>());
98 }
99 
100 template <typename T>
101 inline bool is_zero(const T &a)
102 {
103  return is_equal(a, static_cast<T>(0));
104 }
105 
106 template <typename T>
107 inline bool is_one(const T &a)
108 {
109  return is_equal(a, static_cast<T>(1));
110 }
111 
112 template <typename T>
113 inline bool is_negative(const T &, std::true_type)
114 {
115  return false;
116 }
117 
118 template <typename T>
119 inline bool is_negative(const T &a, std::false_type)
120 {
121  return a < 0;
122 }
123 
124 template <typename T>
125 inline bool is_negative(const T &a)
126 {
127  return is_negative(a, std::is_unsigned<T>());
128 }
129 
130 template <typename T>
131 inline bool is_nullptr(T ptr, std::true_type)
132 {
133  return ptr == nullptr;
134 }
135 
136 template <typename T>
137 inline bool is_nullptr(T, std::false_type)
138 {
139  return false;
140 }
141 
142 template <typename T>
143 inline bool is_nullptr(T ptr)
144 {
145  return is_nullptr(ptr, std::is_pointer<T>());
146 }
147 
148 template <unsigned long long U,
149  int N = std::numeric_limits<unsigned long long>::digits - 1>
150 class Log2L
151 {
152  static constexpr unsigned long long M = 1ULL << N;
153 
154  public:
155  static constexpr int value = M <= U ? N : Log2L<U, N - 1>::value;
156 }; // class Log2L
157 
158 template <unsigned long long U>
159 class Log2L<U, 0>
160 {
161  public:
162  static constexpr int value = 0;
163 }; // class Log2L
164 
165 template <int N>
166 class Log2L<0, N>
167 {
168  public:
169  static constexpr int value = N + 1;
170 }; // class Log2L
171 
172 template <typename UIntType, UIntType U>
173 class Log2 : public Log2L<U, std::numeric_limits<UIntType>::digits - 1>
174 {
175 }; // class Log2
176 
177 template <typename T>
178 class BufferSize : public std::integral_constant<std::size_t, 8192 / sizeof(T)>
179 {
180 }; // class BufferSize;
181 
182 template <std::size_t, typename CharT, typename Traits, typename T,
183  std::size_t N>
184 inline void ostream_ary_space(std::basic_ostream<CharT, Traits> &,
185  const std::array<T, N> &, std::false_type)
186 {
187 }
188 
189 template <std::size_t, typename CharT, typename Traits, typename T,
190  std::size_t N>
191 inline void ostream_ary_space(std::basic_ostream<CharT, Traits> &os,
192  const std::array<T, N> &, std::true_type)
193 {
194  os << ' ';
195 }
196 
197 template <std::size_t, typename CharT, typename Traits, typename T,
198  std::size_t N>
199 inline void ostream_ary(std::basic_ostream<CharT, Traits> &,
200  const std::array<T, N> &, std::false_type)
201 {
202 }
203 
204 template <std::size_t K, typename CharT, typename Traits, typename T,
205  std::size_t N>
206 inline void ostream_ary(std::basic_ostream<CharT, Traits> &os,
207  const std::array<T, N> &ary, std::true_type)
208 {
209  os << std::get<K>(ary);
210  ostream_ary_space<K>(os, ary, std::integral_constant<bool, K + 1 != N>());
211  ostream_ary<K + 1>(os, ary, std::integral_constant<bool, K + 1 < N>());
212 }
213 
214 template <typename CharT, typename Traits, typename T, std::size_t N>
215 inline std::basic_ostream<CharT, Traits> &ostream(
216  std::basic_ostream<CharT, Traits> &os, const std::array<T, N> &ary)
217 {
218  if (!os)
219  return os;
220 
221  ostream_ary<0>(os, ary, std::integral_constant<bool, 0 < N>());
222 
223  return os;
224 }
225 
226 template <std::size_t, typename CharT, typename Traits, typename T,
227  std::size_t N>
228 inline void istream_ary(
229  std::basic_istream<CharT, Traits> &, std::array<T, N> &, std::false_type)
230 {
231 }
232 
233 template <std::size_t K, typename CharT, typename Traits, typename T,
234  std::size_t N>
235 inline void istream_ary(std::basic_istream<CharT, Traits> &is,
236  std::array<T, N> &ary, std::true_type)
237 {
238  is >> std::ws >> std::get<K>(ary);
239  istream_ary<K + 1>(is, ary, std::integral_constant<bool, K + 1 < N>());
240 }
241 
242 template <typename CharT, typename Traits, typename T, std::size_t N>
243 inline std::basic_istream<CharT, Traits> &istream(
244  std::basic_istream<CharT, Traits> &is, std::array<T, N> &ary)
245 {
246  if (!is)
247  return is;
248 
249  std::array<T, N> tmp;
250  istream_ary<0>(is, tmp, std::integral_constant<bool, 0 < N>());
251  if (is)
252  ary = std::move(tmp);
253 
254  return is;
255 }
256 
257 template <typename CharT, typename Traits, typename T, typename Alloc>
258 inline std::basic_ostream<CharT, Traits> &ostream(
259  std::basic_ostream<CharT, Traits> &os, const std::vector<T, Alloc> &vec)
260 {
261  if (!os)
262  return os;
263 
264  os << vec.size();
265  if (!os)
266  return os;
267 
268  for (const auto &v : vec)
269  os << ' ' << v;
270 
271  return os;
272 }
273 
274 template <typename CharT, typename Traits, typename T, typename Alloc>
275 inline std::basic_istream<CharT, Traits> &istream(
276  std::basic_istream<CharT, Traits> &is, std::vector<T, Alloc> &vec)
277 {
278  if (!is)
279  return is;
280 
281  std::size_t n = 0;
282  is >> n;
283  if (!is)
284  return is;
285 
286  std::vector<T, Alloc> tmp(n);
287  for (std::size_t i = 0; i != n; ++i)
288  is >> std::ws >> tmp[i];
289  if (is)
290  vec = std::move(tmp);
291 
292  return is;
293 }
294 
295 template <typename CharT, typename Traits, typename T, std::size_t N>
296 inline std::basic_ostream<CharT, Traits> &operator<<(
297  std::basic_ostream<CharT, Traits> &os, const std::array<T, N> &ary)
298 {
299  return ostream(os, ary);
300 }
301 
302 template <typename CharT, typename Traits, typename T, std::size_t N>
303 inline std::basic_istream<CharT, Traits> &operator>>(
304  std::basic_istream<CharT, Traits> &is, std::array<T, N> &ary)
305 {
306  return istream(is, ary);
307 }
308 
309 template <typename CharT, typename Traits, typename T, typename Alloc>
310 inline std::basic_ostream<CharT, Traits> &operator<<(
311  std::basic_ostream<CharT, Traits> &os, const std::vector<T, Alloc> &vec)
312 {
313  return ostream(os, vec);
314 }
315 
316 template <typename CharT, typename Traits, typename T, typename Alloc>
317 inline std::basic_istream<CharT, Traits> &operator>>(
318  std::basic_istream<CharT, Traits> &is, std::vector<T, Alloc> &vec)
319 {
320  return istream(is, vec);
321 }
322 
323 } // namespace vsmc::internal
324 
325 template <typename CharT, typename Traits, typename T, std::size_t N>
326 inline std::basic_ostream<CharT, Traits> &operator<<(
327  std::basic_ostream<CharT, Traits> &os, const std::array<T, N> &ary)
328 {
329  return internal::ostream(os, ary);
330 }
331 
332 template <typename CharT, typename Traits, typename T, std::size_t N>
333 inline std::basic_istream<CharT, Traits> &operator>>(
334  std::basic_istream<CharT, Traits> &is, std::array<T, N> &ary)
335 {
336  return internal::istream(is, ary);
337 }
338 
339 template <typename CharT, typename Traits, typename T, typename Alloc>
340 inline std::basic_ostream<CharT, Traits> &operator<<(
341  std::basic_ostream<CharT, Traits> &os, const std::vector<T, Alloc> &vec)
342 {
343  return internal::ostream(os, vec);
344 }
345 
346 template <typename CharT, typename Traits, typename T, typename Alloc>
347 inline std::basic_istream<CharT, Traits> &operator>>(
348  std::basic_istream<CharT, Traits> &is, std::vector<T, Alloc> &vec)
349 {
350  return internal::istream(is, vec);
351 }
352 
353 } // namespace vsmc
354 
355 #endif // VSMC_INTERNAL_BASIC_HPP
Definition: monitor.hpp:48
std::basic_istream< CharT, Traits > & operator>>(std::basic_istream< CharT, Traits > &is, std::array< T, N > &ary)
Definition: basic.hpp:303
void istream_ary(std::basic_istream< CharT, Traits > &, std::array< T, N > &, std::false_type)
Definition: basic.hpp:228
std::basic_ostream< CharT, Traits > & ostream(std::basic_ostream< CharT, Traits > &os, const std::array< T, N > &ary)
Definition: basic.hpp:215
void ostream_ary(std::basic_ostream< CharT, Traits > &, const std::array< T, N > &, std::false_type)
Definition: basic.hpp:199
void ostream_ary_space(std::basic_ostream< CharT, Traits > &, const std::array< T, N > &, std::false_type)
Definition: basic.hpp:184
bool is_equal(const T &a, const T &b, std::true_type)
Definition: basic.hpp:83
static constexpr int value
Definition: basic.hpp:155
std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const std::array< T, N > &ary)
Definition: basic.hpp:296
bool is_negative(const T &, std::true_type)
Definition: basic.hpp:113
bool is_one(const T &a)
Definition: basic.hpp:107
bool is_nullptr(T ptr, std::true_type)
Definition: basic.hpp:131
std::basic_istream< CharT, Traits > & istream(std::basic_istream< CharT, Traits > &is, std::array< T, N > &ary)
Definition: basic.hpp:243
bool is_zero(const T &a)
Definition: basic.hpp:101