Teuchos - Trilinos Tools Package
Version of the Day
Toggle main menu visibility
Loading...
Searching...
No Matches
comm
src
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
12
#include "
Teuchos_TableFormat.hpp
"
13
#include "Teuchos_Assert.hpp"
14
15
16
namespace
Teuchos
{
17
18
19
std::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
31
std::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
42
std::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
53
int
TableFormat::computeRequiredColumnWidth
(
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
70
void
TableFormat::writeRow
(
71
std::ostream& out,
72
const
Array
<
RCP<TableEntry>
>& entries
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
91
void
TableFormat::writeRow
(
92
std::ostream& out,
93
int
rowIndex,
94
const
Array<TableColumn>
& columns
95
)
const
96
{
97
Array<RCP<TableEntry>
> entries(columns.
size
());
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
107
void
TableFormat::writeWholeTable
(
108
std::ostream& out,
109
const
std::string& header,
110
const
Array<std::string>
& columnNames,
111
const
Array<TableColumn>
& columns
112
)
const
113
{
114
std::ios::fmtflags f(out.flags());
115
116
/* compute the total width */
117
int
pgWidth = 0;
118
for
(
Array<TableColumn>::size_type
i=0; i<columnNames.
size
(); i++)
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 */
134
for
(
Array<std::string>::size_type
i=0; i<columnNames.
size
(); i++)
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
Teuchos_TableFormat.hpp
Provides utilities for formatting tabular output.
Teuchos::Array
Replacement for std::vector that is compatible with the Teuchos Memory Management classes.
Definition
Teuchos_Array.hpp:163
Teuchos::Array::size
size_type size() const
Definition
Teuchos_Array.hpp:1006
Teuchos::Array::size_type
Ordinal size_type
The type of Array sizes and capacities.
Definition
Teuchos_Array.hpp:205
Teuchos::RCP
Smart reference counting pointer class for automatic garbage collection.
Definition
Teuchos_RCPDecl.hpp:397
Teuchos::TableColumn
Definition
Teuchos_TableColumn.hpp:30
Teuchos::TableFormat::thickline
std::string thickline() const
Return a thick horizontal line in equal signs "====" the width of the page.
Definition
Teuchos_TableFormat.cpp:31
Teuchos::TableFormat::setPageWidth
void setPageWidth(int pw) const
Set the number of characters on a line. This quantity can be updated within the const method writeWho...
Definition
Teuchos_TableFormat.hpp:60
Teuchos::TableFormat::blanks
std::string blanks(int size) const
Return a std::string full of blanks up to the requested size.
Definition
Teuchos_TableFormat.cpp:42
Teuchos::TableFormat::writeRow
void writeRow(std::ostream &out, const Array< RCP< TableEntry > > &entries) const
Write the row of entries.
Definition
Teuchos_TableFormat.cpp:70
Teuchos::TableFormat::thinline
std::string thinline() const
Return a horizontal line in dashes "----" the width of the page.
Definition
Teuchos_TableFormat.cpp:19
Teuchos::TableFormat::writeWholeTable
void writeWholeTable(std::ostream &out, const std::string &tableTitle, const Array< std::string > &columnNames, const Array< TableColumn > &columns) const
Definition
Teuchos_TableFormat.cpp:107
Teuchos::TableFormat::computeRequiredColumnWidth
int computeRequiredColumnWidth(const std::string &name, const TableColumn &column) const
Computes the column width required to write all values to the required precision.
Definition
Teuchos_TableFormat.cpp:53
TEUCHOS_TEST_FOR_EXCEPT
#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.
Definition
Teuchos_TestForException.hpp:285
TEUCHOS_ASSERT_EQUALITY
#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 ...
Definition
Teuchos_Assert.hpp:73
Teuchos
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
Generated by
1.17.0