32 #ifndef VSMC_INTERNAL_COMMON_HPP 33 #define VSMC_INTERNAL_COMMON_HPP 57 #include <initializer_list> 74 #include <type_traits> 85 #pragma clang diagnostic push 86 #pragma clang diagnostic ignored "-Wfloat-equal" 96 #pragma clang diagnostic pop 99 template <
typename UIntType>
100 inline std::string
itos(UIntType i, std::true_type)
103 return std::string(
"0");
108 str[n++] =
'0' + i % 10;
111 std::reverse(str, str + n);
113 return std::string(str);
116 template <
typename IntType>
117 inline std::string
itos(IntType i, std::false_type)
119 using uint_type =
typename std::make_unsigned<IntType>::type;
122 return "-" +
itos(static_cast<uint_type>(-i), std::true_type());
124 return itos(static_cast<uint_type>(i), std::true_type());
127 template <
typename IntType>
128 inline std::string
itos(IntType i)
130 return itos(i, std::is_unsigned<IntType>());
133 template <
typename T, std::
size_t Dim>
134 using Array =
typename std::conditional<Dim == Dynamic, Vector<T>,
135 std::array<T, Dim>>::type;
137 template <
typename T, std::
size_t N>
138 inline void resize(std::array<T, N> &, std::size_t)
142 template <
typename T>
150 template <
typename CharT,
typename Traits,
typename T, std::
size_t N>
152 std::basic_ostream<CharT, Traits> &os,
const std::array<T, N> &ary)
154 if (!os.good() || N == 0)
157 for (std::size_t i = 0; i < N - 1; ++i)
164 template <
typename CharT,
typename Traits,
typename T, std::
size_t N>
166 std::basic_istream<CharT, Traits> &is, std::array<T, N> &ary)
171 std::array<T, N> ary_tmp;
172 for (std::size_t i = 0; i != N; ++i)
173 is >> std::ws >> ary_tmp[i];
176 ary = std::move(ary_tmp);
181 template <
typename CharT,
typename Traits,
typename T, std::
size_t N>
183 std::basic_ostream<CharT, Traits> &os,
const Vector<T> &vec)
185 if (!os.good() || vec.size() == 0)
188 for (std::size_t i = 0; i < vec.size() - 1; ++i)
195 template <
typename CharT,
typename Traits,
typename T, std::
size_t N>
197 std::basic_istream<CharT, Traits> &is,
Vector<T> &vec)
203 for (std::size_t i = 0; i != N; ++i)
204 is >> std::ws >> vec_tmp[i];
207 vec = std::move(vec_tmp);
214 #endif // VSMC_INTERNAL_COMMON_HPP
bool is_equal(const T &a, const T &b)
std::string itos(UIntType i, std::true_type)
std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const Sampler< T > &sampler)
void resize(std::array< T, N > &, std::size_t)
typename std::conditional< Dim==Dynamic, Vector< T >, std::array< T, Dim >>::type Array
std::basic_istream< CharT, Traits > & operator>>(std::basic_istream< CharT, Traits > &is, std::array< T, N > &ary)