Medusa  1.1
Coordinate Free Mehless Method implementation
print.hpp
Go to the documentation of this file.
1 #ifndef MEDUSA_BITS_UTILS_PRINT_HPP_
2 #define MEDUSA_BITS_UTILS_PRINT_HPP_
3 
9 #include <iostream>
10 #include <vector>
11 #include <array>
12 #include <utility>
13 #include <tuple>
14 
15 // additional ostream operators
16 namespace std {
17 
19 template<class T, class U>
20 std::ostream& operator<<(std::ostream& xx, const std::pair<T, U>& par) {
21  return xx << "(" << par.first << "," << par.second << ")";
22 }
23 
25 template<class T, size_t N>
26 std::ostream& operator<<(std::ostream& xx, const std::array<T, N>& arr) {
27  xx << "[";
28  for (size_t i = 0; i < N; ++i) {
29  xx << arr[i];
30  if (i < N - 1) xx << ", ";
31  }
32  xx << "]";
33  return xx;
34 }
35 
37 template<class T, class A>
38 std::ostream& operator<<(std::ostream& xx, const std::vector<T, A>& arr) {
39  // do it like the matlab does it.
40  xx << "[";
41  for (size_t i = 0; i < arr.size(); ++i) {
42  xx << arr[i];
43  if (i < arr.size() - 1) xx << ", ";
44  }
45  xx << "]";
46  return xx;
47 }
48 
50 template<class T, class A>
51 std::ostream& operator<<(std::ostream& xx, const std::vector<std::vector<T, A>>& arr) {
52  xx << "[";
53  for (size_t i = 0; i < arr.size(); ++i) {
54  for (size_t j = 0; j < arr[i].size(); ++j) {
55  xx << arr[i][j];
56  if (j < arr[i].size() - 1) xx << ", ";
57  }
58  if (i < arr.size() - 1) xx << "; ";
59  }
60  xx << "]";
61  return xx;
62 }
63 
65 namespace tuple_print_internal {
66 template <class Tuple, std::size_t N>
67 struct TuplePrinter {
68  static void print(std::ostream& os, const Tuple& t) { // recursive
69  TuplePrinter<Tuple, N - 1>::print(os, t);
70  os << ", " << std::get<N - 1>(t);
71  }
72 };
73 
74 template <class Tuple>
75 struct TuplePrinter<Tuple, 1> {
76  static void print(std::ostream& os, const Tuple& t) { // one element
77  os << std::get<0>(t);
78  }
79 };
80 
81 template <class Tuple>
82 struct TuplePrinter<Tuple, 0> { static void print(std::ostream&, const Tuple&) {} }; // zero elt
83 
84 } // namespace tuple_print_internal
86 
88 template <class... Args>
89 std::ostream& operator<<(std::ostream& os, const std::tuple<Args...>& t) {
90  os << "(";
91  tuple_print_internal::TuplePrinter<decltype(t), sizeof...(Args)>::print(os, t);
92  return os << ")";
93 }
94 
95 
96 } // namespace std
97 
98 #endif // MEDUSA_BITS_UTILS_PRINT_HPP_
std::operator<<
std::ostream & operator<<(std::ostream &os, const std::tuple< Args... > &t)
Print a tuple as (1, 4.5, abc).
Definition: print.hpp:89