23#include <ql/errors.hpp>
24#include <ql/math/comparison.hpp>
26#include <boost/make_shared.hpp>
37 Size samples, Size depth,
const T& t)
38 : asof_(
asof), dates_(dates), samples_(samples), depth_(depth) {
39 QL_REQUIRE(
ids.size() > 0,
"SparseNpvCube::SparseNpvCube no ids specified");
40 QL_REQUIRE(
dates.size() > 0,
"SparseNpvCube::SparseNpvCube no dates specified");
41 QL_REQUIRE(
samples > 0,
"SparseNpvCube::SparseNpvCube samples must be > 0");
42 QL_REQUIRE(
depth > 0,
"SparseNpvCube::SparseNpvCube depth must be > 0");
43 Size
check = std::numeric_limits<Size>::max();
47 QL_REQUIRE(
check >= 1,
"SparseNpvCube::SparseNpvCube: total size exceeded: ids ("
48 <<
ids_.size() <<
") * dates (" <<
dates.size() <<
") * depth (" <<
depth <<
") > "
49 << std::numeric_limits<Size>::max());
51 for (
const auto&
id :
ids) {
65 return (i * (numDates() + 1) + j) * depth() + d;
69 this->
check(i, 0, 0, d);
70 auto v = data_.find(pos(i, 0, d));
72 return static_cast<Real
>(v->second[0]);
74 return static_cast<Real
>(0.0);
78 this->
check(i, 0, 0, d);
79 if (QuantLib::close_enough(
value, 0.0))
81 data_[pos(i, 0, d)] = std::vector<T>(1,
static_cast<T
>(
value));
85 this->
check(i, j, k, d);
86 auto v = data_.find(pos(i, j + 1, d));
88 return static_cast<Real
>(v->second[k]);
90 return static_cast<Real
>(0.0);
94 this->
check(i, j, k, d);
95 if (QuantLib::close_enough(
value, 0.0))
97 auto v = data_.find(pos(i, j + 1, d));
98 if (v != data_.end()) {
99 v->second[k] =
static_cast<T
>(
value);
101 std::vector<T> tmp(samples(), 0.0);
102 tmp[k] =
static_cast<T
>(
value);
103 data_[pos(i, j + 1, d)] = tmp;
108 QL_REQUIRE(i < numIds(),
"Out of bounds on ids (i=" << i <<
", numIds=" << numIds() <<
")");
109 QL_REQUIRE(j < numDates(),
"Out of bounds on dates (j=" << j <<
", numDates=" << numDates() <<
")");
110 QL_REQUIRE(k < samples(),
"Out of bounds on samples (k=" << k <<
", samples=" << samples() <<
")");
111 QL_REQUIRE(d < depth(),
"Out of bounds on depth (d=" << d <<
", depth=" << depth() <<
")");
117 const std::vector<Date>& dates, Size samples, Size depth,
const Real& t);
134 const std::vector<Date>& dates, Size samples, Size depth,
const float& t);
const std::set< std::string > ids() const
Get a set of all ids in the cube.
Size pos(Size i, Size j, Size d) const
Date asof() const override
Return the asof date (T0 date)
Real get(Size i, Size j, Size k, Size d) const override
Get a value from the cube using index.
Size numIds() const override
Return the length of each dimension.
Size depth() const override
Real getT0(Size i, Size d) const override
Get a T0 value from the cube using index.
void setT0(Real value, Size i, Size d) override
Set a value in the cube using index.
Size numDates() const override
void set(Real value, Size i, Size j, Size k, Size d) override
Set a value in the cube using index.
Size samples() const override
std::map< std::string, Size > ids_
void check(Size i, Size j, Size k, Size d) const
const std::map< std::string, Size > & idsAndIndexes() const override
Get a map of id and their index position in this cube.
const std::vector< QuantLib::Date > & dates() const override
Get the vector of dates for this cube.
SafeStack< ValueType > value
in memory cube, storing only non-zero entries for (id, date, depth)