Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace now obsolete LENGTH macro with std::size function #2077

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions Examples/ConvertibleBonds/ConvertibleBonds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
#include <iostream>
#include <iomanip>

#define LENGTH(a) (sizeof(a)/sizeof(a[0]))

using namespace QuantLib;

int main(int, char* []) {
Expand Down Expand Up @@ -86,15 +84,15 @@ int main(int, char* []) {
Real putPrices[]= { 105.0 };

// Load call schedules
for (Size i=0; i<LENGTH(callLength); i++) {
for (Size i=0; i<std::size(callLength); i++) {
callability.push_back(
ext::make_shared<SoftCallability>(Bond::Price(callPrices[i],
Bond::Price::Clean),
schedule.date(callLength[i]),
1.20));
}

for (Size j=0; j<LENGTH(putLength); j++) {
for (Size j=0; j<std::size(putLength); j++) {
callability.push_back(
ext::make_shared<Callability>(Bond::Price(putPrices[j],
Bond::Price::Clean),
Expand Down
6 changes: 2 additions & 4 deletions Examples/FRA/FRA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@

#include <iostream>

#define LENGTH(a) (sizeof(a)/sizeof(a[0]))

using namespace std;
using namespace QuantLib;

Expand Down Expand Up @@ -171,7 +169,7 @@ int main(int, char* []) {
<< endl;

Size i;
for (i=0; i<LENGTH(monthsToStart); i++) {
for (i=0; i<std::size(monthsToStart); i++) {

Date fraValueDate = fraCalendar.advance(
settlementDate,monthsToStart[i],Months,
Expand Down Expand Up @@ -229,7 +227,7 @@ int main(int, char* []) {
fra9x12Rate->setValue(threeMonthFraQuote[9]);


for (i=0; i<LENGTH(monthsToStart); i++) {
for (i=0; i<std::size(monthsToStart); i++) {

Date fraValueDate = fraCalendar.advance(
settlementDate,monthsToStart[i],Months,
Expand Down
6 changes: 2 additions & 4 deletions Examples/FittedBondCurve/FittedBondCurve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@
#include <iostream>
#include <iomanip>

#define LENGTH(a) (sizeof(a)/sizeof(a[0]))

using namespace std;
using namespace QuantLib;

Expand Down Expand Up @@ -129,7 +127,7 @@ int main(int, char* []) {
std::vector<ext::shared_ptr<BondHelper>> instrumentsA;
std::vector<ext::shared_ptr<RateHelper>> instrumentsB;

for (Size j=0; j<LENGTH(lengths); j++) {
for (Size j=0; j<std::size(lengths); j++) {

Date maturity = calendar.advance(bondSettlementDate, lengths[j]*Years);

Expand Down Expand Up @@ -588,7 +586,7 @@ int main(int, char* []) {
<< endl
<< endl;

for (Size k=0; k<LENGTH(lengths)-1; k++) {
for (Size k=0; k<std::size(lengths)-1; k++) {

Real P = instrumentsA[k]->quote()->value();
const Bond& b = *instrumentsA[k]->bond();
Expand Down
6 changes: 3 additions & 3 deletions test-suite/americanoption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,7 @@ BOOST_AUTO_TEST_CASE(testQdAmericanEngines) {
17.05087,26.89157,64.64923,0,390.87453,124.55406,0.01018,94.23963};

std::vector<OptionSpec> testCaseSpecs;
testCaseSpecs.reserve(LENGTH(pde_values) + LENGTH(edgeTestCases));
testCaseSpecs.reserve(std::size(pde_values) + std::size(edgeTestCases));

PseudoRandom::rng_type rng(PseudoRandom::urng_type(12345UL));

Expand Down Expand Up @@ -1467,7 +1467,7 @@ BOOST_AUTO_TEST_CASE(testAndersenLakeHighPrecisionExample) {
QdFpAmericanEngine::FP_A, QdFpAmericanEngine::FP_B
};

for (Size i=0; i < LENGTH(schemes); ++i) {
for (Size i=0; i < std::size(schemes); ++i) {

americanOption.setPricingEngine(
ext::make_shared<QdFpAmericanEngine>(
Expand Down Expand Up @@ -1533,7 +1533,7 @@ BOOST_AUTO_TEST_CASE(testQdEngineStandardExample) {
};
const Real expected[] = { 0.2386475283369327, 0.2386596962737606 };

for (Size i=0; i < LENGTH(schemes); ++i) {
for (Size i=0; i < std::size(schemes); ++i) {
americanOption.setPricingEngine(
ext::make_shared<QdFpAmericanEngine>(
bsProcess,
Expand Down
2 changes: 1 addition & 1 deletion test-suite/amortizingbond.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ BOOST_AUTO_TEST_CASE(testAmortizingFixedRateBond) {

const Real tolerance = 1.0e-6;

for (Size i=0; i<LENGTH(rates); ++i) {
for (Size i=0; i<std::size(rates); ++i) {

auto schedule = sinkingSchedule(refDate, Period(30, Years), freq, NullCalendar());
auto notionals = sinkingNotionals(Period(30, Years), freq, rates[i], 100.0);
Expand Down
10 changes: 5 additions & 5 deletions test-suite/andreasenhugevolatilityinterpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ CalibrationData AndreasenHugeExampleData() {
const Handle<YieldTermStructure> rTS(flatRate(today, 0.0, dc));
const Handle<YieldTermStructure> qTS(flatRate(today, 0.0, dc));

const Size nStrikes = LENGTH(raw);
const Size nMaturities = LENGTH(maturityTimes);
const Size nStrikes = std::size(raw);
const Size nMaturities = std::size(maturityTimes);

static_assert(nMaturities == LENGTH(raw[1])-1, "check raw data");
static_assert(nMaturities == std::size(raw[1])-1, "check raw data");

AndreasenHugeVolatilityInterpl::CalibrationSet calibrationSet;

Expand All @@ -127,7 +127,7 @@ CalibrationData AndreasenHugeExampleData() {
for (const auto & i : raw) {
const Real strike = spot->value()*i[0];

for (Size j=1; j < LENGTH(i); ++j) {
for (Size j=1; j < std::size(i); ++j) {
if (i[j] > QL_EPSILON) {
const Date maturity
= today + Period(Size(365*maturityTimes[j-1]), Days);
Expand Down Expand Up @@ -328,7 +328,7 @@ CalibrationData arbitrageData() {
const Volatility vols[] = { 0.25, 0.35, 0.05, 0.35 };
AndreasenHugeVolatilityInterpl::CalibrationSet calibrationSet;

for (Size i=0; i < LENGTH(strikes); ++i) {
for (Size i=0; i < std::size(strikes); ++i) {
const Real strike = strikes[i];
const Date maturityDate = today + Period(maturities[i], Months);
const Volatility vol = vols[i];
Expand Down
14 changes: 7 additions & 7 deletions test-suite/asianoptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ void testDiscreteGeometricAveragePriceHeston(const ext::shared_ptr<PricingEngine
Real runningAccumulator = 1.0;
Size pastFixings = 0;

for (Size i=0; i<LENGTH(strikes); i++) {
for (Size i=0; i<std::size(strikes); i++) {
Real strike = strikes[i];
int day = days[i];
Real expected = prices[i];
Expand Down Expand Up @@ -986,7 +986,7 @@ BOOST_AUTO_TEST_CASE(testMCDiscreteArithmeticAveragePriceHeston, *precondition(i
ext::shared_ptr<Exercise> exercise(new
EuropeanExercise(fixingDates[119]));

for (Size i=0; i<LENGTH(prices); i++) {
for (Size i=0; i<std::size(prices); i++) {
Real strike = strikes[i];
Real expected = prices[i];

Expand Down Expand Up @@ -2087,7 +2087,7 @@ BOOST_AUTO_TEST_CASE(testAnalyticContinuousGeometricAveragePriceHeston) {
ext::shared_ptr<AnalyticContinuousGeometricAveragePriceAsianHestonEngine> engine(new
AnalyticContinuousGeometricAveragePriceAsianHestonEngine(hestonProcess));

for (Size i=0; i<LENGTH(strikes); i++) {
for (Size i=0; i<std::size(strikes); i++) {
Real strike = strikes[i];
Time day = days[i];
Real expected = prices[i];
Expand Down Expand Up @@ -2123,7 +2123,7 @@ BOOST_AUTO_TEST_CASE(testAnalyticContinuousGeometricAveragePriceHeston) {
ext::shared_ptr<AnalyticContinuousGeometricAveragePriceAsianHestonEngine> engine_2(new
AnalyticContinuousGeometricAveragePriceAsianHestonEngine(hestonProcess_2));

for (Size i=0; i<LENGTH(strikes); i++) {
for (Size i=0; i<std::size(strikes); i++) {
Real strike = strikes[i];
Time day = days[i];
Real expected = prices_2[i];
Expand Down Expand Up @@ -2182,7 +2182,7 @@ BOOST_AUTO_TEST_CASE(testAnalyticContinuousGeometricAveragePriceHeston) {
ext::shared_ptr<AnalyticContinuousGeometricAveragePriceAsianHestonEngine> engine_3(new
AnalyticContinuousGeometricAveragePriceAsianHestonEngine(hestonProcess_3));

for (Size i=0; i<LENGTH(strikes_3); i++) {
for (Size i=0; i<std::size(strikes_3); i++) {
Real strike = strikes_3[i];
Time day = days_3[i];
Real expected = prices_3[i];
Expand Down Expand Up @@ -2346,9 +2346,9 @@ BOOST_AUTO_TEST_CASE(testDiscreteGeometricAveragePriceHestonPastFixings) {
Option::Type type(Option::Call);
Average::Type averageType = Average::Geometric;

for (Size strike_index = 0; strike_index < LENGTH(strikes); strike_index++) {
for (Size strike_index = 0; strike_index < std::size(strikes); strike_index++) {

for (Size day_index = 0; day_index < LENGTH(days); day_index++) {
for (Size day_index = 0; day_index < std::size(days); day_index++) {

for (Size k=0; k<2; k++) {

Expand Down
10 changes: 5 additions & 5 deletions test-suite/barrieroption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -915,8 +915,8 @@ BOOST_AUTO_TEST_CASE(testDividendBarrierOption) {
};

Real relTol = 2e-4;
for (Size i=0; i < LENGTH(barriers); ++i) {
for (Size j=0; j < LENGTH(engines); ++j) {
for (Size i=0; i < std::size(barriers); ++i) {
for (Size j=0; j < std::size(engines); ++j) {
Real barrier = barriers[i];
Barrier::Type barrierType = barrierTypes[i];

Expand Down Expand Up @@ -995,8 +995,8 @@ BOOST_AUTO_TEST_CASE(testDividendBarrierOptionWithDividendsPastMaturity) {
auto payoff = ext::make_shared<PlainVanillaPayoff>(Option::Put, strike);
auto exercise = ext::make_shared<EuropeanExercise>(maturity);

for (Size i=0; i < LENGTH(barriers); ++i) {
for (Size j=0; j < LENGTH(engines); ++j) {
for (Size i=0; i < std::size(barriers); ++i) {
for (Size j=0; j < std::size(engines); ++j) {
Real barrier = barriers[i];
Barrier::Type barrierType = barrierTypes[i];

Expand Down Expand Up @@ -1062,7 +1062,7 @@ BOOST_AUTO_TEST_CASE(testImpliedVolatility) {
Real targetWithDividends[] = { 8.0, 12.0, 9.0, 8.0 };
Real tolerance = 1e-5;

for (Size i=0; i < LENGTH(barriers); ++i) {
for (Size i=0; i < std::size(barriers); ++i) {
Real barrier = barriers[i];
Barrier::Type barrierType = barrierTypes[i];

Expand Down
4 changes: 2 additions & 2 deletions test-suite/blackdeltacalculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ BOOST_AUTO_TEST_CASE(testDeltaValues){
Real error;
Real tolerance;

for (Size i=0; i<LENGTH(values); i++) {
for (Size i=0; i<std::size(values); i++) {

currOt =values[i].ot;
currDt =values[i].dt;
Expand Down Expand Up @@ -550,7 +550,7 @@ BOOST_AUTO_TEST_CASE(testAtmCalcs){
Real currPutDelta;
Real currFwd;

for (Size i=0; i<LENGTH(values); i++) {
for (Size i=0; i<std::size(values); i++) {

currDt =values[i].dt;
currSpot =values[i].spot;
Expand Down
6 changes: 3 additions & 3 deletions test-suite/cdo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,14 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(testHW, T, dataSets) {

ext::shared_ptr<SimpleQuote> correlation(new SimpleQuote(0.0));
Handle<Quote> hCorrelation(correlation);
QL_REQUIRE(LENGTH(hwAttachment) == LENGTH(hwDetachment), "data length does not match");
QL_REQUIRE(std::size(hwAttachment) == std::size(hwDetachment), "data length does not match");

ext::shared_ptr<PricingEngine> midPCDOEngine(new MidPointCDOEngine(yieldHandle));
ext::shared_ptr<PricingEngine> integralCDOEngine(new IntegralCDOEngine(yieldHandle));

const Size i = dataSet;
correlation->setValue(hwData7[i].correlation);
QL_REQUIRE(LENGTH(hwAttachment) == LENGTH(hwData7[i].trancheSpread),
QL_REQUIRE(std::size(hwAttachment) == std::size(hwData7[i].trancheSpread),
"data length does not match");
std::vector<ext::shared_ptr<DefaultLossModel>> basketModels;
std::vector<std::string> modelNames;
Expand Down Expand Up @@ -319,7 +319,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(testHW, T, dataSets) {
return;
}

for (Size j = 0; j < LENGTH(hwAttachment); j++) {
for (Size j = 0; j < std::size(hwAttachment); j++) {
ext::shared_ptr<Basket> basketPtr(
new Basket(asofDate, names, nominals, pool, hwAttachment[j], hwDetachment[j]));
std::ostringstream trancheId;
Expand Down
2 changes: 1 addition & 1 deletion test-suite/digitalcoupon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ BOOST_AUTO_TEST_CASE(testAssetOrNothing) {
Date endDate = vars.calendar.advance(vars.settlement,(k+2)*Years);
Rate nullstrike = Null<Rate>();
Date paymentDate = endDate;
for (Size h=0; h<LENGTH(gearings); h++) {
for (Size h=0; h<std::size(gearings); h++) {

Real gearing = gearings[h];
Rate spread = spreads[h];
Expand Down
2 changes: 1 addition & 1 deletion test-suite/digitaloption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ BOOST_AUTO_TEST_CASE(testCashAtHitOrNothingAmericanGreeks) {
ext::shared_ptr<PricingEngine> engines[] = { euroEngine, amEngine };

bool knockin=true;
for (Size j=0; j<LENGTH(engines); j++) {
for (Size j=0; j<std::size(engines); j++) {
for (auto& type : types) {
for (Real strike : strikes) {
ext::shared_ptr<StrikedTypePayoff> payoff(
Expand Down
12 changes: 6 additions & 6 deletions test-suite/distributions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ void checkBivariate(const char* tag) {
{ -30, 1.0, 1.0, 0.000000 }
};

for (Size i=0; i<LENGTH(values); i++) {
for (Size i=0; i<std::size(values); i++) {
Bivariate bcd(values[i].rho);
Real value = bcd(values[i].a, values[i].b);

Expand Down Expand Up @@ -425,7 +425,7 @@ BOOST_AUTO_TEST_CASE(testInverseCumulativePoisson) {
0.99999999
};

for (Size i=0; i<LENGTH(data); i++) {
for (Size i=0; i<std::size(data); i++) {
if (!close(icp(data[i]), static_cast<Real>(i))) {
BOOST_ERROR(std::setprecision(8)
<< "failed to reproduce known value for x = "
Expand Down Expand Up @@ -490,14 +490,14 @@ BOOST_AUTO_TEST_CASE(testBivariateCumulativeStudent) {
};

Real tolerance = 1.0e-5;
for (Size i=0; i < LENGTH(ns); ++i) {
for (Size i=0; i < std::size(ns); ++i) {
BivariateCumulativeStudentDistribution f1(ns[i], 0.5);
BivariateCumulativeStudentDistribution f2(ns[i], -0.5);
for (Size j=0; j < LENGTH(xs); ++j) {
for (Size j=0; j < std::size(xs); ++j) {
Real calculated1 = f1(xs[j], xs[j]);
Real reference1 = expected1[i*LENGTH(xs)+j];
Real reference1 = expected1[i*std::size(xs)+j];
Real calculated2 = f2(xs[j], xs[j]);
Real reference2 = expected2[i*LENGTH(xs)+j];
Real reference2 = expected2[i*std::size(xs)+j];
if (std::fabs(calculated1 - reference1) > tolerance)
BOOST_ERROR("Failed to reproduce CDF value at " << xs[j] <<
"\n calculated: " << calculated1 <<
Expand Down
2 changes: 1 addition & 1 deletion test-suite/fdheston.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ BOOST_AUTO_TEST_CASE(testFdmHestonIkonenToivanen) {
Real expected[] = { 2.00000, 1.10763, 0.520038, 0.213681, 0.082046 };
const Real tol = 0.001;

for (Size i=0; i < LENGTH(strikes); ++i) {
for (Size i=0; i < std::size(strikes); ++i) {
Handle<Quote> s0(ext::shared_ptr<Quote>(new SimpleQuote(strikes[i])));
ext::shared_ptr<HestonProcess> hestonProcess(
new HestonProcess(rTS, qTS, s0, 0.0625, 5, 0.16, 0.9, 0.1));
Expand Down
8 changes: 4 additions & 4 deletions test-suite/fdsabr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ BOOST_AUTO_TEST_CASE(testOosterleeTestCaseIV) {
const Real strikes[] = { 0.4*f0, f0, 1.6*f0 };

const Real tol = 0.00035;
for (Size i=0; i < LENGTH(maturities); ++i) {
for (Size i=0; i < std::size(maturities); ++i) {
const Date maturityDate = today + maturities[i];
const Time maturityTime = dc.yearFraction(today, maturityDate);

Expand All @@ -393,7 +393,7 @@ BOOST_AUTO_TEST_CASE(testOosterleeTestCaseIV) {
const ext::shared_ptr<Exercise> exercise =
ext::make_shared<EuropeanExercise>(maturityDate);

for (Size j=0; j < LENGTH(strikes); ++j) {
for (Size j=0; j < std::size(strikes); ++j) {
const ext::shared_ptr<StrikedTypePayoff> payoff =
ext::make_shared<PlainVanillaPayoff>(Option::Call, strikes[j]);

Expand Down Expand Up @@ -462,7 +462,7 @@ BOOST_AUTO_TEST_CASE(testBenchOpSabrCase) {

const Real tol = 2e-4;

for (Size i=0; i < LENGTH(f0s); ++i) {
for (Size i=0; i < std::size(f0s); ++i) {

const Date maturity = today + Period(maturityInYears[i]*365, Days);
const Time T = dc.yearFraction(today, maturity);
Expand All @@ -477,7 +477,7 @@ BOOST_AUTO_TEST_CASE(testBenchOpSabrCase) {
f0*std::exp(-0.1*std::sqrt(T)), f0, f0*std::exp(0.1*std::sqrt(T))
};

for (Size j=0; j < LENGTH(strikes); ++j) {
for (Size j=0; j < std::size(strikes); ++j) {
const Real strike = strikes[j];

VanillaOption option(
Expand Down
Loading
Loading