Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
Teuchos_TableFormat.cpp
1// @HEADER
2// *****************************************************************************
3// Teuchos: Common Tools Package
4//
5// Copyright 2004 NTESS and the Teuchos contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#include <iostream>
11
13#include "Teuchos_Assert.hpp"
14
15
16namespace Teuchos {
17
18
19std::string TableFormat::thinline() const
20{
21 std::ostringstream toss;
22 for (int i=0; i<pageWidth_; i++)
23 {
24 toss << "-";
25 }
26 return toss.str();
27}
28
29
30
31std::string TableFormat::thickline() const
32{
33 std::ostringstream toss;
34 for (int i=0; i<pageWidth_; i++)
35 {
36 toss << "=";
37 }
38 return toss.str();
39}
40
41
42std::string TableFormat::blanks(int size) const
43{
44 std::ostringstream toss;
45 for (int i=0; i<size; i++)
46 {
47 toss << " ";
48 }
49 return toss.str();
50}
51
52
54 const std::string& name,
55 const TableColumn& column
56 ) const
57{
58 int rtn = name.length();
59
60 for (int i=0; i<column.numRows(); i++)
61 {
62 int x = column.entry(i)->toString().length();
63 rtn = std::max(rtn, x);
64 }
65
66 return rtn + columnSpacing_;
67}
68
69
71 std::ostream& out,
73 ) const
74{
75 TEUCHOS_TEST_FOR_EXCEPT(entries.size() != columnWidths_.size()
76 && columnWidths_.size() != 0);
77
78 std::ios::fmtflags f( out.flags() );
79 for (Array<RCP<TableEntry> >::size_type i=0; i<entries.size(); i++)
80 {
81 int cw = defaultColumnWidth();
82 if (columnWidths_.size() != 0) cw = columnWidths_[i];
83
84 out << std::left << std::setw(cw) << entries[i]->toString();
85 }
86 out << std::endl;
87 out.flags(f);
88}
89
90
92 std::ostream& out,
93 int rowIndex,
95 ) const
96{
98 for (Array<TableColumn>::size_type i=0; i<columns.size(); i++)
99 {
100 entries[i] = columns[i].entry(rowIndex);
101 }
102
103 writeRow(out, entries);
104}
105
106
108 std::ostream& out,
109 const std::string& header,
112 ) const
113{
114 std::ios::fmtflags f(out.flags());
115
116 /* compute the total width */
117 int pgWidth = 0;
119 {
120 int cw = defaultColumnWidth();
121 if (columnWidths_.size() != 0) cw = columnWidths_[i];
122 pgWidth += cw;
123 }
124 setPageWidth(std::max(pageWidth_, pgWidth));
125
126 /* write the header */
127 out << thickline() << std::endl;
128 out << std::endl;
129 int numBlanks = (pageWidth_ - header.length())/2;
130 out << blanks(numBlanks) << header << std::endl;
131 out << std::endl;
132
133 /* write the column titles */
135 {
136 int cw = defaultColumnWidth();
137 if (columnWidths_.size() != 0) cw = columnWidths_[i];
138
139 out << std::left << std::setw(cw) << columnNames[i];
140 }
141 out << std::endl;
142
143 /* ensure that all columns have the same number of rows */
144 int numRows = columns[0].numRows();
145 for (Array<TableColumn>::size_type i=1; i<columns.size(); i++)
146 {
147 TEUCHOS_ASSERT_EQUALITY(columns[i].numRows(), numRows);
148 }
149
150 /* write the table data */
151 for (int i=0; i<numRows; i++)
152 {
153 if (i % lineInterval_ == 0)
154 out << std::left << thinline() << std::endl;
155 writeRow(out, i, columns);
156 }
157
158 /* write the footer */
159 out << thickline() << std::endl;
160
161 // Restore flags
162 out.flags(f);
163}
164
165
166} // namespace Teuchos
Provides utilities for formatting tabular output.
Replacement for std::vector that is compatible with the Teuchos Memory Management classes.
size_type size() const
Smart reference counting pointer class for automatic garbage collection.
std::string thickline() const
Return a thick horizontal line in equal signs "====" the width of the page.
void setPageWidth(int pw) const
Set the number of characters on a line. This quantity can be updated within the const method writeWho...
std::string blanks(int size) const
Return a std::string full of blanks up to the requested size.
void writeRow(std::ostream &out, const Array< RCP< TableEntry > > &entries) const
Write the row of entries.
std::string thinline() const
Return a horizontal line in dashes "----" the width of the page.
void writeWholeTable(std::ostream &out, const std::string &tableTitle, const Array< std::string > &columnNames, const Array< TableColumn > &columns) const
int computeRequiredColumnWidth(const std::string &name, const TableColumn &column) const
Computes the column width required to write all values to the required precision.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
This macro is designed to be a short version of TEUCHOS_TEST_FOR_EXCEPTION() that is easier to call.
#define TEUCHOS_ASSERT_EQUALITY(val1, val2)
This macro is checks that to numbers are equal and if not then throws an exception with a good error ...
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...