#ifndef MEDUSA_BITS_UTILS_PRINT_HPP_ #define MEDUSA_BITS_UTILS_PRINT_HPP_ /** * @file * Printing helpers for std types. */ #include #include #include #include #include // additional ostream operators namespace std { /// Output pairs as `(1, 2)`. @ingroup utils template std::ostream& operator<<(std::ostream& xx, const std::pair& par) { return xx << "(" << par.first << "," << par.second << ")"; } /// Output arrays as `[1, 2, 3]`. @ingroup utils template std::ostream& operator<<(std::ostream& xx, const std::array& arr) { xx << "["; for(size_t i = 0; i < N; ++i) { xx << arr[i]; if(i < N - 1) xx << ", "; } xx << "]"; return xx; } /// Output vectors as `[1, 2, 3]`. @ingroup utils template std::ostream& operator<<(std::ostream& xx, const std::vector& arr) { // do it like the matlab does it. xx << "["; for(size_t i = 0; i < arr.size(); ++i) { xx << arr[i]; if(i < arr.size() - 1) xx << ", "; } xx << "]"; return xx; } /// Output nested vectors as `[[1, 2]; [3, 4]]`. @ingroup utils template std::ostream& operator<<(std::ostream& xx, const std::vector>& arr) { xx << "["; for(size_t i = 0; i < arr.size(); ++i) { for(size_t j = 0; j < arr[i].size(); ++j) { xx << arr[i][j]; if(j < arr[i].size() - 1) xx << ", "; } if(i < arr.size() - 1) xx << "; "; } xx << "]"; return xx; } /// @cond namespace tuple_print_internal { template struct TuplePrinter { static void print(std::ostream& os, const Tuple& t) { // recursive TuplePrinter::print(os, t); os << ", " << std::get(t); } }; template struct TuplePrinter { static void print(std::ostream& os, const Tuple& t) { // one element os << std::get<0>(t); } }; template struct TuplePrinter { static void print(std::ostream&, const Tuple&) {} }; // zero elt } // namespace tuple_print_internal /// @endcond /// Print a tuple as (1, 4.5, abc). @ingroup utils template std::ostream& operator<<(std::ostream& os, const std::tuple& t) { os << "("; tuple_print_internal::TuplePrinter::print(os, t); return os << ")"; } } // namespace std #endif // MEDUSA_BITS_UTILS_PRINT_HPP_