ALPSCore reference
short_print.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1998-2018 ALPS Collaboration. See COPYRIGHT.TXT
3  * All rights reserved. Use is subject to license terms. See LICENSE.TXT
4  * For use in publications, see ACKNOWLEDGE.TXT
5  */
6 
7 #pragma once
8 
9 #include <vector>
10 #include <boost/array.hpp>
11 //#include <alps/multi_array.hpp> // FIXME
12 #include <ostream>
13 
14 namespace alps {
15  namespace detail {
16  template<typename T> struct short_print_proxy {
17  public:
18  explicit short_print_proxy(T const & v, std::size_t p): value(v), precision(p) {};
19  short_print_proxy(short_print_proxy<T> const & rhs): value(rhs.value) {};
20  T const & value;
21  std::size_t precision;
22  };
23 
24  template <typename T> std::ostream & operator<<(std::ostream & os, short_print_proxy<T> const & v) {
25  return os << v.value;
26  }
27  }
28 
29  template<typename T> detail::short_print_proxy<T const> short_print(T const & v, std::size_t p = 6) {
30  return detail::short_print_proxy<T const>(v, p);
31  }
32 
33  namespace detail {
34  std::ostream & operator<<(std::ostream & os, short_print_proxy<float> const & v);
35  std::ostream & operator<<(std::ostream & os, short_print_proxy<double> const & v);
36  std::ostream & operator<<(std::ostream & os, short_print_proxy<long double> const & v);
37 
38  template<typename T>
39  std::ostream & print_for_sequence(std::ostream & os, T const & value)
40  {
41  switch (value.size()) {\
42  case 0: \
43  return os << "[]";\
44  case 1: \
45  return os << "[" << short_print(value.front()) << "]";\
46  case 2: \
47  return os << "[" << short_print(value.front()) << "," << short_print(value.back()) << "]";\
48  default: \
49  return os << "[" << short_print(value.front()) << ",.." << short_print(value.size()) << "..," << short_print(value.back()) << "]";\
50  }
51  }
52 
53  template <typename T>
54  std::ostream & operator<<(std::ostream & os, short_print_proxy<std::vector<T> const> const & v)
55  {
56  return print_for_sequence(os, v.value);
57  }
58 
59  template <typename T, std::size_t N>
60  std::ostream & operator<<(std::ostream & os, short_print_proxy<boost::array<T, N> const> const & v)
61  {
62  return print_for_sequence(os, v.value);
63  }
64 //DEPENDENCE ON MULTIARRAY TO BE REMOVED
65 /* template <typename T, std::size_t N> std::ostream & operator<<(std::ostream & os, short_print_proxy<alps::multi_array<T, N> const> const & v) {
66  switch (v.value.num_elements()) {
67  case 0:
68  return os << "[]";
69  case 1:
70  return os << "[" << short_print(*(v.value.data())) << "]";
71  case 2:
72  return os << "[" << short_print(*(v.value.data())) << "," << short_print(*(v.value.data()+v.value.num_elements()-1)) << "]";
73  default:
74  return os << "[" << short_print(*(v.value.data())) << ",.." << short_print(v.value.num_elements()) << "..," << short_print(*(v.value.data()+v.value.num_elements()-1)) << "]";
75  }
76  // FIXME -> move to multi_array
77  }*/
78  }
79 }
80 
detail::short_print_proxy< T const > short_print(T const &v, std::size_t p=6)
Definition: short_print.hpp:29