From da8b08e4e3d7c189c11b1db6abf10926ba8373f7 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Tue, 12 Oct 2021 15:02:33 +0200 Subject: [PATCH 1/3] abacus: bignum: use IIFE I think it looks cleaner to use Immediately Invoked Function Expressions in such cases. --- src/bignum/bignum.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/bignum/bignum.cc b/src/bignum/bignum.cc index 2121cdc..6b6987e 100644 --- a/src/bignum/bignum.cc +++ b/src/bignum/bignum.cc @@ -82,12 +82,12 @@ digits_type do_addition(digits_type const& lhs, digits_type const& rhs) { ++it2; } - auto it = it1; - auto end = end1; - if (it1 == end1) { - it = it2; - end = end2; - } + auto [it, end] = [=]() { + if (it1 != end1) { + return std::make_pair(it1, end1); + } + return std::make_pair(it2, end2); + }(); while (it != end) { int addition = *it + carry; From 4ea7a38b614fdef27423a340c85459ce151fdf70 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Tue, 12 Oct 2021 15:05:38 +0200 Subject: [PATCH 2/3] abacus: use C++20 --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d343e5a..7d72bf3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ enable_testing() add_library(common_options INTERFACE) target_compile_features(common_options INTERFACE - cxx_std_17 + cxx_std_20 ) target_compile_options(common_options INTERFACE -Wall From 8cb26ee31611287cc32d8cf042bac47198e5c5f0 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Tue, 12 Oct 2021 15:05:21 +0200 Subject: [PATCH 3/3] abacus: bignum: use span Once again, the code looks cleaner that way. --- src/bignum/bignum.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/bignum/bignum.cc b/src/bignum/bignum.cc index 6b6987e..cadc170 100644 --- a/src/bignum/bignum.cc +++ b/src/bignum/bignum.cc @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -82,18 +83,17 @@ digits_type do_addition(digits_type const& lhs, digits_type const& rhs) { ++it2; } - auto [it, end] = [=]() { + auto leftover = [=]() { if (it1 != end1) { - return std::make_pair(it1, end1); + return std::span(it1, end1); } - return std::make_pair(it2, end2); + return std::span(it2, end2); }(); - while (it != end) { - int addition = *it + carry; + for (auto value : leftover) { + int addition = value + carry; carry = addition / BASE; res.push_back(addition % BASE); - ++it; } if (carry != 0) {