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-2015, 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/config.h>
37 #include <vsmc/internal/assert.hpp>
39 #include <vsmc/internal/traits.hpp>
40 
41 #include <vsmc/math/cblas.hpp>
42 #include <vsmc/math/constants.hpp>
43 #include <vsmc/math/vmath.hpp>
44 
46 
47 #include <algorithm>
48 #include <array>
49 #include <atomic>
50 #include <cassert>
51 #include <cmath>
52 #include <cstddef>
53 #include <cstdint>
54 #include <cstdio>
55 #include <cstdlib>
56 #include <cstring>
57 #include <exception>
58 #include <fstream>
59 #include <functional>
60 #include <future>
61 #include <initializer_list>
62 #include <iomanip>
63 #include <iostream>
64 #include <iterator>
65 #include <limits>
66 #include <list>
67 #include <map>
68 #include <memory>
69 #include <mutex>
70 #include <new>
71 #include <numeric>
72 #include <random>
73 #include <sstream>
74 #include <stdexcept>
75 #include <string>
76 #include <thread>
77 #include <tuple>
78 #include <type_traits>
79 #include <utility>
80 #include <vector>
81 
82 namespace vsmc
83 {
84 
85 namespace internal
86 {
87 
88 template <typename UIntType>
89 inline std::string itos(UIntType i, std::true_type)
90 {
91  if (i == 0)
92  return std::string("0");
93 
94  char str[24] = {0};
95  std::size_t n = 0;
96  while (i > 0) {
97  str[n++] = '0' + i % 10;
98  i /= 10;
99  }
100  std::reverse(str, str + n);
101 
102  return std::string(str);
103 }
104 
105 template <typename IntType>
106 inline std::string itos(IntType i, std::false_type)
107 {
108  using uint_type = typename std::make_unsigned<IntType>::type;
109 
110  if (i < 0)
111  return "-" + itos(static_cast<uint_type>(-i), std::true_type());
112 
113  return itos(static_cast<uint_type>(i), std::true_type());
114 }
115 
116 template <typename IntType>
117 inline std::string itos(IntType i)
118 {
119  return itos(i, std::is_unsigned<IntType>());
120 }
121 
122 } // namespace vsmc::internal
123 
124 template <typename CharT, typename Traits, typename T, std::size_t N>
125 inline std::basic_ostream<CharT, Traits> &operator<<(
126  std::basic_ostream<CharT, Traits> &os, const std::array<T, N> &ary)
127 {
128  if (!os.good())
129  return os;
130 
131  for (std::size_t i = 0; i < N - 1; ++i)
132  os << ary[i] << ' ';
133  os << ary[N - 1];
134 
135  return os;
136 }
137 
138 template <typename CharT, typename Traits, typename T, std::size_t N>
139 inline std::basic_istream<CharT, Traits> &operator>>(
140  std::basic_istream<CharT, Traits> &is, std::array<T, N> &ary)
141 {
142  if (!is.good())
143  return is;
144 
145  std::array<T, N> ary_tmp;
146  for (std::size_t i = 0; i != N; ++i)
147  is >> std::ws >> ary_tmp[i];
148 
149  if (is.good())
150  ary = std::move(ary_tmp);
151 
152  return is;
153 }
154 
155 } // namespace vsmc
156 
157 #endif // VSMC_INTERNAL_COMMON_HPP
Definition: monitor.hpp:49
std::string itos(UIntType i, std::true_type)
Definition: common.hpp:89
std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const Sampler< T > &sampler)
Definition: sampler.hpp:929
std::basic_istream< CharT, Traits > & operator>>(std::basic_istream< CharT, Traits > &is, std::array< T, N > &ary)
Definition: common.hpp:139