Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 #include <iostream>
43 
44 #include "Teuchos_TableFormat.hpp"
45 #include "Teuchos_Assert.hpp"
46 
47 
48 namespace Teuchos {
49 
50 
51 std::string TableFormat::thinline() const
52 {
53  std::ostringstream toss;
54  for (int i=0; i<pageWidth_; i++)
55  {
56  toss << "-";
57  }
58  return toss.str();
59 }
60 
61 
62 
63 std::string TableFormat::thickline() const
64 {
65  std::ostringstream toss;
66  for (int i=0; i<pageWidth_; i++)
67  {
68  toss << "=";
69  }
70  return toss.str();
71 }
72 
73 
74 std::string TableFormat::blanks(int size) const
75 {
76  std::ostringstream toss;
77  for (int i=0; i<size; i++)
78  {
79  toss << " ";
80  }
81  return toss.str();
82 }
83 
84 
86  const std::string& name,
87  const TableColumn& column
88  ) const
89 {
90  int rtn = name.length();
91 
92  for (int i=0; i<column.numRows(); i++)
93  {
94  int x = column.entry(i)->toString().length();
95  rtn = std::max(rtn, x);
96  }
97 
98  return rtn + columnSpacing_;
99 }
100 
101 
103  std::ostream& out,
104  const Array<RCP<TableEntry> >& entries
105  ) const
106 {
107  TEUCHOS_TEST_FOR_EXCEPT(entries.size() != columnWidths_.size()
108  && columnWidths_.size() != 0);
109 
110  std::ios::fmtflags f( out.flags() );
111  for (Array<RCP<TableEntry> >::size_type i=0; i<entries.size(); i++)
112  {
113  int cw = defaultColumnWidth();
114  if (columnWidths_.size() != 0) cw = columnWidths_[i];
115 
116  out << std::left << std::setw(cw) << entries[i]->toString();
117  }
118  out << std::endl;
119  out.flags(f);
120 }
121 
122 
124  std::ostream& out,
125  int rowIndex,
126  const Array<TableColumn>& columns
127  ) const
128 {
129  Array<RCP<TableEntry> > entries(columns.size());
130  for (Array<TableColumn>::size_type i=0; i<columns.size(); i++)
131  {
132  entries[i] = columns[i].entry(rowIndex);
133  }
134 
135  writeRow(out, entries);
136 }
137 
138 
140  std::ostream& out,
141  const std::string& header,
142  const Array<std::string>& columnNames,
143  const Array<TableColumn>& columns
144  ) const
145 {
146  std::ios::fmtflags f(out.flags());
147 
148  /* compute the total width */
149  int pgWidth = 0;
150  for (Array<TableColumn>::size_type i=0; i<columnNames.size(); i++)
151  {
152  int cw = defaultColumnWidth();
153  if (columnWidths_.size() != 0) cw = columnWidths_[i];
154  pgWidth += cw;
155  }
156  setPageWidth(std::max(pageWidth_, pgWidth));
157 
158  /* write the header */
159  out << thickline() << std::endl;
160  out << std::endl;
161  int numBlanks = (pageWidth_ - header.length())/2;
162  out << blanks(numBlanks) << header << std::endl;
163  out << std::endl;
164 
165  /* write the column titles */
166  for (Array<std::string>::size_type i=0; i<columnNames.size(); i++)
167  {
168  int cw = defaultColumnWidth();
169  if (columnWidths_.size() != 0) cw = columnWidths_[i];
170 
171  out << std::left << std::setw(cw) << columnNames[i];
172  }
173  out << std::endl;
174 
175  /* ensure that all columns have the same number of rows */
176  int numRows = columns[0].numRows();
177  for (Array<TableColumn>::size_type i=1; i<columns.size(); i++)
178  {
179  TEUCHOS_ASSERT_EQUALITY(columns[i].numRows(), numRows);
180  }
181 
182  /* write the table data */
183  for (int i=0; i<numRows; i++)
184  {
185  if (i % lineInterval_ == 0)
186  out << std::left << thinline() << std::endl;
187  writeRow(out, i, columns);
188  }
189 
190  /* write the footer */
191  out << thickline() << std::endl;
192 
193  // Restore flags
194  out.flags(f);
195 }
196 
197 
198 } // 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
Ordinal size_type
The type of Array sizes and capacities.
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 &quot;====&quot; the width of the page.
size_type size() const
std::string thinline() const
Return a horizontal line in dashes &quot;----&quot; 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...
Replacement for std::vector that is compatible with the Teuchos Memory Management classes...