Skip to content

Commit c9c3238

Browse files
committed
Merge branch 'combinators' into 'main'
WIF - Combinators - Throw std::runtime_error in combine() when the input vector is empty See merge request feta/wif-group/libwif!47
2 parents 59c79e1 + 1e3650a commit c9c3238

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

src/wif/combinators/averageCombinator.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@
1010
#include "wif/combinators/averageCombinator.hpp"
1111

1212
#include <numeric>
13+
#include <stdexcept>
1314

1415
namespace WIF {
1516

1617
double AverageCombinator::combine(const std::vector<double>& valuesToCombine)
1718
{
19+
if (valuesToCombine.empty()) {
20+
throw std::runtime_error("Nothing to combine! The supplied vector is empty.");
21+
}
22+
1823
double valuesSum = std::accumulate(valuesToCombine.begin(), valuesToCombine.end(), 0.0);
19-
return valuesToCombine.size() ? valuesSum / valuesToCombine.size() : 0;
24+
return valuesSum / valuesToCombine.size();
2025
}
2126

2227
} // namespace WIF

src/wif/combinators/binaryDSTCombinator.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88

99
#include "wif/combinators/binaryDSTCombinator.hpp"
1010

11+
#include <stdexcept>
12+
1113
namespace WIF {
1214

1315
double BinaryDSTCombinator::combine(const std::vector<double>& valuesToCombine)
1416
{
15-
if (valuesToCombine.size() == 0) {
16-
return 0.0;
17+
if (valuesToCombine.empty()) {
18+
throw std::runtime_error("Nothing to combine! The supplied vector is empty.");
1719
}
1820

1921
constexpr size_t POSITIVE_HYPOTHESIS = 0xAA;

src/wif/combinators/sumCombinator.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@
1010
#include "wif/combinators/sumCombinator.hpp"
1111

1212
#include <numeric>
13+
#include <stdexcept>
1314

1415
namespace WIF {
1516

1617
double SumCombinator::combine(const std::vector<double>& valuesToCombine)
1718
{
19+
if (valuesToCombine.empty()) {
20+
throw std::runtime_error("Nothing to combine! The supplied vector is empty.");
21+
}
22+
1823
return std::accumulate(valuesToCombine.begin(), valuesToCombine.end(), 0.0);
1924
}
2025

0 commit comments

Comments
 (0)