|
| 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 | + |
| 19 | + |
| 20 | +#include <ored/report/utilities.hpp> |
| 21 | + |
| 22 | +namespace ore { |
| 23 | +namespace data { |
| 24 | + |
| 25 | +//! Adds a column to an existing InMemoryReport, the column value will be set to *value* for all existing rows |
| 26 | +//! Caution: This copies all existing values of the report and create a new one |
| 27 | +// TODO: Improve the report class to allow to add new columns to avoid copies |
| 28 | +QuantLib::ext::shared_ptr<ore::data::InMemoryReport> |
| 29 | +addColumnToExisitingReport(const std::string& columnName, const std::string& value, |
| 30 | + const QuantLib::ext::shared_ptr<ore::data::InMemoryReport>& report) { |
| 31 | + QuantLib::ext::shared_ptr<ore::data::InMemoryReport> newReport = |
| 32 | + QuantLib::ext::make_shared<ore::data::InMemoryReport>(); |
| 33 | + if (report != nullptr) { |
| 34 | + newReport->addColumn(columnName, string()); |
| 35 | + for (size_t i = 0; i < report->columns(); i++) { |
| 36 | + newReport->addColumn(report->header(i), report->columnType(i), report->columnPrecision(i)); |
| 37 | + } |
| 38 | + for (size_t row = 0; row < report->rows(); row++) { |
| 39 | + newReport->next(); |
| 40 | + newReport->add(value); |
| 41 | + for (size_t col = 0; col < report->columns(); col++) { |
| 42 | + newReport->add(report->data(col)[row]); |
| 43 | + } |
| 44 | + } |
| 45 | + newReport->end(); |
| 46 | + } |
| 47 | + return newReport; |
| 48 | +} |
| 49 | + |
| 50 | +QuantLib::ext::shared_ptr<ore::data::InMemoryReport> |
| 51 | +concatenateReports(const std::vector<QuantLib::ext::shared_ptr<ore::data::InMemoryReport>>& reports) { |
| 52 | + if (!reports.empty() && reports.front() != nullptr) { |
| 53 | + auto firstReport = reports.front(); |
| 54 | + QuantLib::ext::shared_ptr<InMemoryReport> concatenatedReport = |
| 55 | + QuantLib::ext::make_shared<InMemoryReport>(*firstReport); |
| 56 | + for (size_t i = 1; i < reports.size(); i++) { |
| 57 | + if (reports[i] != nullptr) { |
| 58 | + concatenatedReport->add(*reports[i]); |
| 59 | + } |
| 60 | + } |
| 61 | + return concatenatedReport; |
| 62 | + } |
| 63 | + return nullptr; |
| 64 | +} |
| 65 | +} // namespace data |
| 66 | +} // namespace ore |
0 commit comments