MCKL
Monte Carlo Kernel Library
iostream.hpp
Go to the documentation of this file.
1 //============================================================================
2 // MCKL/include/mckl/internal/iostream.hpp
3 //----------------------------------------------------------------------------
4 // MCKL: Monte Carlo Kernel Library
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013-2018, 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 MCKL_INTERNAL_IOSTREAM_HPP
33 #define MCKL_INTERNAL_IOSTREAM_HPP
34 
35 #include <mckl/internal/config.h>
36 #include <array>
37 #include <iostream>
38 #include <list>
39 #include <vector>
40 
41 namespace mckl {
42 
43 namespace internal {
44 
45 template <typename CharT, typename Traits, typename T, std::size_t N>
46 inline std::basic_ostream<CharT, Traits> &ostream(
47  std::basic_ostream<CharT, Traits> &os, const std::array<T, N> &ary)
48 {
49  if (!os) {
50  return os;
51  }
52 
53  os << N;
54  if (!os) {
55  return os;
56  }
57 
58  for (const auto &v : ary) {
59  os << ' ' << v;
60  }
61 
62  return os;
63 }
64 
65 template <typename CharT, typename Traits, typename T, std::size_t N>
66 inline std::basic_istream<CharT, Traits> &istream(
67  std::basic_istream<CharT, Traits> &is, std::array<T, N> &ary)
68 {
69  if (!is) {
70  return is;
71  }
72 
73  std::size_t n = 0;
74  is >> n;
75  if (!is) {
76  return is;
77  }
78 
79  std::array<T, N> tmp;
80 
81  n = std::min(n, N);
82  for (std::size_t i = 0; i != n; ++i) {
83  is >> std::ws >> tmp[i];
84  }
85  if (is) {
86  ary = std::move(tmp);
87  }
88 
89  return is;
90 }
91 
92 template <typename CharT, typename Traits, typename T, typename Alloc>
93 inline std::basic_ostream<CharT, Traits> &ostream(
94  std::basic_ostream<CharT, Traits> &os, const std::vector<T, Alloc> &vec)
95 {
96  if (!os) {
97  return os;
98  }
99 
100  os << vec.size();
101  if (!os) {
102  return os;
103  }
104 
105  for (const auto &v : vec) {
106  os << ' ' << v;
107  }
108 
109  return os;
110 }
111 
112 template <typename CharT, typename Traits, typename T, typename Alloc>
113 inline std::basic_istream<CharT, Traits> &istream(
114  std::basic_istream<CharT, Traits> &is, std::vector<T, Alloc> &vec)
115 {
116  if (!is) {
117  return is;
118  }
119 
120  std::size_t n = 0;
121  is >> n;
122  if (!is) {
123  return is;
124  }
125 
126  std::vector<T, Alloc> tmp(n);
127  for (std::size_t i = 0; i != n; ++i) {
128  is >> std::ws >> tmp[i];
129  }
130  if (is) {
131  vec = std::move(tmp);
132  }
133 
134  return is;
135 }
136 
137 template <typename CharT, typename Traits, typename T, typename Alloc>
138 inline std::basic_ostream<CharT, Traits> &ostream(
139  std::basic_ostream<CharT, Traits> &os, const std::list<T, Alloc> &lst)
140 {
141  if (!os) {
142  return os;
143  }
144 
145  os << lst.size();
146  if (!os) {
147  return os;
148  }
149 
150  for (const auto &v : lst) {
151  os << ' ' << v;
152  }
153 
154  return os;
155 }
156 
157 template <typename CharT, typename Traits, typename T, typename Alloc>
158 inline std::basic_istream<CharT, Traits> &istream(
159  std::basic_istream<CharT, Traits> &is, std::list<T, Alloc> &lst)
160 {
161  if (!is) {
162  return is;
163  }
164 
165  std::size_t n = 0;
166  is >> n;
167  if (!is) {
168  return is;
169  }
170 
171  std::list<T, Alloc> tmp;
172  T val;
173  for (std::size_t i = 0; i != n; ++i) {
174  is >> std::ws >> val;
175  tmp.push_back(val);
176  }
177  if (is) {
178  lst = std::move(tmp);
179  }
180 
181  return is;
182 }
183 
184 } // namespace internal
185 
186 template <typename CharT, typename Traits, typename T, std::size_t N>
187 inline std::basic_ostream<CharT, Traits> &operator<<(
188  std::basic_ostream<CharT, Traits> &os, const std::array<T, N> &ary)
189 {
190  return internal::ostream(os, ary);
191 }
192 
193 template <typename CharT, typename Traits, typename T, std::size_t N>
194 inline std::basic_istream<CharT, Traits> &operator>>(
195  std::basic_istream<CharT, Traits> &is, std::array<T, N> &ary)
196 {
197  return internal::istream(is, ary);
198 }
199 
200 template <typename CharT, typename Traits, typename T, typename Alloc>
201 inline std::basic_ostream<CharT, Traits> &operator<<(
202  std::basic_ostream<CharT, Traits> &os, const std::vector<T, Alloc> &vec)
203 {
204  return internal::ostream(os, vec);
205 }
206 
207 template <typename CharT, typename Traits, typename T, typename Alloc>
208 inline std::basic_istream<CharT, Traits> &operator>>(
209  std::basic_istream<CharT, Traits> &is, std::vector<T, Alloc> &vec)
210 {
211  return internal::istream(is, vec);
212 }
213 
214 template <typename CharT, typename Traits, typename T, typename Alloc>
215 inline std::basic_ostream<CharT, Traits> &operator<<(
216  std::basic_ostream<CharT, Traits> &os, const std::list<T, Alloc> &lst)
217 {
218  return internal::ostream(os, lst);
219 }
220 
221 template <typename CharT, typename Traits, typename T, typename Alloc>
222 inline std::basic_istream<CharT, Traits> &operator>>(
223  std::basic_istream<CharT, Traits> &is, std::list<T, Alloc> &lst)
224 {
225  return internal::istream(is, lst);
226 }
227 
228 } // namespace mckl
229 
230 #endif // MCKL_INTERNAL_IOSTREAM_HPP
std::basic_ostream< CharT, Traits > & ostream(std::basic_ostream< CharT, Traits > &os, const std::array< T, N > &ary)
Definition: iostream.hpp:46
Definition: mcmc.hpp:40
std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const Matrix< T, Layout, Alloc > &mat)
Output operator.
Definition: matrix.hpp:745
std::basic_istream< CharT, Traits > & istream(std::basic_istream< CharT, Traits > &is, std::array< T, N > &ary)
Definition: iostream.hpp:66
std::basic_istream< CharT, Traits > & operator>>(std::basic_istream< CharT, Traits > &is, Matrix< T, Layout, Alloc > &mat)
Input operator.
Definition: matrix.hpp:769