Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
Teuchos_TableFormat.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 
43 #include "Teuchos_TableFormat.hpp"
44 #include "Teuchos_Assert.hpp"
45 
46 
47 namespace Teuchos {
48 
49 
50 std::string TableFormat::thinline() const
51 {
52  std::ostringstream toss;
53  for (int i=0; i<pageWidth_; i++)
54  {
55  toss << "-";
56  }
57  return toss.str();
58 }
59 
60 
61 
62 std::string TableFormat::thickline() const
63 {
64  std::ostringstream toss;
65  for (int i=0; i<pageWidth_; i++)
66  {
67  toss << "=";
68  }
69  return toss.str();
70 }
71 
72 
73 std::string TableFormat::blanks(int size) const
74 {
75  std::ostringstream toss;
76  for (int i=0; i<size; i++)
77  {
78  toss << " ";
79  }
80  return toss.str();
81 }
82 
83 
85  const std::string& name,
86  const TableColumn& column
87  ) const
88 {
89  int rtn = name.length();
90 
91  for (int i=0; i<column.numRows(); i++)
92  {
93  int x = column.entry(i)->toString().length();
94  rtn = std::max(rtn, x);
95  }
96 
97  return rtn + columnSpacing_;
98 }
99 
100 
102  std::ostream& out,
103  const Array<RCP<TableEntry> >& entries
104  ) const
105 {
106  TEUCHOS_TEST_FOR_EXCEPT(entries.size() != columnWidths_.size()
107  && columnWidths_.size() != 0);
108 
109  for (Array<RCP<TableEntry> >::size_type i=0; i<entries.size(); i++)
110  {
111  int cw = defaultColumnWidth();
112  if (columnWidths_.size() != 0) cw = columnWidths_[i];
113 
114  out << std::left << std::setw(cw) << entries[i]->toString();
115  }
116  out << std::endl;
117 }
118 
119 
121  std::ostream& out,
122  int rowIndex,
123  const Array<TableColumn>& columns
124  ) const
125 {
126  Array<RCP<TableEntry> > entries(columns.size());
127  for (Array<TableColumn>::size_type i=0; i<columns.size(); i++)
128  {
129  entries[i] = columns[i].entry(rowIndex);
130  }
131 
132  writeRow(out, entries);
133 }
134 
135 
137  std::ostream& out,
138  const std::string& header,
139  const Array<std::string>& columnNames,
140  const Array<TableColumn>& columns
141  ) const
142 {
143 
144  /* compute the total width */
145  int pgWidth = 0;
146  for (Array<TableColumn>::size_type i=0; i<columnNames.size(); i++)
147  {
148  int cw = defaultColumnWidth();
149  if (columnWidths_.size() != 0) cw = columnWidths_[i];
150  pgWidth += cw;
151  }
152  setPageWidth(std::max(pageWidth_, pgWidth));
153 
154  /* write the header */
155  out << thickline() << std::endl;
156  out << std::endl;
157  int numBlanks = (pageWidth_ - header.length())/2;
158  out << blanks(numBlanks) << header << std::endl;
159  out << std::endl;
160 
161  /* write the column titles */
162  for (Array<std::string>::size_type i=0; i<columnNames.size(); i++)
163  {
164  int cw = defaultColumnWidth();
165  if (columnWidths_.size() != 0) cw = columnWidths_[i];
166 
167  out << std::left << std::setw(cw) << columnNames[i];
168  }
169  out << std::endl;
170 
171  /* ensure that all columns have the same number of rows */
172  int numRows = columns[0].numRows();
173  for (Array<TableColumn>::size_type i=1; i<columns.size(); i++)
174  {
175  TEUCHOS_ASSERT_EQUALITY(columns[i].numRows(), numRows);
176  }
177 
178  /* write the table data */
179  for (int i=0; i<numRows; i++)
180  {
181  if (i % lineInterval_ == 0)
182  out << std::left << thinline() << std::endl;
183  writeRow(out, i, columns);
184  }
185 
186  /* write the footer */
187  out << thickline() << std::endl;
188 
189 }
190 
191 
192 } // namespace Teuchos
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. ...
const RCP< TableEntry > & entry(int i) const
std::string toString() const
Convert an Array to an std::string
Provides utilities for formatting tabular output.
std::string blanks(int size) const
Return a std::string full of blanks up to the requested size.
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 thickline() const
Return a thick horizontal line in equal signs "====" the width of the page.
size_type size() const
std::string thinline() const
Return a horizontal line in dashes "----" the width of the page.
Smart reference counting pointer class for automatic garbage collection.
#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 ...
void writeRow(std::ostream &out, const Array< RCP< TableEntry > > &entries) const
Write the row of entries.
#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...
Memory-safe templated array class that encapsulates std::vector.