vSMC
vSMC: Scalable Monte Carlo
common.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/internal/common.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_COMMON_HPP
33 #define VSMC_INTERNAL_COMMON_HPP
34 
35 #include <vsmc/internal/assert.hpp>
36 #include <vsmc/internal/config.h>
39 #include <vsmc/internal/traits.hpp>
40 #include <vsmc/math/math.hpp>
42 
43 #include <algorithm>
44 #include <array>
45 #include <atomic>
46 #include <cassert>
47 #include <cmath>
48 #include <cstddef>
49 #include <cstdint>
50 #include <cstdio>
51 #include <cstdlib>
52 #include <cstring>
53 #include <exception>
54 #include <fstream>
55 #include <functional>
56 #include <future>
57 #include <initializer_list>
58 #include <iomanip>
59 #include <iostream>
60 #include <iterator>
61 #include <limits>
62 #include <list>
63 #include <map>
64 #include <memory>
65 #include <mutex>
66 #include <new>
67 #include <numeric>
68 #include <random>
69 #include <sstream>
70 #include <stdexcept>
71 #include <string>
72 #include <thread>
73 #include <tuple>
74 #include <type_traits>
75 #include <utility>
76 #include <vector>
77 
78 namespace vsmc
79 {
80 
81 namespace internal
82 {
83 
84 #ifdef VSMC_CLANG
85 #pragma clang diagnostic push
86 #pragma clang diagnostic ignored "-Wfloat-equal"
87 #endif
88 
89 template <typename T>
90 inline bool is_equal(const T &a, const T &b)
91 {
92  return a == b;
93 }
94 
95 #ifdef VSMC_CLANG
96 #pragma clang diagnostic pop
97 #endif
98 
99 template <typename UIntType>
100 inline std::string itos(UIntType i, std::true_type)
101 {
102  if (i == 0)
103  return std::string("0");
104 
105  char str[24] = {0};
106  std::size_t n = 0;
107  while (i > 0) {
108  str[n++] = '0' + i % 10;
109  i /= 10;
110  }
111  std::reverse(str, str + n);
112 
113  return std::string(str);
114 }
115 
116 template <typename IntType>
117 inline std::string itos(IntType i, std::false_type)
118 {
119  using uint_type = typename std::make_unsigned<IntType>::type;
120 
121  if (i < 0)
122  return "-" + itos(static_cast<uint_type>(-i), std::true_type());
123 
124  return itos(static_cast<uint_type>(i), std::true_type());
125 }
126 
127 template <typename IntType>
128 inline std::string itos(IntType i)
129 {
130  return itos(i, std::is_unsigned<IntType>());
131 }
132 
133 template <typename T, std::size_t Dim>
134 using Array = typename std::conditional<Dim == Dynamic, Vector<T>,
135  std::array<T, Dim>>::type;
136 
137 template <typename T, std::size_t N>
138 inline void resize(std::array<T, N> &, std::size_t)
139 {
140 }
141 
142 template <typename T>
143 inline void resize(Vector<T> &vec, std::size_t n)
144 {
145  vec.resize(n);
146 }
147 
148 } // namespace vsmc::internal
149 
150 template <typename CharT, typename Traits, typename T, std::size_t N>
151 inline std::basic_ostream<CharT, Traits> &operator<<(
152  std::basic_ostream<CharT, Traits> &os, const std::array<T, N> &ary)
153 {
154  if (!os.good() || N == 0)
155  return os;
156 
157  for (std::size_t i = 0; i < N - 1; ++i)
158  os << ary[i] << ' ';
159  os << ary[N - 1];
160 
161  return os;
162 }
163 
164 template <typename CharT, typename Traits, typename T, std::size_t N>
165 inline std::basic_istream<CharT, Traits> &operator>>(
166  std::basic_istream<CharT, Traits> &is, std::array<T, N> &ary)
167 {
168  if (!is.good())
169  return is;
170 
171  std::array<T, N> ary_tmp;
172  for (std::size_t i = 0; i != N; ++i)
173  is >> std::ws >> ary_tmp[i];
174 
175  if (is.good())
176  ary = std::move(ary_tmp);
177 
178  return is;
179 }
180 
181 template <typename CharT, typename Traits, typename T, std::size_t N>
182 inline std::basic_ostream<CharT, Traits> &operator<<(
183  std::basic_ostream<CharT, Traits> &os, const Vector<T> &vec)
184 {
185  if (!os.good() || vec.size() == 0)
186  return os;
187 
188  for (std::size_t i = 0; i < vec.size() - 1; ++i)
189  os << vec[i] << ' ';
190  os << vec[N - 1];
191 
192  return os;
193 }
194 
195 template <typename CharT, typename Traits, typename T, std::size_t N>
196 inline std::basic_istream<CharT, Traits> &operator>>(
197  std::basic_istream<CharT, Traits> &is, Vector<T> &vec)
198 {
199  if (!is.good())
200  return is;
201 
202  Vector<T> vec_tmp(vec.size());
203  for (std::size_t i = 0; i != N; ++i)
204  is >> std::ws >> vec_tmp[i];
205 
206  if (is.good())
207  vec = std::move(vec_tmp);
208 
209  return is;
210 }
211 
212 } // namespace vsmc
213 
214 #endif // VSMC_INTERNAL_COMMON_HPP
Definition: monitor.hpp:49
bool is_equal(const T &a, const T &b)
Definition: common.hpp:90
std::string itos(UIntType i, std::true_type)
Definition: common.hpp:100
std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const Sampler< T > &sampler)
Definition: sampler.hpp:861
void resize(std::array< T, N > &, std::size_t)
Definition: common.hpp:138
typename std::conditional< Dim==Dynamic, Vector< T >, std::array< T, Dim >>::type Array
Definition: common.hpp:135
std::basic_istream< CharT, Traits > & operator>>(std::basic_istream< CharT, Traits > &is, std::array< T, N > &ary)
Definition: common.hpp:165