Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
inmemoryreport.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
20
21#include <boost/algorithm/string/join.hpp>
22
23namespace ore {
24namespace data {
25
26Report& InMemoryReport::addColumn(const string& name, const ReportType& rt, Size precision) {
27 headers_.push_back(name);
28 columnTypes_.push_back(rt);
29 columnPrecision_.push_back(precision);
30 data_.push_back(vector<ReportType>()); // Initialise vector for
31 i_++;
32 return *this;
33}
34
36 QL_REQUIRE(i_ == headers_.size(), "Cannot go to next line, only " << i_ << " entires filled, report headers are: "
37 << boost::join(headers_, ","));
38 i_ = 0;
39 return *this;
40}
41
43 // check type is valid
44 QL_REQUIRE(i_ < headers_.size(), "No column to add [" << rt << "] to.");
45 QL_REQUIRE(rt.which() == columnTypes_[i_].which(), "Cannot add value "
46 << rt << " of type " << rt.which() << " to column "
47 << headers_[i_] << " of type " << columnTypes_[i_].which()
48 << ", report headers are: " << boost::join(headers_, ","));
49
50 data_[i_].push_back(rt);
51 i_++;
52 return *this;
53}
54
56 QL_REQUIRE(columns() == report.columns(), "Cannot combine reports of different sizes ("
57 << columns() << " vs " << report.columns()
58 << "), report headers are: " << boost::join(headers_, ","));
59 end();
60 for (Size i = 0; i < columns(); i++) {
61 string h1 = headers_[i];
62 string h2 = report.header(i);
63 QL_REQUIRE(h1 == h2, "Cannot combine reports with different headers (\""
64 << h1 << "\" and \"" << h2
65 << "\"), report headers are: " << boost::join(headers_, ","));
66 }
67
68 if (i_ == headers_.size())
69 next();
70
71 for (Size rowIdx = 0; rowIdx < report.rows(); rowIdx++) {
72 for (Size columnIdx = 0; columnIdx < report.columns(); columnIdx++) {
73 add(report.data(columnIdx)[rowIdx]);
74 }
75 next();
76 }
77
78 return *this;
79}
80
82 QL_REQUIRE(i_ == headers_.size() || i_ == 0, "report is finalized with incomplete row, got data for "
83 << i_ << " columns out of " << columns()
84 << ", report headers are: " << boost::join(headers_, ","));
85}
86
87const vector<Report::ReportType>& InMemoryReport::data(Size i) const {
88 QL_REQUIRE(data_[i].size() == rows(), "internal error: report column "
89 << i << " (" << header(i) << ") contains " << data_[i].size()
90 << " rows, expected are " << rows()
91 << " rows, report headers are: " << boost::join(headers_, ","));
92 return data_[i];
93}
94
95void InMemoryReport::toFile(const string& filename, const char sep, const bool commentCharacter, char quoteChar,
96 const string& nullString, bool lowerHeader) {
97
98 CSVFileReport cReport(filename, sep, commentCharacter, quoteChar, nullString, lowerHeader);
99
100 for (Size i = 0; i < headers_.size(); i++) {
102 }
103
104 auto numColumns = columns();
105 if (numColumns > 0) {
106 auto numRows = data_[0].size();
107
108 for (Size i = 0; i < numRows; i++) {
109 cReport.next();
110 for (Size j = 0; j < numColumns; j++) {
111 cReport.add(data_[j][i]);
112 }
113 }
114 }
115
116 cReport.end();
117}
118
119} // namespace data
120} // namespace ore
Report & add(const ReportType &rt) override
Definition: csvreport.cpp:175
void end() override
Definition: csvreport.cpp:191
Report & next() override
Definition: csvreport.cpp:153
Report & addColumn(const string &name, const ReportType &rt, Size precision=0) override
Definition: csvreport.cpp:136
vector< Size > columnPrecision_
Report & add(const ReportType &rt) override
vector< ReportType > columnTypes_
const string & header(Size i) const
const vector< ReportType > & data(Size i) const
Returns the data.
vector< vector< ReportType > > data_
Report & next() override
Report & addColumn(const string &name, const ReportType &rt, Size precision=0) override
void toFile(const string &filename, const char sep=',', const bool commentCharacter=true, char quoteChar='\0', const string &nullString="#N/A", bool lowerHeader=false)
boost::variant< Size, Real, string, Date, Period > ReportType
Definition: report.hpp:66
In memory report class.
@ data
Definition: log.hpp:77
Size size(const ValueType &v)
Definition: value.cpp:145
Serializable Credit Default Swap.
Definition: namespaces.docs:23
string name