Skip to content

Commit

Permalink
chore: Improve sum_each template
Browse files Browse the repository at this point in the history
  • Loading branch information
oboukli committed Jan 26, 2024
1 parent f24f964 commit fadc796
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 31 deletions.
11 changes: 2 additions & 9 deletions benchmark/sub_array_sums_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,13 @@ TEST_CASE("sub_array_sums benchmarking", "[benchmark][sub_array_sums]")
.title("Sum of all subarrays of size K")

.run(
NAMEOF_RAW(
// clang-format off
sum_each<std::array<int, 6>,
std::array<int, 4>,
std::array<int, 6>::size_type>)
// clang-format on
NAMEOF_RAW(sum_each<std::array<int, 6>, std::array<int, 4>>)
.c_str(),
[]() {
static constexpr std::array const numbers{1, 1, 1, 2, 2, 2};
static std::array<int, 4> sums{};
static constexpr std::array<int, 6>::size_type const sub_size{
3};

sum_each(numbers, sums, sub_size);
sum_each(numbers, sums, 3);

ankerl::nanobench::doNotOptimizeAway(sums);
})
Expand Down
16 changes: 10 additions & 6 deletions include/forfun/sub_array_sums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@
#define FORFUN_SUB_ARRAY_SUMS_HPP_

#include <algorithm>
#include <iterator>

namespace forfun::sub_array_sums {

template <
typename T,
typename TSum,
typename S = T::size_type,
typename V = T::value_type>
void sum_each(T const& numbers, TSum& sums, S const sub_size) noexcept
template <typename T, typename TSum>
requires(
std::contiguous_iterator<typename T::iterator>
and std::contiguous_iterator<typename TSum::iterator>)
void sum_each(
T const& numbers, TSum& sums, typename T::size_type const sub_size) noexcept
{
using S = T::size_type;
using V = T::value_type;

auto const nums_size{numbers.size()};
V sub_sum{};

Expand Down
32 changes: 16 additions & 16 deletions test/sub_array_sums_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ TEST_CASE("sub_array_sums", "[sub_array_sums]")
{
std::vector<int> const numbers{};
std::vector<int> sums(1);
constexpr std::vector<int>::size_type const sub_size{0};
std::vector const expected{0};
static constexpr auto const sub_size{0};

CAPTURE(numbers);
CAPTURE(sums.size());
Expand All @@ -34,8 +34,8 @@ TEST_CASE("sub_array_sums", "[sub_array_sums]")
{
std::vector<int> const numbers{};
std::vector<int> sums{};
constexpr std::vector<int>::size_type const sub_size{0};
std::vector<int> const expected{};
static constexpr auto const sub_size{0};

CAPTURE(numbers);
CAPTURE(sums.size());
Expand All @@ -49,8 +49,8 @@ TEST_CASE("sub_array_sums", "[sub_array_sums]")
{
std::vector const numbers{5};
std::vector<int> sums(1);
constexpr std::vector<int>::size_type const sub_size{0};
std::vector const expected{0};
static constexpr auto const sub_size{0};

CAPTURE(numbers);
CAPTURE(sums.size());
Expand All @@ -64,8 +64,8 @@ TEST_CASE("sub_array_sums", "[sub_array_sums]")
{
std::vector const numbers{{7, 11}};
std::vector<int> sums(1);
constexpr std::vector<int>::size_type const sub_size{0};
std::vector const expected{0};
static constexpr auto const sub_size{0};

CAPTURE(numbers);
CAPTURE(sums.size());
Expand All @@ -79,8 +79,8 @@ TEST_CASE("sub_array_sums", "[sub_array_sums]")
{
std::vector const numbers{13};
std::vector<int> sums(1);
constexpr std::vector<int>::size_type const sub_size{1};
std::vector const expected{13};
static constexpr auto const sub_size{1};

CAPTURE(numbers);
CAPTURE(sums.size());
Expand All @@ -94,8 +94,8 @@ TEST_CASE("sub_array_sums", "[sub_array_sums]")
{
std::vector const numbers{{2, 3}};
std::vector<int> sums(1);
constexpr std::vector<int>::size_type const sub_size{2};
std::vector const expected{5};
static constexpr auto const sub_size{2};

CAPTURE(numbers);
CAPTURE(sums.size());
Expand All @@ -109,8 +109,8 @@ TEST_CASE("sub_array_sums", "[sub_array_sums]")
{
std::vector const numbers{{3, 11, 17}};
std::vector<int> sums(1);
constexpr std::vector<int>::size_type const sub_size{3};
std::vector const expected{31};
static constexpr auto const sub_size{3};

CAPTURE(numbers);
CAPTURE(sums.size());
Expand All @@ -124,8 +124,8 @@ TEST_CASE("sub_array_sums", "[sub_array_sums]")
{
std::vector const numbers{{19, 23}};
std::vector<int> sums(2);
constexpr std::vector<int>::size_type const sub_size{1};
std::vector const expected{{19, 23}};
static constexpr auto const sub_size{1};

CAPTURE(numbers);
CAPTURE(sums.size());
Expand All @@ -139,8 +139,8 @@ TEST_CASE("sub_array_sums", "[sub_array_sums]")
{
std::vector const numbers{{29, 31, 37}};
std::vector<int> sums(3);
constexpr std::vector<int>::size_type const sub_size{1};
std::vector const expected{{29, 31, 37}};
static constexpr auto const sub_size{1};

CAPTURE(numbers);
CAPTURE(sums.size());
Expand All @@ -157,8 +157,8 @@ TEST_CASE("sub_array_sums", "[sub_array_sums]")
{
std::vector const numbers{{1, 2, 3}};
std::vector<int> sums(1);
constexpr std::vector<int>::size_type const sub_size{101};
std::vector const expected{6};
static constexpr auto const sub_size{101};

CAPTURE(numbers);
CAPTURE(sums.size());
Expand All @@ -172,8 +172,8 @@ TEST_CASE("sub_array_sums", "[sub_array_sums]")
{
std::vector const numbers{{1, 2, 2, 1, 1, 2}};
std::vector<int> sums(5);
constexpr std::vector<int>::size_type const sub_size{2};
std::vector const expected{{3, 4, 3, 2, 3}};
static constexpr auto const sub_size{2};

CAPTURE(numbers);
CAPTURE(sums.size());
Expand All @@ -187,8 +187,8 @@ TEST_CASE("sub_array_sums", "[sub_array_sums]")
{
std::vector const numbers{{1, 1, 1, 2, 2, 2}};
std::vector<int> sums(4);
constexpr std::vector<int>::size_type const sub_size{3};
std::vector const expected{{3, 4, 5, 6}};
static constexpr auto const sub_size{3};

CAPTURE(numbers);
CAPTURE(sums.size());
Expand All @@ -202,8 +202,8 @@ TEST_CASE("sub_array_sums", "[sub_array_sums]")
{
std::vector const numbers{{1, 1, 1, 2, 2, 2}};
std::vector<int> sums(2);
constexpr std::vector<int>::size_type const sub_size{3};
std::vector const expected{{3, 4}};
static constexpr auto const sub_size{3};

CAPTURE(numbers);
CAPTURE(sums.size());
Expand All @@ -217,8 +217,8 @@ TEST_CASE("sub_array_sums", "[sub_array_sums]")
{
std::vector const numbers{{1, 1, 1, 2, 2, 2}};
std::vector<int> sums(3);
constexpr std::vector<int>::size_type const sub_size{3};
std::vector const expected{{3, 4, 5}};
static constexpr auto const sub_size{3};

CAPTURE(numbers);
CAPTURE(sub_size);
Expand All @@ -232,8 +232,8 @@ TEST_CASE("sub_array_sums", "[sub_array_sums]")
{
static constexpr std::array const numbers{1, 1, 1, 2, 2, 2};
std::array<int, 4> sums{3, 4, 5, 6};
static constexpr std::array<int, 6>::size_type const sub_size{3};
static constexpr std::array const expected{3, 4, 5, 6};
static constexpr auto const sub_size{3};

CAPTURE(numbers);
CAPTURE(sums.size());
Expand All @@ -247,8 +247,8 @@ TEST_CASE("sub_array_sums", "[sub_array_sums]")
{
static constexpr std::array const numbers{1, 1, 1, 2, 2, 2};
std::array<int, 6> sums{-1, -1, -1, -1, -1, -1};
static constexpr std::array<int, 6>::size_type const sub_size{3};
static constexpr std::array const expected{3, 4, 5, 6, -1, -1};
static constexpr auto const sub_size{3};

CAPTURE(numbers);
CAPTURE(sums.size());
Expand Down

0 comments on commit fadc796

Please sign in to comment.