[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] valyuta/004 58962eb 01/11: Refactor
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] valyuta/004 58962eb 01/11: Refactor |
Date: |
Sun, 20 Dec 2020 17:40:08 -0500 (EST) |
branch: valyuta/004
commit 58962eb3e6acf018e557e5b752a39cbf1e50ec44
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>
Refactor
Write "T {}" to get a zero of type T, as the "Implementation note"
explains, to avoid needless work when constructing "currency {}".
---
stratified_algorithms.hpp | 23 ++++++++++++++---------
1 file changed, 14 insertions(+), 9 deletions(-)
diff --git a/stratified_algorithms.hpp b/stratified_algorithms.hpp
index 4dd7631..a31a4d8 100644
--- a/stratified_algorithms.hpp
+++ b/stratified_algorithms.hpp
@@ -74,6 +74,15 @@
/// so it's in the second bracket. The second rate is applied to the
/// entire amount of 1500. The function is not monotone except in the
/// degenerate case of uniformly equal rates.
+///
+/// Implementation note: Many function templates in this header
+/// require a zero of the appropriate type T, and accordingly define
+/// a local variable
+/// static constexpr T zero {};
+/// in case this zero is expensive to construct. It is constructed
+/// as "T{}" rather than "T(0)" because the latter uses an explicit
+/// integer argument, which may require a converting constructor
+/// (for example, with class currency).
namespace tiered_and_banded_rates{} // doxygen workaround.
@@ -159,8 +168,7 @@ T tiered_product<T>::operator()
,std::vector<T> const& rates
) const
{
- // Cache T(0) in case it's expensive to construct.
- T const zero = T(0);
+ static constexpr T zero {};
LMI_ASSERT(zero <= new_incremental_amount);
LMI_ASSERT(zero <= prior_total_amount);
@@ -225,8 +233,7 @@ T tiered_rate<T>::operator()
,std::vector<T> const& rates
) const
{
- // Cache T(0) in case it's expensive to construct.
- T const zero = T(0);
+ static constexpr T zero {};
T product = tiered_product<T>()(amount, zero, incremental_limits, rates);
T result = rates.at(0);
@@ -279,8 +286,7 @@ T banded_rate<T>::operator()
,std::vector<T> const& rates
) const
{
- // Cache T(0) in case it's expensive to construct.
- T const zero = T(0);
+ static constexpr T zero {};
LMI_ASSERT(zero <= total_amount);
LMI_ASSERT(!cumulative_limits.empty());
@@ -343,8 +349,7 @@ T banded_product<T>::operator()
template<typename T>
void progressively_limit(T& a, T& b, T const& limit)
{
- // Cache T(0) in case it's expensive to construct.
- T const zero = T(0);
+ static constexpr T zero {};
LMI_ASSERT(zero <= limit);
if(a <= zero && b <= zero)
@@ -464,7 +469,7 @@ void progressively_limit(T& a, T& b, T const& limit)
template<typename T>
T progressively_reduce(T& a, T& b, T const& delta)
{
- T const zero = T(0); // Cache T(0) in case it's expensive to construct.
+ static constexpr T zero {};
T r(delta); // Return value.
if(zero == r)
{
- [lmi-commits] [lmi] valyuta/004 updated (3742c54 -> b73e751), Greg Chicares, 2020/12/20
- [lmi-commits] [lmi] valyuta/004 be42a58 03/11: Assert rather than assign, Greg Chicares, 2020/12/20
- [lmi-commits] [lmi] valyuta/004 4a58266 09/11: Suppress the costly assertion that was recently added, Greg Chicares, 2020/12/20
- [lmi-commits] [lmi] valyuta/004 2913536 06/11: Fix a unit test, Greg Chicares, 2020/12/20
- [lmi-commits] [lmi] valyuta/004 c5f666f 08/11: Improve rounding to cents for solves, Greg Chicares, 2020/12/20
- [lmi-commits] [lmi] valyuta/004 34326d6 04/11: Eliminate a return code that was always zero, Greg Chicares, 2020/12/20
- [lmi-commits] [lmi] valyuta/004 abb1363 05/11: Refactor a unit test, Greg Chicares, 2020/12/20
- [lmi-commits] [lmi] valyuta/004 7eacf2b 07/11: Prefer explicit to implicit currency rounding in unit test, Greg Chicares, 2020/12/20
- [lmi-commits] [lmi] valyuta/004 58962eb 01/11: Refactor,
Greg Chicares <=
- [lmi-commits] [lmi] valyuta/004 9a03ce9 10/11: Add a handy git command, Greg Chicares, 2020/12/20
- [lmi-commits] [lmi] valyuta/004 b73e751 11/11: Record timings as of 20201215T2247Z SHA1 3742c540, Greg Chicares, 2020/12/20
- [lmi-commits] [lmi] valyuta/004 0c93fc2 02/11: Fix a problem somewhat mystically, Greg Chicares, 2020/12/20