XACC
iomanip.h
Go to the documentation of this file.
1 /*
2  * This file is part of Quantum++.
3  *
4  * MIT License
5  *
6  * Copyright (c) 2013 - 2020 Vlad Gheorghiu.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  */
26 
32 #ifndef INTERNAL_CLASSES_IOMANIP_H_
33 #define INTERNAL_CLASSES_IOMANIP_H_
34 
35 namespace qpp {
36 namespace internal {
37 // ostream manipulators for nice formatting of
38 // Eigen matrices and STL/C-style containers/vectors
39 
40 template <typename InputIterator>
41 class IOManipRange : public IDisplay {
42  InputIterator first_, last_;
43  std::string separator_, start_, end_;
44  double chop_;
45 
46  public:
47  explicit IOManipRange(InputIterator first, InputIterator last,
48  const std::string& separator,
49  const std::string& start = "[",
50  const std::string& end = "]", double chop = qpp::chop)
51  : first_{first}, last_{last},
52  separator_{separator}, start_{start}, end_{end}, chop_{chop} {}
53 
54  // to silence -Weffc++ warnings for classes that have pointer members
55  // (whenever we have a pointer instantiation,
56  // i.e. iterator is a raw pointer)
57  IOManipRange(const IOManipRange&) = default;
58 
59  IOManipRange& operator=(const IOManipRange&) = default;
60 
61  private:
62  std::ostream& display(std::ostream& os) const override {
63  os << start_;
64 
65  bool first = true;
66  for (InputIterator it = first_; it != last_; ++it) {
67  if (!first)
68  os << separator_;
69  first = false;
70  os << abs_chop(*it, chop_);
71  }
72  os << end_;
73 
74  return os;
75  }
76 }; // class IOManipRange
77 
78 template <typename PointerType>
79 class IOManipPointer : public IDisplay {
80  const PointerType* p_;
81  idx N_;
82  std::string separator_, start_, end_;
83  double chop_;
84 
85  public:
86  explicit IOManipPointer(const PointerType* p, idx N,
87  const std::string& separator,
88  const std::string& start = "[",
89  const std::string& end = "]",
90  double chop = qpp::chop)
91  : p_{p}, N_{N},
92  separator_{separator}, start_{start}, end_{end}, chop_{chop} {}
93 
94  // to silence -Weffc++ warnings for classes that have pointer members
95  IOManipPointer(const IOManipPointer&) = default;
96 
97  IOManipPointer& operator=(const IOManipPointer&) = default;
98 
99  private:
100  std::ostream& display(std::ostream& os) const override {
101  os << start_;
102 
103  for (idx i = 0; i < N_ - 1; ++i)
104  os << abs_chop(p_[i], chop_) << separator_;
105  if (N_ > 0)
106  os << abs_chop(p_[N_ - 1], chop_);
107 
108  os << end_;
109 
110  return os;
111  }
112 }; // class IOManipPointer
113 
114 // silence g++4.8.x bogus warning -Wnon-virtual-dtor for
115 // qpp::internal::Display_impl_ when class qpp::internal::IOManipEigen
116 // privately inherits from it
117 #if ((__GNUC__ == 4) && (__GNUC_MINOR__ == 8) && !__clang__)
118 #pragma GCC diagnostic push
119 #pragma GCC diagnostic ignored "-Weffc++"
120 #endif
121 class IOManipEigen : public IDisplay, private Display_Impl_ {
122 #if ((__GNUC__ == 4) && (__GNUC_MINOR__ == 8) && !__clang__)
123 #pragma GCC diagnostic pop
124 #endif
125  cmat A_;
126  double chop_;
127 
128  public:
129  // Eigen matrices
130  template <typename Derived>
131  explicit IOManipEigen(const Eigen::MatrixBase<Derived>& A,
132  double chop = qpp::chop)
133  : A_{A.template cast<cplx>()}, // copy, so we can bind rvalues safely
134  chop_{chop} {}
135 
136  // Complex numbers
137  explicit IOManipEigen(const cplx z, double chop = qpp::chop)
138  : A_{cmat::Zero(1, 1)}, chop_{chop} {
139  // put the complex number inside an Eigen matrix
140  A_(0, 0) = z;
141  }
142 
143  private:
144  std::ostream& display(std::ostream& os) const override {
145  return display_impl_(A_, os, chop_);
146  }
147 }; // class IOManipEigen
148 
149 } /* namespace internal */
150 } /* namespace qpp */
151 
152 #endif /* INTERNAL_CLASSES_IOMANIP_H_ */
Quantum++ main namespace.
Definition: circuits.h:35