PrintHelpers.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <algorithm>
7 #include <sstream>
8 #include <string>
9 #include <utility>
10 #include <vector>
11 
12 /*!
13  * \ingroup UtilitiesGroup
14  * \brief Applies the function f(out, it) to each item from begin to
15  * end, separated by commas and surrounded by parens.
16  */
17 template <typename ForwardIt, typename Func>
18 void sequence_print_helper(std::ostream& out, ForwardIt&& begin,
19  ForwardIt&& end, Func f) noexcept {
20  out << "(";
21  if (begin != end) {
22  while (true) {
23  f(out, begin++);
24  if (begin == end) {
25  break;
26  }
27  out << ",";
28  }
29  }
30  out << ")";
31 }
32 
33 /*!
34  * \ingroup UtilitiesGroup
35  * \brief Prints all the items as a comma separated list surrounded by parens.
36  */
37 template <typename ForwardIt>
38 void sequence_print_helper(std::ostream& out, ForwardIt&& begin,
39  ForwardIt&& end) noexcept {
41  out, std::forward<ForwardIt>(begin), std::forward<ForwardIt>(end),
42  [](std::ostream& os, const ForwardIt& it) noexcept { os << *it; });
43 }
44 
45 //@{
46 /*!
47  * \ingroup UtilitiesGroup
48  * Like sequence_print_helper, but sorts the string representations.
49  */
50 template <typename ForwardIt, typename Func>
51 void unordered_print_helper(std::ostream& out, ForwardIt&& begin,
52  ForwardIt&& end, Func f) noexcept {
54  while (begin != end) {
56  f(ss, begin++);
57  entries.push_back(ss.str());
58  }
59  std::sort(entries.begin(), entries.end());
60  sequence_print_helper(out, entries.begin(), entries.end());
61 }
62 
63 template <typename ForwardIt>
64 void unordered_print_helper(std::ostream& out, ForwardIt&& begin,
65  ForwardIt&& end) noexcept {
67  out, std::forward<ForwardIt>(begin), std::forward<ForwardIt>(end),
68  [](std::ostream& os, const ForwardIt& it) noexcept { os << *it; });
69 }
70 //@}
void unordered_print_helper(std::ostream &out, ForwardIt &&begin, ForwardIt &&end, Func f) noexcept
Definition: PrintHelpers.hpp:51
void sequence_print_helper(std::ostream &out, ForwardIt &&begin, ForwardIt &&end, Func f) noexcept
Applies the function f(out, it) to each item from begin to end, separated by commas and surrounded by...
Definition: PrintHelpers.hpp:18