QMCPACK
StlPrettyPrint.hpp
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2020 QMCPACK developers.
6 //
7 // File developed by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Laboratory
8 //
9 // File create by: Peter Doak
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 #ifndef QMCPLUSPLUS_STLPRETTYPRINT_HPP
13 #define QMCPLUSPLUS_STLPRETTYPRINT_HPP
14 
15 #include <iostream>
16 #include <vector>
17 
18 namespace qmcplusplus
19 {
20 
21 /** collapsed vector printout
22  * [3, 3, 2, 2] is printed as [3(x2), 2(x2)]
23  */
24 template<typename T>
25 std::ostream& operator<<(std::ostream& out, const std::vector<T>& rhs)
26 {
27  out << "[";
28  auto cursor = rhs.begin();
29  while (cursor != rhs.end())
30  {
31  // each iteration handles one unique value
32  const T ref_value = *cursor;
33  size_t count = 1;
34  while (++cursor != rhs.end() && *cursor == ref_value)
35  count++;
36  out << ref_value;
37  // identical elements are collapsed
38  if (count > 1)
39  out << "(x" << count << ")";
40  // if not the last element, add a separator
41  if (cursor != rhs.end())
42  out << ", ";
43  }
44  out << "]";
45  return out;
46 }
47 
48 } // namespace qmcplusplus
49 #endif
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43