xerus
a general purpose tensor library
containerOutput.h
Go to the documentation of this file.
1 // Xerus - A General Purpose Tensor Library
2 // Copyright (C) 2014-2017 Benjamin Huber and Sebastian Wolf.
3 //
4 // Xerus is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as published
6 // by the Free Software Foundation, either version 3 of the License,
7 // or (at your option) any later version.
8 //
9 // Xerus is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
13 //
14 // You should have received a copy of the GNU Affero General Public License
15 // along with Xerus. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // For further information on Xerus visit https://libXerus.org
18 // or contact us at contact@libXerus.org.
19 
25 #pragma once
26 #include <tuple>
27 #include <set>
28 #include <vector>
29 #include <map>
30 #include <iostream>
31 
32 namespace xerus { namespace misc {
33  // Internal Helper function to print tuples.
34  namespace internal {
35  template<size_t n = 0, typename... Tp, typename std::enable_if<n+1 == sizeof...(Tp), int>::type = 0>
36  inline void print(std::ostream& _out, const std::tuple<Tp...>& t) {
37  _out << std::get<n>(t);
38  }
39 
40  template<size_t n = 0, typename... Tp, typename std::enable_if<n+1 < sizeof...(Tp), int>::type = 0>
41  inline void print(std::ostream& _out, const std::tuple<Tp...>& t) {
42  _out << std::get<n>(t) << ", ";
43  print<n + 1, Tp...>(_out, t);
44  }
45  }
46 
47 
49  template<class... Tp>
50  std::ostream& operator<<(std::ostream& _out, const std::tuple<Tp...>& _tuple) {
51  _out << "<";
52  xerus::misc::internal::print<0, Tp...>(_out, _tuple);
53  _out << ">";
54  return _out;
55  }
56 
57 
59  template<class first_t, class second_t>
60  std::ostream& operator<<(std::ostream& _out, const std::pair<first_t, second_t>& _pair) {
61  _out << "[ " << _pair.first << " | " << _pair.second << " ]";
62  return _out;
63  }
64 
65 
67  template<class item_t, class... rest_t>
68  std::ostream& operator<<(std::ostream& _out, const std::vector<item_t, rest_t...>& _container) {
69  if(_container.size() == 0) { _out << "{ }"; return _out; }
70  _out << "{ ";
71  for(const item_t& item : _container) { _out << item << ", "; }
72  _out << "\b\b }";
73  return _out;
74  }
75 
76 
78  template<class item_t, class... rest_t>
79  std::ostream& operator<<(std::ostream& _out, const std::set<item_t, rest_t...>& _container) {
80  if(_container.size() == 0) { _out << "{ }"; return _out; }
81  _out << "{ ";
82  for(const item_t& item : _container) { _out << item << ", "; }
83  _out << "\b\b }";
84  return _out;
85  }
86 
87 
89  template<class T, class U>
90  std::ostream& operator<<(std::ostream& _out, const std::map<T,U>& _set) {
91  if(_set.size() == 0) { _out << "{ }"; return _out; }
92  _out << "{ ";
93  for(const auto& item : _set) { _out << "(" << item.first << ", " << item.second << "), "; }
94  _out << "\b\b }";
95  return _out;
96  }
97 
98 }} // namespaces xerus::misc
The main namespace of xerus.
Definition: basic.h:37
void print(std::ostream &_out, const std::tuple< Tp... > &t)
std::ostream & operator<<(std::ostream &_out, const std::tuple< Tp... > &_tuple)
Allow to pipe tuples to ostreams.