1 /* Copyright 2017-2021 PaGMO development team
2 
3 This file is part of the PaGMO library.
4 
5 The PaGMO library is free software; you can redistribute it and/or modify
6 it under the terms of either:
7 
8   * the GNU Lesser General Public License as published by the Free
9     Software Foundation; either version 3 of the License, or (at your
10     option) any later version.
11 
12 or
13 
14   * the GNU General Public License as published by the Free Software
15     Foundation; either version 3 of the License, or (at your option) any
16     later version.
17 
18 or both in parallel, as here.
19 
20 The PaGMO library is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
23 for more details.
24 
25 You should have received copies of the GNU General Public License and the
26 GNU Lesser General Public License along with the PaGMO library.  If not,
27 see https://www.gnu.org/licenses/. */
28 
29 #include <algorithm>
30 #include <iostream>
31 #include <iterator>
32 #include <string>
33 #include <utility>
34 #include <vector>
35 
36 #include <pagmo/io.hpp>
37 
38 namespace pagmo
39 {
40 
41 namespace detail
42 {
43 
44 // Construct from table headers, and optional indentation to be used when printing
45 // the table.
table(std::vector<std::string> headers,std::string indent)46 table::table(std::vector<std::string> headers, std::string indent)
47     : m_indent(std::move(indent)), m_headers(std::move(headers))
48 {
49     std::transform(m_headers.begin(), m_headers.end(), std::back_inserter(m_sizes),
50                    [](const std::string &s) { return s.size(); });
51 }
52 
53 // Print the table to stream.
operator <<(std::ostream & os,const table & t)54 std::ostream &operator<<(std::ostream &os, const table &t)
55 {
56     // Small helper functor to print a single row.
57     auto print_row = [&t, &os](const std::vector<std::string> &row) {
58         std::transform(row.begin(), row.end(), t.m_sizes.begin(), std::ostream_iterator<std::string>(os),
59                        [](const std::string &str, const table::s_size_t &size) {
60                            return str + std::string(size - str.size() + 2u, ' ');
61                        });
62     };
63     os << t.m_indent;
64     print_row(t.m_headers);
65     os << '\n' << t.m_indent;
66     std::transform(t.m_sizes.begin(), t.m_sizes.end(), std::ostream_iterator<std::string>(os),
67                    [](const table::s_size_t &size) { return std::string(size + 2u, '-'); });
68     os << '\n';
69     for (const auto &v : t.m_rows) {
70         os << t.m_indent;
71         print_row(v);
72         os << '\n';
73     }
74     return os;
75 }
76 
77 } // namespace detail
78 
79 } // namespace pagmo
80