Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Change interface to accept raw pointers as iterators
  • Loading branch information
xvallspl committed Apr 6, 2017
commit 7c92d5483618c84f09cf5acef202790492f23fdc
22 changes: 11 additions & 11 deletions math/mathcore/inc/Math/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,21 @@ namespace ROOT {
fSum = t;
}

/// Iterate over an iterable container of values and accumulate on the exising result
template<class Container>
void Add(const Container &elements)
{
static_assert(!std::is_same<decltype(++(elements.begin()), elements.end(), elements.front()), T>::value, "argument is not a container of the same type as the KahanSum class");
for (auto e : elements) this->Add(e);
/// Iterate over a datastructure referenced by a pointer and accumulate on the exising result
template<class Iterator>
void Add(const Iterator begin, const Iterator end)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xvallspl, @lmoneta : is it really better to have begin and end here? Can't we pass something to iterate on? With range based looping this would not imply any code change in the body of the methods.

{
static_assert(!std::is_same<decltype(*begin), T>::value, "Iterator points to an element of the different type than the KahanSum class");
for( auto it = begin; it!=end; it++ ) this->Add(*it);
}

/// Iterate over an iterable and return the result of its accumulation
template<class Container>
static T Accumulate(const Container &elements)
/// Iterate over a datastructure referenced by a pointer and return the result of its accumulation
template<class Iterator>
static T Accumulate(const Iterator begin, const Iterator end)
{
static_assert(!std::is_same<decltype(++(elements.begin()), elements.end(), elements.front()), T>::value, "argument is not a container of the same type as the KahanSum class");
static_assert(!std::is_same<decltype(*begin), T>::value, "Iterator points to an element of the different type than the KahanSum class");
KahanSum init;
init.Add(elements);
init.Add(begin, end);
return init.fSum;
}

Expand Down
4 changes: 2 additions & 2 deletions math/mathcore/test/testKahan.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ int KahanTest()
{
std::vector<double> numbers = {0.01, 0.001, 0.0001, 0.000001, 0.00000000001};
ROOT::Math::KahanSum<double> k;
k.Add(numbers);
auto result = ROOT::Math::KahanSum<double>::Accumulate(numbers);
k.Add(numbers.begin(), numbers.end());
auto result = ROOT::Math::KahanSum<double>::Accumulate(numbers.begin(), numbers.end());
if (k.Result() != result) return 1;
return 0;
}
Expand Down