Skip to content
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
7 changes: 7 additions & 0 deletions llvm/include/llvm/ADT/STLExtras.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <iterator>
#include <limits>
#include <memory>
#include <numeric>
#include <optional>
#include <tuple>
#include <type_traits>
Expand Down Expand Up @@ -1694,6 +1695,12 @@ template <typename R> constexpr size_t range_size(R &&Range) {
return static_cast<size_t>(std::distance(adl_begin(Range), adl_end(Range)));
}

/// Wrapper for std::accumulate.
template <typename R, typename E> auto accumulate(R &&Range, E &&Init) {
return std::accumulate(adl_begin(Range), adl_end(Range),
std::forward<E>(Init));
}

/// Provide wrappers to std::for_each which take ranges instead of having to
/// pass begin/end explicitly.
template <typename R, typename UnaryFunction>
Expand Down
10 changes: 10 additions & 0 deletions llvm/unittests/ADT/STLExtrasTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1602,6 +1602,16 @@ TEST(STLExtrasTest, Fill) {
EXPECT_THAT(V2, ElementsAre(Val, Val, Val, Val));
}

TEST(STLExtrasTest, Accumulate) {
EXPECT_EQ(accumulate(std::vector<int>(), 0), 0);
EXPECT_EQ(accumulate(std::vector<int>(), 3), 3);
std::vector<int> V1 = {1, 2, 3, 4, 5};
EXPECT_EQ(accumulate(V1, 0), std::accumulate(V1.begin(), V1.end(), 0));
EXPECT_EQ(accumulate(V1, 10), std::accumulate(V1.begin(), V1.end(), 10));
EXPECT_EQ(accumulate(drop_begin(V1), 7),
std::accumulate(V1.begin() + 1, V1.end(), 7));
}

struct Foo;
struct Bar {};

Expand Down